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:
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...)