The product I am currently developing is a unix server for market datas. Log4J is being use for trace and debug. If you also use Log4J in your Java program, and your program generates traces in a term, this might interest you. I wrote a simple Log4J appender that generates ANSI colors for any Log4J trace. If your application outputs a DEBUG message, a green string message is outputed to the xterm. This appender doesn’t work under Windows 2000 and NT4. It might be interesting for any console java application such as JBoss if your xterm has support for colors. Feel free to use it but don’t forget to send me credits if you include-it in your apps.
/** * Copyright (c) 2004-2008 Nicolas Martignole * All rights reserved * http://www.jroller.com/page/Trecollo/Weblog * http://www.touilleur-express.fr * Created by : Nicolas Martignole * Date: 13 oct. 2004 Time: 15:12:23 */ package com.reuters.pds.common.sdk.util; import org.apache.log4j.Layout; import org.apache.log4j.Level; import org.apache.log4j.WriterAppender; import org.apache.log4j.spi.LoggingEvent; import org.apache.log4j.spi.ThrowableInformation; /** * ANSIColorAppender is a Log4J Appender that output LoggingEvent messages using ANSI colors. * * Each level (DEBUG,INFO,WARN) has its own color that you can customize. * To use this file, in your log4j.properties or log4j.xml specify ANSIColorAppender * instead of a ConsoleAppender. * * @author Nicolas Martignole (nicolas_at_martignole_dot_net) * @version 13 oct. 2004 */ public class ANSIColorAppender extends WriterAppender { /** * Method from Log4j AppenderSkeleton that gets call for any Log4J events. * * @param event * @see org.apache.log4j.AppenderSkeleton */ public void append(LoggingEvent event) { System.out.print(colorizeToANSI(layout.format(event), event.getLevel(), event.getThrowableInformation())); } /** * Requires a layout * * @return true */ public boolean requiresLayout() { return true; } /** * This method overrides the parent {@link WriterAppender#closeWriter} * implementation to do nothing because the console stream is not ours to close. */ protected final void closeWriter() { } /** * @param layout */ public void setLayout(Layout layout) { super.setLayout(layout); } /** * Colorizes the specified message for the specified log4j level. * To get a list of available colors see url http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html. */ private String colorizeToANSI(Object message, Level level, ThrowableInformation ti) { StringBuffer oBuffer = new StringBuffer(); switch (level.toInt()) { case Level.ALL_INT: oBuffer.append("\u001b[1m\u001b[30m"); break; case Level.FATAL_INT: oBuffer.append("\u001b[1m\u001b[31m"); break; case Level.ERROR_INT: oBuffer.append("\u001b[31m"); break; case Level.WARN_INT: oBuffer.append("\u001b[35m"); break; case Level.INFO_INT: oBuffer.append("\u001b[34m"); break; case Level.DEBUG_INT: oBuffer.append("\u001b[32m"); break;// dark green } oBuffer.append(message); oBuffer.append("\u001b[0m"); // Print in red the whole exception stack trace if (ti != null) { String s[] = ti.getThrowableStrRep(); for (int i = 0; i < s.length; i++) { oBuffer.append("\n\u001b[31m"); oBuffer.append(s[i]); oBuffer.append("\u001b[0m"); } } oBuffer.append("\u001b[0m"); return oBuffer.toString(); } }