50

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
David Tinker
  • 9,383
  • 9
  • 66
  • 98

3 Answers3

47

You can use MulticolorLayout from jcabi-log. Add this dependency to the project:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-log</artifactId>
  <version>0.17.1</version>
</dependency>

And then configure it in log4j.properties:

log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=com.jcabi.log.MulticolorLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%color{%p}] %c: %m%n

Same in log4j.xml:

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="com.jcabi.log.MulticolorLayout">
        <param name="ConversionPattern" value="[%color{%p}] %m%n" />
    </layout>
</appender>

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:

log4j.appender.CONSOLE.layout.ConversionPattern=[%p] $color-cyan{%c}: %color-red{%m}%n
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] $color-0;0;31{%c}: %m%n

More documentation about ANSI colors.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • Added a more advanced example of log4j and a example output. I'm quite pleased with this solution, and I wanted to save someone else having to deal with ansi colors, logging levels, conversion patterns colors and testing. Tested in Ansi Console on Eclipse and ANSICON. – ggb667 Apr 02 '14 at 21:01
  • 0.15 is the current version. – ggb667 Oct 04 '14 at 04:57
31

You can extend PatternLayout and override format(ILoggingEvent). There you could look at LoggingEvent.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:

  "\u001b["  // Prefix - see [1]
+ "0"        // Brightness
+ ";"        // Separator
+ "31"       // Red foreground
+ "m"        // Suffix
+ text       // the text to output
+ "\u001b[m " // Prefix + Suffix to reset color

Here some examples:

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:

<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
               value="\u001b[0;%X{randColor}m ....... \u001b[m" />
    </layout>
</appender>

[1] What does "\u001B[J" represent?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • 3
    I don't want to "damage" the output logs by adding all sorts of formatting to the log messages as the log messages go to files as well. I want this to be done by configuration of the logging system and only for the console logger. – David Tinker Oct 21 '11 at 11:08
  • 5
    "[...]different color **when logging to a console**[...]" – Tomasz Nurkiewicz Oct 21 '11 at 11:08
  • 5
    [Tips working on Macos Terminal] To get more colors you can do this : `\u001b[0;38;2;233;235;235m....your text....\u001b[m` For details see https://en.wikipedia.org/wiki/ANSI_escape_code and search for `38;2;;;` *found this thread while looking for colored text in Terminal and this post helped me so I share this little tips* – Maxence Jul 23 '14 at 09:29
3

See PatternLayout, configuration property "highlight": https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout

Description of the highlight property

vbg
  • 613
  • 6
  • 10