Tuesday, April 8, 2014

Changing log level in log4j without restarting your application

For instance,  you have an application deployed in production and you need to set the log level to debug to investigate an issue. Normally, you will edit the log4j.properties and restart the application in order to have the changes take effect.

What if there are many users currently logged in to the application and you are not allowed to restart?

Will you wait for every user to logoff? What if there are 24/7 operations running on the application?

In that case, restart is not an option. Reverting the log level back to original will also require another restart.

Solution
Implement Log4j file watchdog


Although you will still need to deploy code changes to implement the watcher, you only do it at first then the succeeding changes on your log4j configuration (log4j.properties) will no longer need application restart to take effect.

Here's how it's done
org.apache.log4j.Logger logger = null;
org.apache.log4j.PropertyConfigurator.configureAndWatch("C:\\log4j.properties");
logger = org.apache.log4j.Logger.getLogger("");
logger.info("Info Message");
logger.error("Error Message");
logger.warn("Warn Message");
logger.trace("Trace Message");



No idea on what is log4j and how to use it?
Visit https://logging.apache.org/log4j/1.2/

No comments:

Post a Comment

AOL Directory Sumbit Url
Submit Site to Yahoo