Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

java - How to create custom RewritePolicy in log4j2?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...