我有一个 Java 8 项目,我的配置文件在资源文件夹中,一切正常。现在我切换到 Java 9,添加了 log4j.api
的要求,并且找不到配置文件了。
我是否需要在我的 module-info
文件中添加其他内容以便记录器找到它的配置?
现在是这样
module project.main {
requires jdk.incubator.httpclient;
requires jackson.databind;
requires jackson.core;
requires jackson.annotations;
requires log4j.api;
}
项目结构如下:
build.gradle 文件如下:
最佳答案
log4j~faq建议至少使用 log4j-api-2.x
和 log4j-core-2.x
。最好将这些添加到您的 build.gradle
文件中:
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.9.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: '2.9.0'
并确保 conflicting dependencies are excluded
在 module-info.java
中,您将进一步更新(这是我在 custom maven project 中所做的)
requires log4j; // not log4j.api
它应该也适用于相同的目录结构。
提示:这是我开始调试它的地方。 Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses
Thread.getContextClassLoader().getResource()
to locate the default configuration files and does not directly check the file system...
在 ClassLoader#getResource
中放置了一个调试点方法,只关注图书馆寻找的资源。
同时提出关于资源的观点,如 resources/foo/bar/log4j.properties
,如 JDK-8142968 的发行说明中所述。
- JDK internal resources, other than class files, in the standard and JDK modules can no longer be located with the
ClassLoader.getResourceXXX
APIs. This may impact code that relies on using these APIs to get at JDK internal properties files or other resources.
并得到 ClassLoader#getResource
的 java-doc 的支持还有:
- Resources in named modules are subject to the encapsulation rules specified by
Module.getResourceAsStream
. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).
关于java - java 9 找不到 log4j2 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46138895/