What are the different approaches for changing the log4j log level dynamically, so that I will not have to redeploy the application. Will the changes be permanent in those cases?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can use following code snippet
File Watchdog
Log4j is able to watch the
log4j.xml
file for configuration changes. If you change the log4j file, log4j will automatically refresh the log levels according to your changes. See the documentation oforg.apache.log4j.xml.DOMConfigurator.configureAndWatch(String,long
) for details. The default wait time between checks is 60 seconds. These changes would be persistent, since you directly change the configuration file on the filesystem. All you need to do is to invoke DOMConfigurator.configureAndWatch() once.Caution: configureAndWatch method is unsafe for use in J2EE environments due to a Thread leak
JMX
Another way to set the log level (or reconfiguring in general) log4j is by using JMX. Log4j registers its loggers as JMX MBeans. Using the application servers MBeanServer consoles (or JDK's jconsole.exe) you can reconfigure each individual loggers. These changes are not persistent and would be reset to the config as set in the configuration file after you restart your application (server).
Self-Made
As described by Aaron, you can set the log level programmatically. You can implement it in your application in the way you would like it to happen. For example, you could have a GUI where the user or admin changes the log level and then call the
setLevel()
methods on the logger. Whether you persist the settings somewhere or not is up to you.I did this to dynamically Change log4j log level and it worked for me, I have n't referred any document. I used this system property value to set my logfile name. I used the same technique to set logging level as well, and it worked
passed this as JVM parameter (I use Java 1.7)
in the log4j.properties file, I added this entry
I tried
It all worked. hope this helps!
I have these following dependencies in my pom.xml
Changing the log level is simple; modifying other portions of the configuration will pose a more in depth approach.
The changes are permanent through the life cyle of the
Logger
. On reinitialization the configuration will be read and used as setting the level at runtime does not persist the level change.UPDATE: If you are using Log4j 2 you should remove the calls to
setLevel
per the documentation as this can be achieved via implementation classes.Log4j2 can be configured to refresh its configuration by scanning the log4j2.xml file (or equivalent) at given intervals. Just add the "monitorInterval" parameter to your configuration tag. See line 2 of the sample log4j2.xml file, which tells log4j to to re-scan its configuration if more than 5 seconds have passed since the last log event.
If you would want to change the logging level of all the loggers use the below method. This will enumerate over all the loggers and change the logging level to given level. Please make sure that you DO NOT have
log4j.appender.loggerName.Threshold=DEBUG
property set in yourlog4j.properties
file.