It is possible to filter messages using markers, such as :
<MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/>
However I'm trying to route a message based on the marker using the RoutingAppender. I don't want to filter the same arguments multiple times in multiple Appenders. Here's my configuration sample (yaml):
Routing:
name: ROUTING_APPENDER
Routes:
pattern: "$${ctx:marker}" #<-- How to use Marker here?
Route:
- key: MyRoutingKey
ref: MyCustomAppender
The documentation stipulates :
The pattern is evaluated against all the registered Lookups and the
result is used to select a Route
However there seems to be no Lookup for Markers, same for LogLevel. It would be possible to add a custom MarkerValue or LogLevelValue in the ThreadContextMap but I don't find the solution really efficient, it duplicates known information.
Is it not documented or just impossible? Should there be a built-in way to have access to those values in Lookup?
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.