java - java 9 找不到 log4j2 配置文件

标签 java log4j2 java-9 module-info

我有一个 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;
}

项目结构如下:

structure

build.gradle 文件如下:

enter image description here

最佳答案

log4j~faq建议至少使用 log4j-api-2.xlog4j-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.getResourceXXXAPIs. 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/

相关文章:

java - 使用 JShell 进行调试

java - java中的二维字符串数组排序

Java multiple extends Zk Component 变得非常冗长

java - Log4j 配置 : how to use environment variable in yaml configuration file log4j2. yaml

log4j2 - 使用 log4j2 进行公共(public)日志记录

java - 为什么像 List/Map/Set.of(...) 或 Arrays.asList(...) 这样的方法会返回不可变列表?

java - 为什么我们在java-9模块系统中需要require static?

用于带有 JDBCthin 驱动程序的 xml 类型的 Java API

java - to.UpperCase 跳过它应该工作的过程

java - 如何屏蔽 log4j2 日志消息