You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Bruno Dumon <br...@advalvas.be> on 2001/02/17 14:42:52 UTC
LinuxConsoleAppender with Colour
Hi everyone,
I wrote an Appender for log4j which shows the log statements in different
colours depending on the priority. Tested on Linux (console and xterm).
Code is below.
Greetings,
Bruno Dumon
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
/**
Appender which prints to System.out and shows different
priorities in different colors.
@author <a href="mailto:bruno.dumon@the-ecorp.com">Bruno Dumon</a>
*/
public class LinuxConsoleAppender
extends AppenderSkeleton
{
private static final char ESCAPE = 033;
private static final String RED = "[31m";
private static final String GREEN = "[32m";
private static final String YELLOW = "[33m";
private static final String BLUE = "[34m";
private static final String PURPLE = "[35m";
private static final String CYAN = "[36m";
private static final String SILVER = "[37m";
private static final String WHITE = "[38m";
private static final String REVERSE = "[7m";
// copied from Priority class because they have default access
final static int FATAL_INT = 50000;
final static int ERROR_INT = 40000;
final static int WARN_INT = 30000;
final static int INFO_INT = 20000;
final static int DEBUG_INT = 10000;
protected void append(LoggingEvent event)
{
switch (event.priority.toInt())
{
case ERROR_INT:
System.out.print(ESCAPE);
System.out.print(RED);
break;
case WARN_INT:
System.out.print(ESCAPE);
System.out.print(GREEN);
break;
case INFO_INT:
System.out.print(ESCAPE);
System.out.print(BLUE);
break;
case FATAL_INT:
System.out.print(ESCAPE);
System.out.print(RED);
System.out.print(ESCAPE);
System.out.print(REVERSE);
break;
// DEBUG statements are shown in black
}
// print but remove newline
String toPrint = this.layout.format(event);
System.out.print(toPrint.substring(0, toPrint.length()-1));
// reset colors
System.out.print(ESCAPE);
System.out.print("[m");
// newline
System.out.println();
if(layout.ignoresThrowable()) {
if(event.throwable != null) {
event.throwable.printStackTrace(System.out);
}
else if (event.throwableInformation != null) {
System.out.println(event.throwableInformation);
}
}
}
public boolean requiresLayout()
{
return true;
}
public void close()
{
this.closed = true;
}
}