Reputation: 2419
Is it possible to change log4j settings during application execution?
Upvotes: 12
Views: 10705
Reputation: 56724
I'm using:
and it is done automatically (no need to restart Tomcat) if you follow the next steps (let's suppose that you have already deployed the war file to %CATALINA_HOME%\webapps and you have launched startup.bat):
Edit log4j.properties / log4j.xml and change the logger level
E.g.:
log4j.properties:
log4j.rootLogger=INFO, stdout, file
becomes
log4j.rootLogger=DEBUG, stdout, file
log4j.xml
<root>
<level value="INFO" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
becomes
<root>
<level value="DEBUG" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
If you have both files in the classes folder, the XML file will be considered (the .properties file will be ignored).
In 0-10 seconds you'll be able to see the following lines in Tomcat console and in %CATALINA_HOME%\logs\catalina.yourDate.log:
Nov 12, 2015 4:52:49 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/YourApp] has started
Nov 12, 2015 4:52:50 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO ...
Nov 12, 2015 4:52:50 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/YourApp] is completed
Use the application and see the new logging level (in console and/or in the logging files).
Upvotes: 2
Reputation: 274888
You can use PropertyConfigurator.configureAndWatch to make log4j spawn a thread to periodically check your properties file for changes.
Alternatively, you can use JMX described in this post: Change Logging Levels using JMX
Upvotes: 9
Reputation: 9514
There ways of doing this by making your Log4j accessible through JMX and using a JMX console to control the filters, loggers, levels, appenders etc...
I have an example somewhere but I need to prune it a little to make it understandable. Would you want this ?
Upvotes: 3