Simple Configuration for Log4j

Ever wanted to use Log4j, but didn’t know how to configure it? Or you don’t remember how to configure it in a jiffy?

Just add the following two lines in your application, like say in the main()-method and then your’re done:


BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d %5p [%t] %C{1} %M - %m%n")));
Logger.getRootLogger().setLevel(Level.INFO);

The first line sets logging to go to the console and it will look something like this:

2008-04-18 11:21:33,395 INFO [main] TestApp main - exists: true

If you want a different logging level, then just take Level.DEBUG, or whatever else suits you.