gpt4 book ai didi

java - 我可以让 log4j 使用资源文件夹中的配置吗?

转载 作者:行者123 更新时间:2023-12-02 04:42:04 24 4
gpt4 key购买 nike

让 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 :

  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 YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  3. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  4. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  5. If a test file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  6. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  7. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  8. 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 不同。

关于java - 我可以让 log4j 使用资源文件夹中的配置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com