The only solution I have found so far is to use 5 markers:
final Marker traceMarker = MarkerFactory.getMarker("TRACE"); final Marker debugMarker = MarkerFactory.getMarker("DEBUG"); final Marker infoMarker = MarkerFactory.getMarker("INFO"); final Marker warnMarker = MarkerFactory.getMarker("WARN"); final Marker errorMarker = MarkerFactory.getMarker("ERROR"); final Marker fatalMarker = MarkerFactory.getMarker("FATAL");
And the log going through the marker every time:
logger.info(infoMarker, "!!! INFO World !!!"); logger.error(errorMarker, "!!! ERROR World !!!"); logger.error(fatalMarker, "!!! FATAL World !!!");
And modify PatternLayout to completely remove LogLevel and always register a marker, for example:
PatternLayout: Pattern: "%d{ISO8601_BASIC} %marker [%t] %logger{3.} - %msg%n"
I think this solution is a hack ... It will also remove the log level of any external library using the correct LogLevel.
Summary This solution is not a good solution.
UPDATE . I tried another solution by writing RewritePolicy:
public class FatalRewritePolicy implements RewritePolicy { public static final String FATAL = "FATAL"; @Override public LogEvent rewrite(final LogEvent logEvent) { final Marker marker = logEvent.getMarker(); if (marker == null) return logEvent;
There seems to be no way to change LogLevel for LogEvent in Log4j2 (which makes sense).
Summary : There is still no solution.
Daniel Marcotte
source share