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 Wolfgang Hoschek <wh...@lbl.gov> on 2004/06/14 22:54:38 UTC

ColorConsoleAppender Contribution

Here is a ColorConsoleAppender contribution that shows different log 
levels in
different colors; very useful for effectively sieving through large 
number of
log messages. Feel free to include this contribution in log4j in 
whichever way you'd see fit.
Most likely, it could be done in a somewhat cleaner manner, and 
possibly with Windows supports as well.

Regards,
Wolfgang.


package gov.lbl.dsd.sea.nio.util;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;

/**
  * Log4J Appender which prints to System.out and shows different log 
levels in
  * different colors; very useful for effectively sieving through large 
number of
  * log messages. Colors are used only on Unix, and ignored on Windows 
because we
  * don't know how to do it. (If it's possible and you know how, submit a
  * patch!); If you wish to use this appender let log4j know about it by 
adding
  * 
"log4j.appender.CONSOLE=gov.lbl.dsd.sea.nio.util.ColorConsoleAppender" 
to the
  * log4j.properties file.
  *
  */
public class ColorConsoleAppender extends ConsoleAppender {

	private static final char ESCAPE = 033;
	private static final String UNESCAPE = "[m";
	private static final String REVERSE = "[7m";

	private static final String BLACK = "[30m";
	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 boolean isWindows = 
System.getProperty("os.name").startsWith("Windows");

	// overridden
	protected void subAppend(LoggingEvent event) {
		this.qw.write(formatEvent(event));

		if (layout.ignoresThrowable()) {
			String[] s = event.getThrowableStrRep();
			if (s != null) {
				int len = s.length;
				for (int i = 0; i < len; i++) {
					if (!isWindows) {
						this.qw.write(toColorString(event.getLevel()).toString());						
					}
					this.qw.write(s[i]);
					if (!isWindows) {
						this.qw.write(ESCAPE + UNESCAPE);						
					}
					this.qw.write(Layout.LINE_SEP);
				}
			}
		}

		if (this.immediateFlush) {
			this.qw.flush();
		}
	}

	protected String formatEvent(LoggingEvent event) {

		String toPrint = this.layout.format(event);
		if (isWindows) {
			return toPrint;
		}
		else {
			StringBuffer s = this.toColorString(event.getLevel());
			
			//remove newline
			s.append(toPrint.substring(0, toPrint.length() - 1));

			// reset colors
			s.append(ESCAPE).append(UNESCAPE);
			
			s.append(Layout.LINE_SEP);
			return s.toString();
		}

	}

	protected StringBuffer toColorString(Level level) {
		StringBuffer s = new StringBuffer(2);
		s.append(ESCAPE);
		
		switch (level.toInt()) {
			case Level.ERROR_INT:
				s.append(RED);
				break;
			case Level.WARN_INT:
				s.append(PURPLE);
				break;
			case Level.INFO_INT:
				s.append(GREEN);
				break;
			case Level.FATAL_INT:
				s.append(RED).append(ESCAPE).append(REVERSE);
				break;
			case Level.DEBUG_INT:
				s.append(BLUE);
				break;
			default:
				s.append(CYAN);
				break;
		}
		return s;
	}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org