The documentation for RoutingAppender shows the ThreadContext lookup, but routing can also work with other lookups. One idea is to create a custom lookup.
A custom lookup is implemented as a log4j2 plugin. To help log4j2 find your plugin you can enable packages="yourCustomPackage" in your configuration file. Your plugin class needs to be on the classpath so log4j can find it. Here's the plugin code for the custom lookup:
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;
@Plugin(name = "marker", category = "Lookup")
public class MarkerLookup implements StrLookup {
public String lookup(String key) {
return null
}
public String lookup(LogEvent event, String key) {
final Marker marker = event.getMarker();
return marker == null ? null : marker.getName();
}
}
And in the configuration file :
Routing:
name: ROUTING_APPENDER
Routes:
pattern: "$${marker:}"
Route:
- key: PERFORMANCE
ref: PERFORMANCE_APPENDER
- key: PAYLOAD
ref: PAYLOAD_APPENDER
- key: FATAL
ref: FATAL_APPENDER
- ref: APPLICATION_APPENDER #Default route
Credits to the Log4j2 developers (https://issues.apache.org/jira/browse/LOG4J2-1015).
UPDATE : According to them, it should be built-in in the next version (2.4). So no needs to write custom plugin after that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…