java 类路径中的多个 Log4j.properties 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10890505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:59:29  来源:igfitidea点击:

Multiple Log4j.properties files in classpath

javalogginglog4j

提问by cosmos

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

Log4j 如何在其类路径中管理多个 log4j.properties?哪个 log4j.properties 文件优先?让我描述一下确切的场景。

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.

我有多个由不同团队开发的 maven 模块,每个模块都有自己的 log4j.properties 文件。所有这些 log4j.properties 文件都配置了 RootLogger 以及 ConsoleAppender 和 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 ?

现在,当 Log4j 加载哪个 log4j.properties 文件时,它将用于配置 RootLogger 设置?另外,Log4j 将如何创建 Logger 层次结构?其他 3rd 方 jar 中的 log4j.properties 文件将如何影响日志记录过程?

采纳答案by JB Nizet

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.

将加载类路径中的第一个文件。所以如果 A.jar 和 B.jar 都包含一个文件,并且 A.jar 在类路径中的 B.jar 之前,A.jar 的文件将被加载。这就是类加载器的工作方式。

回答by Kieron Hardy

log4j ver 1.x:

log4j 版本 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

正如其他人所说,log4j 在类路径中查找第一个配置文件。请参阅: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'.

但是,当类路径中同时存在 'log4j.xml' 和 'log4j.properties' 文件时,从实验看来,log4j 优先于 'log4j.xml' 而不是 '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.

即首先,log4j 似乎在类路径中查找第一个“log4j.xml”文件。如果没有,那么 log4j 似乎会在类路径中查找第一个“log4j.properties”文件。

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:

log4j 版本 2.x:

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

默认配置文件的搜索顺序记录在此处: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.

即对于 log4j 版本 2.x,如果没有找到更高优先级的配置文件(例如 log4j2-test.[properties | yaml | json | xml]),那么如果在类路径中找到文件 log4j2.properties 将被使用。请注意,log4j2.xml 的优先级最低,只有在找不到 log4j2 'properties'、'yaml' 或 'json' 配置文件时才会使用。

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

注意:为了帮助调试 log4j 配置问题,设置“log4j.debug”属性,例如:

java -Dlog4j.debug ... 

See also:How to initialize log4j properly?

另请参阅:如何正确初始化 log4j?