3

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?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Daniel Marcotte
  • 1,270
  • 3
  • 16
  • 27

2 Answers2

6

Your custom rewrite policy should be coded as a log4j2 plugin. This enables you to configure your custom RewritePolicy in a RewriteAppender.

@Plugin(name = "InjectMarkerPolicy", category = "Core",
        elementType = "rewritePolicy", printObject = true)
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's context map is immutable, so need to make a copy...
        Map<String, String> mdc = new HashMap<>(event.getContextMap());
        mdc.put("_marker", marker.getName());

        LogEvent result = new Log4jLogEvent(event.getLoggerName(), event.getMarker(),
            event.getLoggerFqcn(), event.getLevel(), event.getMessage(),
            event.getThrown(), mdc, event.getContextStack(),
            event.getThreadName(), event.getSource(), event.getTimeMillis());

        return result;
    }
}

Example config (TODO: set correct value for packages attribute):

<Configuration status="trace" packages="my.rewritepolicy.plugin.package">
  <Appenders>
    <Console name="STDOUT">
      <PatternLayout pattern="[%-5level] %c{1.} %m%n"/>
    </Console>
    <Rewrite name="Rewrite">
      <InjectMarkerPolicy />
      <AppenderRef ref="STDOUT"/>
    </Rewrite>
  </Appenders>
  <Loggers>
    <Root level="trace">
      <AppenderRef ref="Rewrite"/>
    </Root>
  </Loggers>
</Configuration>
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Thank you it's working #1. This is a nice workaround for routing on the Marker field (as proposed here : https://issues.apache.org/jira/browse/LOG4J2-1015) at the cost of creating the Log4jLogEvent twice. – Daniel Marcotte May 15 '15 at 15:22
  • 1
    In Yaml : Rewrite: name: REWRITE_APPENDER AppenderRef: ref: ROUTING_APPENDER InjectMarkerPolicy: ref: anyString #mandatory – Daniel Marcotte May 15 '15 at 15:24
  • I am getting error in below yaml config. log4j2.xml configuration works fine. Any idea? Error - "InjectMarkerPolicy contains an invalid element or attribute "ref" Rewrite: name: REWRITE_APPENDER AppenderRef: ref: console InjectMarkerPolicy: ref: anyString – Jinesh Mathew Jan 28 '21 at 17:51
  • @JineshMathew Looking at the XML in my answer, `InjectMarkerPolicy` doesn’t have a `ref` attribute. – Remko Popma Jan 28 '21 at 19:34
6

Don't forget the Factory Method for your plugin or it will fail to start:

    @PluginFactory
    public static MarkerInjectorRewritePolicy createPolicy() {
        return new MarkerInjectorRewritePolicy();
    }

here's a working example on my github account: https://github.com/sercasti/Log4j-RewriteAppender/

sercasti
  • 550
  • 3
  • 7
  • 1
    Your example was very helpful, thank you. Also something that tripped me up was adding packages="com.my.package" to the Configuration element. The documentation doesn't make it clear that this is necessary. – jerney Sep 28 '17 at 14:22