2

I am trying to append a tag (a string) to every log message at log4j2, and the message will be pass to socket serializedlayout for output. so it that possible to intercept the log message and add some tag (string) to it?

Below is log4j2.xml

<Socket name="LOGSTASH" host="192.168.0.1" port="4567" immediateFlush="true" >
        <SerializedLayout />
</Socket>

Below is calling

private static final Logger log = LogManager.getLogger(aClazz.class);

private void aMethod(){
    log.error("ooops");
}

Below is origin output

method : aMethod()
class : aClazz
level : error
message : ooops
trace.........

Below is expected output

method : aMethod()
class : aClazz
level : error
message : [Some String] ooops
trace.........
Asher
  • 21
  • 1

1 Answers1

0

Because you are using the SerializedLayout you can't take advantage of the normal mechanism for injecting ThreadContext values into the output string. However, you can wrap your SocketAppender in a RewriteAppender and create a custom rewrite policy to modify the message however you would like, including modifying the message by inserting a ThreadContext value.

rgoers
  • 8,696
  • 1
  • 22
  • 24
  • thanks for your answer, but I found this post said that "RewriteAppender will not be able to do anything with either SimpleMessage or ParameterizedMessages" [link](http://stackoverflow.com/questions/22218832/how-to-use-log4j2-xml-rewrite-appender-for-modifying-logevent-before-it-logs-in) – Asher Dec 31 '15 at 02:00