Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reload log4j.xml / log4j.properties file dynamically in Tomcat?

Tags:

java

tomcat

log4j

The problem is, whenever you change the log4j.properties/log4j.xml, you need to restart the tomcat [ or say any other server ]. Is there any workaround of reloading the log4j configuration?

like image 964
Rakesh Juyal Avatar asked Jan 22 '10 08:01

Rakesh Juyal


People also ask

Where is log4j properties file located in Tomcat?

Log4j and Tomcat Libraries are placed in common library directory. The configuration file for Tomcat is in common/classes directory, the configuration file for a application is placed in the WEB-INF/classes folder of the application.

Where should log4j properties file be stored?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.


2 Answers

From http://logging.apache.org/log4j/1.2/faq.html#3.6

Is there a way to get log4j to automatically reload a configuration file if it changes?

Yes. Both the DOMConfigurator and the PropertyConfigurator support automatic reloading through the configureAndWatch method. See the API documentation for more details.

Because the configureAndWatch launches a separate wathdog thread, and because there is no way to stop this thread in log4j 1.2, the configureAndWatch method is unsafe for use in J2EE envrironments where applications are recycled.

Said that, I've successfully used PropertyConfigurator#configureAndWatch method in a Java EE environment (Sun One Web Server, not Tomcat).

like image 192
Guido Avatar answered Sep 23 '22 15:09

Guido


As of log4j 2.x you can reload the config periodically, in this example every 30 seconds:

<configuration monitorInterval="30"> 

Please take a look here for more information on log4j 2.x configuration:

like image 35
Cristian Măgherușan-Stanciu Avatar answered Sep 24 '22 15:09

Cristian Măgherușan-Stanciu