I am tracking down some concurrency issues and it would be very helpful to have the output lines from each thread in a different color when logging to a console. I am on OS X. Could this be done using a conversion pattern to output some control codes or would it need a custom appender? Anyone know how?
2011-10-21 12:14:42,859 ["http-bio-8080"-exec-9] DEBUG ...
2011-10-21 12:14:43,198 ["http-bio-8080"-exec-10] DEBUG ...
The lines for exec-9 and exec-10 should be in different colors.
You can extend
PatternLayoutand overrideformat(ILoggingEvent). There you could look atLoggingEvent.getThreadName()to get some color based on the thread name (odd/even, maybe?).In order to output color to the console, you’ll need to use an ANSI Escape Sequence.
For instance, to output a red text:
Here some examples:
ColoredPatternLayoutimplementation by Ingo Thon.Just to add, maybe you could also achieve this by setting in the MDC a variable “randColor” with a random ANSI color code, for instance, in a
Filter, and using it in theconversionPatternof a standardorg.apache.log4j.PatternLayoutin your log4j’s console appender configuration:[1] What does "\u001B[J" represent?