Dashboard > HOW-TOS > ... > Java > ANSI Color logging with log4j for any appender
HOW-TOS Log In   View a printable version of the current page.
ANSI Color logging with log4j for any appender
Added by Peter Krenesky, last edited by Peter Krenesky on Mar 06, 2006  (view change)
Labels: 
(None)

This is a tutorial on how to add ANSI color to your log4j output. This method uses an extension of [PatternLayout] that prepends ANSI codes based on the log level of the message being written. This layout should work with any appender.

1) download and compile ANSIColorLayout.java

2) modify log4j.properties to use [ANSIColorLayout]

# set the layout class
log4j.appender.A1.layout=org.osuosl.logging.ANSIColorLayout

# change layout pattern (optional)
log4j.appender.A1.layout.ConversionPattern=%-5p [%d{MM-dd-yyyy HH:mm:ss}] %c - %m%n

#change colors from defaults for class (optional)
log4j.appender.A1.all=\u001B[1;37m
log4j.appender.A1.fatal=\u001B[1;31m
log4j.appender.A1.error=\u001B[0;31m
log4j.appender.A1.warn=\u001B[1;33m
log4j.appender.A1.info=\u001B[0;37m
log4j.appender.A1.debug=\u001B[0;36m
log4j.appender.A1.stacktrace=\u001B[0;31m
log4j.appender.A1.defaultcolor=\u001B[1;37m

3) tail your log in an ANSI compatible shell

hey, thought this thing was great. One issue I had was that the colors weren't getting reset on each line, so if I tailed, then quit out, my terminal ended up in the same color as the last log4j line that printed out in my log. That, plus non-log4j lines were being printed in the same color as the last log4j line, which was making things confusing for me. I realize some folks may want it that way, but fyi, if you want to change it so that the color is reset after every line, just add the line below (with the +):

--- /tmp/ANSIColorLayout.java   2005-12-07 18:22:33.000000000 -0800
+++ ANSIColorLayout.java        2005-12-07 18:13:51.000000000 -0800
@@ -183,6 +183,7 @@
                 break;
         }
         oBuffer.append(super.format(loggingEvent));
+        oBuffer.append(defaultcolor);
 
         return oBuffer.toString();
     }

Then I change my default color as follows in my log4j.properties:

log4j.appender.A1.layout.defaultcolor=\u001B[0m

and all is well.

(from ck, I would have signed in, but unfortunately, the howto space isn't accessible for logged in folks without other perms, looks like...)

Posted by Anonymous at Dec 07, 2005 18:30 | Permalink

Hi,

I cannot make the ANSIColorLayout work with the org.apache.log4j.ConsoleAppender appender. When the properties are loaded then the errors

log4j:WARN No such property [info] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [warn] in org.apache.log4j.ConsoleAppender.
log4j:WARN No such property [fatal] in org.apache.log4j.ConsoleAppender.
R log4j:WARN No such property [debug] in org.apache.log4j.ConsoleAppender.
R log4j:WARN No such property [stacktrace] in org.apache.log4j.ConsoleAppender.
R log4j:WARN No such property [error] in org.apache.log4j.ConsoleAppender.
[09.12.05 11:00:33:667 CET] 15b1a07f SystemErr R log4j:WARN No such property [all] in org.apache.log4j.ConsoleAppender.
[09.12.05 11:00:33:667 CET] 15b1a07f SystemErr R log4j:WARN No such property [defaultcolor] in org.apache.log4j.ConsoleAppender.

Posted by Anonymous at Dec 09, 2005 02:05 | Permalink

continuing last entry...

Those errors are displayed.

My log4j.properties file looks like

log4j.rootLogger=ERROR, OTTO
...
log4j.appender.OTTO=org.apache.log4j.ConsoleAppender
log4j.appender.OTTO.layout=org.osuosl.logging.ANSIColorLayout
log4j.appender.OTTO.layout.ConversionPattern= %c.%M (%F:%L) - %m%n

#change colors from defaults for class (optional)
log4j.appender.OTTO.all=\u001B[1;37m
log4j.appender.OTTO.fatal=\u001B[1;31m
log4j.appender.OTTO.error=\u001B[0;31m
log4j.appender.OTTO.warn=\u001B[1;33m
log4j.appender.OTTO.info=\u001B[0;37m
log4j.appender.OTTO.debug=\u001B[0;36m
...

Thanks for any hint

-Malu

Posted by Anonymous at Dec 09, 2005 02:09 | Permalink

Hi again,
I found the solution to this problem. The layout before the property name was missed.
log4j.appender.OTTO.layout.debug=\u001B[0;36m

Cheers
-Malu

Posted by Anonymous at Dec 09, 2005 02:22 | Permalink

It was just an oversight, resetting to the default color should be the default behavior.  I didn't notice bleeding in my shell because each logging message would change to its configured color and my colored prompt reset the colors when I exited tail.  In any case, I applied this patch to the version in the SVN repository.

 Thanks for the contribution and feedback. 

Posted by Peter Krenesky at Mar 06, 2006 14:42 | Permalink
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.7 Build:#524 Jul 28, 2006) - Bug/feature request - Contact Administrators