I'm trying to code my own RewritePolicy in Log4j2. The documentation states that :
RewritePolicy is an interface that allows implementations to inspect and possibly modify LogEvents before they are passed to Appender. RewritePolicy declares a single method named rewrite that must be implemented. The method is passed the LogEvent and can return the same event or create a new one.
Here's my java class :
public final class MarkerInjectorRewritePolicy implements RewritePolicy {
@Override
public LogEvent rewrite(final LogEvent event) {
final Marker marker = event.getMarker();
if (marker == null)
return event;
// If there's a Marker, add it to the ThreadContextMap so the RoutingAppender can properly routes log messages
event.getContextMap().put("_marker", marker.getName());
return event;
}
}
Here's my yaml configuration file :
Rewrite:
name: REWRITE_APPENDER
AppenderRef:
ref: ROUTING_APPENDER
PropertiesRewritePolicy:
Property:
- name: foo
value: bar
However I have no idea how to inject it in my configuration file. How can I make it work at runtime?