JavaEE项目中的Log4j2配置文件
Log4j2 configuration file in Java EE project
我有简单的 Maven
Java-EE 基于 Jersey
Web 服务在我的 Eclipse
中运行 Windows 10. 项目是从创建的jersey-quickstart-webapp
神器。我正在尝试使用 Log4j
记录器,但找不到放置 log4j2.xml
文件的位置。我试图将它与源代码 java 文件放在一起,但记录器抱怨找不到配置文件。保存配置文件的正确位置是什么?
根据docs,Log4j2会按照以下顺序查找配置文件:
- 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.
- If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
- If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
- If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
- If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
- If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
- If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the
classpath.
- If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
- If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
- 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
放入资源目录。通常,所有配置文件都保存在那里。
我有简单的 Maven
Java-EE 基于 Jersey
Web 服务在我的 Eclipse
中运行 Windows 10. 项目是从创建的jersey-quickstart-webapp
神器。我正在尝试使用 Log4j
记录器,但找不到放置 log4j2.xml
文件的位置。我试图将它与源代码 java 文件放在一起,但记录器抱怨找不到配置文件。保存配置文件的正确位置是什么?
根据docs,Log4j2会按照以下顺序查找配置文件:
- 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.
- If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
- If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
- If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
- If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
- If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
- If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
- If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
- If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
- 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
放入资源目录。通常,所有配置文件都保存在那里。