我可以让 log4j 使用资源文件夹中的配置吗?
Can I make log4j use the configuration in my resources folder?
让 log4j 工作比宣传的要复杂得多。我不断收到此错误:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
我试图将我的 log4j.xml
文件放在各处,但我想要的是将它放在我的 /resources
文件夹中。在我的pom.xml
中,定义了这些路径:
<properties>
<src.dir>src/</src.dir>
<test.dir>test/</test.dir>
<resource.dir>resources/</resource.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<testSourceDirectory>${test.dir}</testSourceDirectory>
<sourceDirectory>${src.dir}</sourceDirectory>
<resources>
<resource>
<directory>${resource.dir}</directory>
</resource>
</resources>
... long list of plugins coming, let's cut it off.
resources
目录中的所有资源都可以从 .jar
访问。有图像和图标,一切正常。但是Log4j忽略了配置文件。
那么我怎样才能告诉它文件在哪里并最终开始真正学习使用它呢?
您正在使用 log4j2
- 这需要 log4j2.xml
,而不是 log4j.xml
。
只需重命名文件,将其放在资源文件夹(或类路径中的任何其他位置)即可。
或者,您可以查看 documentation:
- 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 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 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.
但是请注意,log4j2 xml-配置的语法也与版本 1 不同。
让 log4j 工作比宣传的要复杂得多。我不断收到此错误:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
我试图将我的 log4j.xml
文件放在各处,但我想要的是将它放在我的 /resources
文件夹中。在我的pom.xml
中,定义了这些路径:
<properties>
<src.dir>src/</src.dir>
<test.dir>test/</test.dir>
<resource.dir>resources/</resource.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<testSourceDirectory>${test.dir}</testSourceDirectory>
<sourceDirectory>${src.dir}</sourceDirectory>
<resources>
<resource>
<directory>${resource.dir}</directory>
</resource>
</resources>
... long list of plugins coming, let's cut it off.
resources
目录中的所有资源都可以从 .jar
访问。有图像和图标,一切正常。但是Log4j忽略了配置文件。
那么我怎样才能告诉它文件在哪里并最终开始真正学习使用它呢?
您正在使用 log4j2
- 这需要 log4j2.xml
,而不是 log4j.xml
。
只需重命名文件,将其放在资源文件夹(或类路径中的任何其他位置)即可。
或者,您可以查看 documentation:
- 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 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 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.
但是请注意,log4j2 xml-配置的语法也与版本 1 不同。