Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Re-Execute Log4j "Default Initialization Procedure"?

Tags:

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.

like image 887
curd0 Avatar asked Jan 13 '11 23:01

curd0


People also ask

How does log4j get initialized?

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.

How do I change my log4j configuration?

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.

What is log4j default configuration?

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.


1 Answers

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.

like image 149
Jan Zyka Avatar answered Oct 15 '22 08:10

Jan Zyka