有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java EE项目中的tomcat Log4j2配置文件

我有一个简单的MavenJava EE,它基于运行在Windows 10中的Jerseyweb服务。项目是从jersey-quickstart-webapp工件创建的。我试图使用Log4j记录器,但找不到放置log4j2.xml文件的位置。我试图将其与源代码java文件放在一起,但记录器抱怨没有找到配置文件。保存配置文件的正确位置是什么


共 (2) 个答案

  1. # 1 楼答案

    根据docs,Log4j2将按以下顺序查找配置文件:

    1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
    2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
    3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
    4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
    5. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
    6. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
    7. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
    8. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
    9. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
    10. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

    所以您可以看到,除了(1)之外,所有人都在从类路径查看配置文件。在标准Maven项目中,/src/main/resources是类路径的根。所以你可以把log4j2.xml放在/src/main/resources

    否则,必须通过log4j.configurationFile系统属性显式指定配置文件:

    -Dlog4j.configurationFile=path/to/log4j2.xml
    
  2. # 2 楼答案

    把它放到资源目录中。通常,所有配置文件都保存在那里