java 9 未找到 log4j2 配置文件
log4j2 configuration file not found with java 9
我有一个 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
方法中放置了一个调试点,只关注库寻找的资源。
还要提出 JDK-8142968
中发行说明中所述的 resources/foo/bar/log4j.properties
等资源的观点
- 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 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
方法中放置了一个调试点,只关注库寻找的资源。
还要提出 JDK-8142968
中发行说明中所述的resources/foo/bar/log4j.properties
等资源的观点
- 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).