如何在IntelliJ和Gradle中配置Log4j。

huangapple go评论78阅读模式
英文:

How to configure Log4j with IntelliJ and Gradle

问题

我一定做了某些愚蠢的事情,但我找不到它。我正试图将Log4j添加到我的项目中,但它没有读取属性文件。我已经创建了一个最小的可行示例项目。在IntelliJ中,我创建了一个新的gradle项目。我添加了log4j并添加了一个主类。

以下是项目结构。

.
├── build.gradle
├── gradle
│     └── wrapper
│           ├── gradle-wrapper.jar
│           └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │     ├── java
    │     │     └── Main.java
    │     └── resources
    │           └── log4j.properties
    └── test
        ├── java
        └── resources

build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
}

log4j.properties

log4j.rootLogger=TRACE

Main.java

import org.apache.logging.log4j.LogManager;

public class Main {
    public static void main(String[] args) {
        System.out.println("Print");
        System.out.println("Root Logger Level: " + LogManager.getRootLogger().getLevel());
    }
}

当我运行这个程序时,输出显示根记录器的级别为“ERROR”,尽管我将它设置为trace。当然,这意味着任何低于错误级别的日志语句都不会被打印出来。

Print
Root Logger Level: ERROR

我阅读的每一份文档都说属性文件需要位于类路径上的一个目录中,这是有道理的。根据gradle的文档和我自己的调查,resources目录位于类路径上。在实验中,我尝试将文件放置在层次结构的每个级别,但没有任何区别。我看不出为什么属性文件找不到。

我尝试通过将-Dlog4j.configuration=<full/path/to/properties>添加到VM参数来直接指定它。这仍然不起作用,但是gradle给我以下输出:

BUILD SUCCESSFUL in 748ms
3 actionable tasks: 1 executed, 2 up-to-date
ERROR StatusLogger Reconfiguration failed: No configuration found for '2a139a55' at 'null' in 'null'
10:23:30 PM: Task execution finished 'Main.main()'.

这似乎是在程序终止后发生的。我不确定它是否相关。

我肯定是漏掉了一些愚蠢的东西,但我找不到任何东西。我看了几十个示例,看起来我做得一切都对。我漏掉了什么?

英文:

I must be doing something stupid, but I can't find it. I'm trying to add Log4j to my project, but it isn't reading the properties file. I have created a minimal workable example project. In IntelliJ, I created a new gradle project. I added log4j and added a main class.

Here is the project structure.

.
├── build.gradle
├── gradle
│&#160;&#160; └── wrapper
│&#160;&#160;     ├── gradle-wrapper.jar
│&#160;&#160;     └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │&#160;&#160; ├── java
    │&#160;&#160; │&#160;&#160; └── Main.java
    │&#160;&#160; └── resources
    │&#160;&#160;     └── log4j.properties
    └── test
        ├── java
        └── resources

build.gradle

plugins {
    id &#39;java&#39;
}

group &#39;org.example&#39;
version &#39;1.0-SNAPSHOT&#39;

repositories {
    mavenCentral()
}

dependencies {
    compile group: &#39;org.apache.logging.log4j&#39;, name: &#39;log4j-core&#39;, version: &#39;2.13.3&#39;
}

log4j.properties

log4j.rootLogger=TRACE

Main.java

import org.apache.logging.log4j.LogManager;

public class Main {
    public static void main(String[] args) {
        System.out.println(&quot;Print&quot;);
        System.out.println(&quot;Root Logger Level: &quot; + LogManager.getRootLogger().getLevel());
    }
}

When I run this, the output says that the root logger's level is "ERROR", even though I'm setting it to trace. Of course this means that any log statements below error level are not printed.

Print
Root Logger Level: ERROR

Every piece of documentation I read says that the properties file needs to be in a directory on the classpath, which makes sense. According to the gradle documentation and my own investigation, the resources directory is on the classpath. Experimentally, I have tried placing the file at every level of the hierarchy with no difference. I can't see any reason why the properties file isn't being found.

I tried specifying it directly by adding -Dlog4j.configuration=&lt;full/path/to/properties&gt; to the VM arguments. This still didn't work, but gradle gives me the following output:

BUILD SUCCESSFUL in 748ms
3 actionable tasks: 1 executed, 2 up-to-date
ERROR StatusLogger Reconfiguration failed: No configuration found for &#39;2a139a55&#39; at &#39;null&#39; in &#39;null&#39;
10:23:30 PM: Task execution finished &#39;Main.main()&#39;.

This appears to happen after the program has terminated. I'm not sure if it's related.

I must be missing something stupid, but I can't find anything. I've looked at dozens of examples and it seems like I'm doing everything right. What am I missing?

答案1

得分: 1

对于原始问题,因为您正在尝试使用log4j2,首选的配置模式是xml文件。您可以参考手册:https://logging.apache.org/log4j/2.x/manual/configuration.html

因此,要使用该版本,您可以这样做:

System.setProperty(ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY, "src/main/resources/configuration.xml");
英文:

To the original question, because you are trying to use log4j2, the preferred mode of configuration is xml file. You can refer to manual: https://logging.apache.org/log4j/2.x/manual/configuration.html

So to use the version

System.setProperty(ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY, &quot;src/main/resources/configuration.xml&quot;);

答案2

得分: 0

以下的解决方法对我很有帮助。根据 https://logging.apache.org/log4j/2.x/faq.html 上提到的文档,我们需要将 log4jxml 重命名为 log4j2.xml,因为这是新的命名约定,并将其放置在 src/main/resources 目录下。在我的情况下,只要我这样做了,这个问题就解决了。

英文:

The following solution helped me. As per the documentation mentioned on https://logging.apache.org/log4j/2.x/faq.html, we have to rename log4jxml to log4j2.xml as this is the new naming convention and place this in src/main/resources.In my case this issue was solved as soon as I did this.

huangapple
  • 本文由 发表于 2020年9月10日 10:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63821963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定