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?
In order to output color to the console, you'll need to use an ANSI Escape Sequence.
For instance, to output a red text:
"\u001b["// Prefix - see [1]+"0"// Brightness+";"// Separator+"31"// Red foreground+"m"// Suffix+ text // the text to output+"\u001b[m "// Prefix + Suffix to reset color
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 the conversionPattern of a standard org.apache.log4j.PatternLayout in your log4j's console appender configuration:
In this example, %p will be replaced by DEBUG, INFO, ERROR, etc. and then painted into the color that is relevant to the logging level. Besides that you are able to use your own colors or predefined colors, for example: