log4j pattern %X and what property to assign to it

Gleeb picture Gleeb · Aug 30, 2011 · Viewed 8.4k times · Source

i am trying to use a log viewer (doesn't matter which one) to parse my log files.

my log4j pattern is this.

%p [%t] (%C{1}:%M():%L) %d{dd/MM/yyyy-HH:mm:ss,SSS} S:%X{serviceType} N:%X{requestID}- %m%n

the log viewers (at least the open source ones) need you to implement a pattern so they will be able to read the file.

for example: for the log4j pattern: %p [%t] (%C{1}:%M():%L) %d{dd/MM/yyyy-HH:mm:ss,SSS} - %m%n

the log viewer pattern would be: pattern= pattern=LEVEL [THREAD] (CLASS:METHOD():LINE) TIMESTAMP - MESSAGE

the example works well.

but i have not been able to parse the %X property in any way. i have seen there are property types NDC and PROP(key) but i seem to either miss use them or they are not related to the %X

so the question is how to implement the pattern so it will read the %X parameter.

thanks.

Answer

Cygnusx1 picture Cygnusx1 · Aug 31, 2011

Ok, i think i see the problem.

Your application use the log4J MDC since it use the %X in the pattern layout. Your log viewer seems to support only NDC.

log4j pattern layout for NDC is %x (lowercase).

If you have control on the application, you have to change MDC -> NDC and modifiy the log4j.xml to use %x instead of %X. That may be a big task if the app is huge...

Another solution would be to find a log viewer that support MDC(%X)

I tried to look around for the PROP(key), but there is not much doc on it ;-(

Good luck