Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j multiple configuration files

Tags:

log4j

I have couple of projects embedded in a web app as jars. Each project has a log4j.properties file. When the web app is deloyed, which configuration file gets used and how to override the configurations in log4j.xml in a jar file. The jars are not web projects. They are more like service layer code. What is the order in which the log4j.properties file is loaded in the below scenario

Web-project
   classes
      log4j.properties
   ProjectB.jar
      com
      log4j.properties
   ProjectC.jar
      com
      log4j.properties and so on. 
like image 392
user373201 Avatar asked Nov 07 '11 21:11

user373201


People also ask

Where is log4j configuration file?

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.

What is the use of log4j properties file?

properties file is a log4j configuration file which keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j. properties in the CLASSPATH. The level of the root logger is defined as DEBUG.


1 Answers

If your jars are separate web applications, each web application should use the one it first finds on the classpath (WEB-INF/classes).

You can pass a -Dlog4j.configuration=path_to_file setting to e.g. the tomcat startup to make sure that it uses the one you intend it to use. However, this would then to my understanding and knowledge be the one that tomcat will use for every webapp that is deployed.

Question here is how you deploy your apps. Either all web applications in one tomcat in which case you probably want each web application to use a different log4.properties (or log4j.xml) or in the case where you specify one to tomcat, it should use the one you specify.

What it boils down to as far as i know: Either the first one found on classpath (remember: each web-app has it's own classpath) or the one you specify via the -D setting.

Just found this reference which i think nicely summarizes the main concepts of logging in tomcat and webapps deployed in tomcat: http://wiki.apache.org/tomcat/FAQ/Logging

If you need even more control over the log4j logging, you can resort to coding the log4j configuration in java. However, this would mean that you have to modify the source code and add code into it, which relates to infrastructure and relates deployment details to your application (not so nice).

like image 81
mkro Avatar answered Sep 19 '22 16:09

mkro