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

curd0 picture curd0 · Jan 14, 2011 · Viewed 8.4k times · Source

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.

Answer

Jan Zyka picture Jan Zyka · Feb 15, 2011

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.