log4j to console programatically

I thought I logged this, but when I searched I couldn’t find it. I guess I have lots more things I forget to log here. And it shows they’re useful, because I do come back here to search for it.

I often write small driver snippets to test my code in small units, and I need an easy way to configure logging so that I can see the output on my console without running to a file. And here’s the solution:

Logger root = Logger.getRootLogger();
root.addAppender(new ConsoleAppender(
    new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));

Source: http://robertmaldon.blogspot.com/2007/09/programmatically-configuring-log4j-and.html

Leave a Reply