Wednesday, May 02, 2007

Increasing log4j verbosity

To programmatically increase log4j verbosity, a simple code snippet does the trick:

final Logger rootLogger = Logger.getRootLogger();

rootLogger.setLevel(Level.toLevel(
        rootLogger.getLevel().toInt() + 1,
        Level.ALL));

I use this to handle the -verbose command-line switch. Repeating the switch keeps increasing my program's verbosity.

Naturally there are wrinkles with a complex log4j configuration, but the idiom remains.

2 comments:

Unknown said...

That snippet's crying out to me, "Hide me behind an intention-revealing method!" Logging.incrementVerbosity(), say. It'd be hard to look at "blah-Level-level-logger-level-blah-Logger + 1" over and over again.

Brian Oxley said...

Paul, come pair with me! :)