import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleListener; import org.apache.log4j.LogManager; import org.apache.log4j.PropertyConfigurator; public class Log4JInitializer implements LifecycleListener { private String propertiesFile; public String getPropertiesFile() { return this.propertiesFile; } public void setPropertiesFile(String propertiesFile) { this.propertiesFile = propertiesFile; } @Override public void lifecycleEvent(LifecycleEvent event) { if (Lifecycle.BEFORE_START_EVENT.equals(event.getType())) initializeLog4j(); } private void initializeLog4j() { // configure from file, and let log4j monitor the file for changes PropertyConfigurator.configureAndWatch(propertiesFile); // shutdown log4j (and its monitor thread) on shutdown Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { LogManager.shutdown(); } }); } }
I simply listen for the "BEFORE_START_EVENT", and if that happens (which is once per Tomcat startup) I initialize Log4j using the configureAndWatch method. I also don't forget to install a shutdown hook to cleanup the thread Log4j creates to poll the configuration file for changes (I could also have chosen to listen to the "AFTER_STOP_EVENT" from Tomcat in stead).
Package this in a jar, put it on the Tomcat classpath, and now you can configure it in your Tomcat serverl.xml.
<Server> ... <Listener className="Log4JInitializer" propertiesFile="/path/to/log4j.properties"/> </Server>Can't be much easier, and it does what it has to do.