Monday, January 14, 2008

Customizing JUnit Logging

//create a console logger
Logger consLogger = Logger.getLogger(UpdateGPBenefitEligCommand.class);
Appender myAppender = new ConsoleAppender(new SimpleLayout());
consLogger.addAppender(myAppender);

//now you can pass this logger into a class
MyClass myClass = new MyClass();
myClass.setLogger(consLogger);

Obviously the class must have a setLogger method and the logger definition in the class cannot be "final". Now you could be using debugging and breakpoints instead but sometimes logging is easier and it's already there you just need to make it more accessible for testing.

No comments: