Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I adjust log4j levels at runtime?

I have a simple web app running on Tomcat 5.5 with log4j for logging. Occasionally I need to push the logging down to DEBUG but most of the time I'm happy with INFO.

I can change my config xml and restart the app but I would prefer to switch the log levels on the fly. Is there a standard technique for this?

like image 348
Dave Patterson Avatar asked Jul 29 '09 13:07

Dave Patterson


People also ask

How do I change the log4j properties at runtime?

Use LogManager. resetConfiguration(); to clear the current config and configure it again. Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config). This way, all the loggers (and their levels, etc) stay intact.

How do I change log level in log4j?

Setting Levels using Configuration File log4j provides you configuration file based level setting which sets you free from changing the source code when you want to change the debugging level. Following is an example configuration file which would perform the same task as we did using the log. setLevel(Level.


2 Answers

Just use the programmatic API:

logger.setLevel(Level.DEBUG)

in your program when you need more verbose logging output, and

logger.setLevel(Level.INFO)

to make it less verbose again.

like image 91
Vinay Sajip Avatar answered Oct 09 '22 20:10

Vinay Sajip


I created a new java application module (jar) to do this in a web interface at runtime, and also have a page to view the log result. Configure the jar is very simple: must be placed in the /WEB-INF/lib folder, and add a mapping to the servlet in the /WEB-INF/web.xml file.

Project site: http://www.log4jwebtracker.com

like image 5
Mariano Ruiz Avatar answered Oct 09 '22 22:10

Mariano Ruiz