You are here

How to make log4j dynamic

log4j works by reading the logging level from log4j.properties. However normally we put the system in such a way that the log4j.properties is read while the application is loaded initially. There are at times, 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 and will pick it up if there are chagnes to it.

Make sure your applicatiuon executes this line of code at starting up. In web application, you can start a Http listener in which this line can be put.

Add new comment