At runtime I often create/modify log4j Loggers, Appenders, Levels, Layouts, and time to time need to reset everything back to defaults.
Log4j system has well defined Default Initialization Procedure that is executed when log4j classes are loaded into memory. Is there any way to re-execute the entire Procedure programmatically later at runtime?
I found several resetConfiguration()
methods in log4j documentation, but not sure if any of them will do what the Default Initialization Procedure does:
BasicConfigurator.resetConfiguration();
Hierarchy.resetConfiguration();
LogManager.resetConfiguration();
Any other suggestions on resetting log4j configurations are more then welcome! Thank you.
properties file exists at that location, it initializes log4j via the PropertyConfigurator using the log4j. properties file. If the log4j. properties file doesn't exist, the application gets initialized via the BasicConfigurator.
The simplest is to put a log4j. xml on the classpath. Then add the monitorInterval="30" attribute to the opening configuration tag. This tells log4j to check the file for changes every 30 seconds.
Log4j will provide a default configuration if it cannot locate a configuration file. The default configuration, provided in the DefaultConfiguration class, will set up: A ConsoleAppender attached to the root logger. A PatternLayout set to the pattern "%d{HH:mm:ss.
According to the documentation for the doConfigure
method:
Read configuration from a file. The existing configuration is not cleared nor reset. If you require a different behavior, then call resetConfiguration
method before calling doConfigure.
So I belive that calling LogManager.resetConfiguration()
and recall PropertyConfigurator.configure()
with the same files as on startup will do what you want.
The resetConfiguration()
method is documented in Hierarchy class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With