Friday, March 12, 2010

Tweaking the log4j BasicConfigurator

This is not magic that I am posting, but something I don't want to sort out again in the future. My current project was involving some extensive debugging of my unit tests, with long log lines on the console causing me to have to full-screen or scroll too much (forced to work on windows and lots of mouse clicks really get my goat).

By digging into the log4j BasicConfigurator.class (with my buddy Maurice, credit to the mastermind!) it has a default setup as follows:

root.addAppender(new ConsoleAppender(new PatternLayout("%r [%t] %p %c %x - %m%n")));

This will generate logging output like this:
4513 [main] DEBUG org.jbpm.graph.def.GraphElement  - event 'process-start' on ProcessDefinition(Exception Handling) for Token(/)
4654 [main] DEBUG org.jbpm.graph.def.GraphElement  -  event 'process-start' on ProcessDefinition(Exception Framework) for Token(/)

I adjusted this in my unit tests as follows:
((PatternLayout) ((Appender) Logger.getRootLogger().getAllAppenders()
  .nextElement()).getLayout())
  .setConversionPattern("%r [%t] %p %c %x -%n%n  %m%n%n");

This will give you output like this:
4513 [main] DEBUG org.jbpm.graph.def.GraphElement  -

  event 'process-start' on ProcessDefinition(Exception Handling) for Token(/)

4654 [main] DEBUG org.jbpm.graph.def.GraphElement  -

  event 'process-start' on ProcessDefinition(Exception Framework) for Token(/)