[英]log4j2 configuration file not found with java 9
I had a Java 8 project and my configuration file was in resource folder and everything worked fine. 我有一个Java 8项目,我的配置文件在资源文件夹中,一切正常。 Now I switched to Java 9, added requirement for
log4j.api
, and configuration file cannot be found anymore. 现在我切换到Java 9,添加了对
log4j.api
要求,并且无法再找到配置文件。
Do I need to add something else in my module-info
file for the logger to find it's config? 我是否需要在我的
module-info
文件中添加其他东西,以便记录器找到它的配置?
For now, it's like this 就目前而言,就是这样
module project.main {
requires jdk.incubator.httpclient;
requires jackson.databind;
requires jackson.core;
requires jackson.annotations;
requires log4j.api;
}
The Project structure is as: 项目结构如下:
The build.gradle file is as: build.gradle文件如下:
The log4j~faq suggests using at least log4j-api-2.x
and log4j-core-2.x
. log4j~faq建议至少使用
log4j-api-2.x
和log4j-core-2.x
. Preferably add these to your build.gradle
file: 最好将这些添加到
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'
And make sure conflicting dependencies are excluded 并确保排除冲突的依赖项
In the module-info.java
further you shall update(this is what I did in a custom maven project ) 在
module-info.java
你应该更新(这是我在自定义maven项目中所做的)
requires log4j; // not log4j.api
It should work for you then with the same directory structure as well. 它应该适用于您,然后使用相同的目录结构。
Hint : This is where I started debugging it from. 提示 :这是我开始调试它的地方。 Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?
为什么我会看到“没有找到记录器的appender”和“请正确配置log4j”的警告?
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.
当无法找到默认配置文件log4j.properties和log4j.xml且应用程序未执行显式配置时,会发生这种情况。 log4j uses
Thread.getContextClassLoader().getResource()
to locate the default configuration files and does not directly check the file system...log4j使用
Thread.getContextClassLoader().getResource()
来定位默认配置文件,而不是直接检查文件系统...
Placed a debug point in the ClassLoader#getResource
method and just keep an eye of resources looked for by the library. 在
ClassLoader#getResource
方法中放置一个调试点,只关注库所查找的资源。
Also to bring up the point over resources like resources/foo/bar/log4j.properties
as stated in the release notes in JDK-8142968 还要提出
resources/foo/bar/log4j.properties
等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.标准和JDK模块中的JDK内部资源(类文件除外)不能再与
ClassLoader.getResourceXXX
API一起定位。 This may impact code that relies on using these APIs to get at JDK internal properties files or other resources.这可能会影响依赖于使用这些API获取JDK内部属性文件或其他资源的代码。
and seconded by the java-doc of ClassLoader#getResource
as well: 并且也被
ClassLoader#getResource
的java-doc借调:
- Resources in named modules are subject to the encapsulation rules specified by
Module.getResourceAsStream
.命名模块中的资源受
Module.getResourceAsStream
指定的封装规则的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).此外,除了资源的名称以“.class”结尾的特殊情况之外,此方法只会在无条件打开包时找到命名模块包中的资源(即使此方法的调用方是相同的)模块作为资源)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.