Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Log4j.properties files in classpath

How does Log4j manages multiple log4j.properties in its classpath? Which log4j.properties file takes precedence? Let me describe the exact scenario.

I have multiple maven modules developed by different teams and each one of them has its own log4j.properties file. All of these log4j.properties file have RootLogger configured along with ConsoleAppender and FileAppenders.

Now, when Log4j loads which log4j.properties file will it use to configure the RootLogger settings ? Also, how will Log4j create the Logger hierarchy ? How will the log4j.properties file in other 3rd party jars affect the logging process ?

like image 391
cosmos Avatar asked Jun 05 '12 01:06

cosmos


People also ask

Where should log4j properties be placed?

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. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

How do you set log file path in log4j properties dynamically?

System. setProperty("{my. log", "C:/logfile. log");

Where is log4j log file location?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.


2 Answers

log4j ver 1.x:

As others have stated, log4j looks for the first configuration file in the classpath. See: http://logging.apache.org/log4j/1.2/manual.html

But when there are both 'log4j.xml' and 'log4j.properties' files in the classpath, it seems from experimentation that log4j gives precedence to 'log4j.xml' over 'log4j.properties'.

i.e. First, log4j seems to look in the classpath for the first 'log4j.xml' file. If there is none, then log4j seems to then look in the classpath for the first 'log4j.properties' file.

Copying and pasting from the code below may help in determining what configuration file is being used:

import org.apache.log4j.Logger;

public class TestLog4j
{

  static
  {
    System.out.println("Classpath: [" + System.getProperty( "java.class.path" ) + "]" );
    System.out.println("Found logging configuration files:");
    System.out.println("  log4j.xml: " + Logger.getRootLogger().getClass().getResource( "/log4j.xml" ) );
    System.out.println("  log4j.properties: " + Logger.getRootLogger().getClass().getResource( "/log4j.properties" ) );
  }

  public static void main(String[] args)
  {
    System.out.println("main():");
  }
}

Edit:

log4j ver 2.x:

The search order for the default configuration file is documented here: http://logging.apache.org/log4j/2.x/manual/configuration.html

i.e. For log4j version 2.x, if no higher precedence configuration file is found (e.g. log4j2-test.[properties | yaml | json | xml]) then the file log4j2.properties is used if found in the classpath. Note that log4j2.xml has the lowest precedence, and will be used only if log4j2 'properties', 'yaml' or 'json' configuration files are all not found.

Note: To aid in debugging log4j configuration issues, set the 'log4j.debug' property, e.g. with:

java -Dlog4j.debug ... 

See also: How to initialize log4j properly?

like image 41
Kieron Hardy Avatar answered Sep 22 '22 04:09

Kieron Hardy


The first file in the classpath will be loaded. So if A.jar and B.jar both contain a file, and A.jar comes before B.jar in the classpath, A.jar's file will be loaded. That's how the class loader works.

like image 156
JB Nizet Avatar answered Sep 22 '22 04:09

JB Nizet