Colorized logging messages in Console or File using the well-known log4j

LOG4J is being used by a lot of JAVA developers.
It is a better way of displaying debugging messages other than using the old "system.out" method.

But under LINUX, you can improve a lot those outputs. You can colorized them !!
Yeah this is true. It's not magic. You just have to remember that years ago,
we were using ANSI syntax on BBS (Bulletin Board Systems ... before the era of Internet).

Thus the trick is just to define your own log4j appender with your own class :
package com.mypackage;

import org.apache.log4j.Level;
import org.apache.log4j.Priority;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.spi.LoggingEvent;

public class ANSIConsoleOrFileAppender extends WriterAppender {
 
private static final int NORMAL = 0;
private static final int BRIGHT = 1;
private static final int FOREGROUND_BLACK = 30;
private static final int FOREGROUND_RED = 31;
private static final int FOREGROUND_GREEN = 32;
private static final int FOREGROUND_YELLOW = 33;
private static final int FOREGROUND_BLUE = 34;
private static final int FOREGROUND_MAGENTA = 35;
private static final int FOREGROUND_CYAN = 36;
private static final int FOREGROUND_WHITE = 37;
private static final String PREFIX = "\u001b[";
private static final String SUFFIX = "m";
private static final char SEPARATOR = ';';
private static final String END_COLOUR = PREFIX + SUFFIX;
private static final String FATAL_COLOUR = PREFIX + BRIGHT + SEPARATOR + FOREGROUND_RED + SUFFIX;
private static final String ERROR_COLOUR = PREFIX + NORMAL + SEPARATOR + FOREGROUND_RED + SUFFIX;
private static final String WARN_COLOUR = PREFIX + NORMAL + SEPARATOR + FOREGROUND_YELLOW + SUFFIX;
private static final String INFO_COLOUR = PREFIX + NORMAL+ SEPARATOR + FOREGROUND_GREEN + SUFFIX;
private static final String DEBUG_COLOUR = PREFIX + NORMAL + SEPARATOR + FOREGROUND_CYAN + SUFFIX;
private static final String TRACE_COLOUR = PREFIX + NORMAL + SEPARATOR + FOREGROUND_BLUE + SUFFIX;
protected void subAppend(LoggingEvent event) {
this.qw.write(getColour(event.getLevel()));
super.subAppend(event);
this.qw.write(END_COLOUR);
if (this.immediateFlush)
this.qw.flush();
}
private String getColour(final Level level) {
switch (level.toInt()) {
case Priority.FATAL_INT: return FATAL_COLOUR;
case Priority.ERROR_INT: return ERROR_COLOUR;
case Priority.WARN_INT: return WARN_COLOUR;
case Priority.INFO_INT: return INFO_COLOUR;
case Priority.DEBUG_INT:return DEBUG_COLOUR;
default: return TRACE_COLOUR;
}
}
}
Then you can define your logging preferences thru this log4j.xml file in your WAR project for example under src/main/webapp/WEB-INF/classes :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

    <appender name="CONSOLE-OUT" class="com.mypackage.ANSIConsoleOrFileAppender">
        <param name="target" value="System.out" />

        <layout class="org.apache.log4j.PatternLayout">
            <param name="conversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="levelMin" value="finest" />
            <param name="levelMax" value="warn" />
        </filter>
    </appender>

    <logger name="com.mypackage">
        <level value="trace"/>   <!-- levels : TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
    </logger>

    <logger name="org.springframework">
        <level value="off"/>
    </logger>
    
    <root>
        <priority value="trace" />
         
        <appender-ref ref="CONSOLE-OUT" />
    </root>
</log4j:configuration>
Then you can see the colored output using your Jetty or Tomcat server.
You just have to be precise when you specify the level of your log message.
In fact those levels will be filtered using a threshold one.

I think we can improve the way it looks inside a single log message by putting the given parameters in bold for example (as it is done in editors using syntax highlighting)
But this will be for another blog ...
In the meantime, you can see how to code color sequences :
ANSI color codes

For the time being, ENJOY your new way of life with proper logging messages !!

0 comments:

Post a Comment

Copyright © IT pages