Social Media

Quick Logging on Log4j

Ok occasionally I need to log from a class that I wouldn’t normally log from so wouldnt have configuration setup. An example being a test class

In the class below you simply add the config into a static method –

[sourcecode lang=”java”] public class MyTest {

static
{
Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.DEBUG);
rootLogger.addAppender(new ConsoleAppender(new PatternLayout("%-6r [%p] %c – %m%n")));
}

private static final Logger LOGGER = Logger.getLogger(MyTest.class);

public void MyMethod() {
LOGGER.debug("in MyMethod");
}

}
[/sourcecode]

Its a hack – but its useful in a hurry

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: