Saturday, July 26, 2014

Making log4j dynamic

log4j works by reading the logging level from log4j.properties. However normally we put the application in such a way that the log4j.properties is read while the application is loaded initially. However there are times when we need to make the logging in such a way that we can change the logging level dynamically. This is especially useful when we want to debug a running system in certain state and we can see more elaborate bahaviour by turning the log levels to more finer levels.
To do that, it's very easy.
PropertyConfigurator.configureAndWatch(<absolute path to the log4j.properties>, <delay in ms>)
This will start the logging system and will start a thread in background that will keep an eye on the log4j.properties file and will pick it up if there are changes to it.
Make sure your application executes this line of code at start up. In a web application, one can put this code in an HttpListener.

No comments:

Post a Comment