使用java 9找不到log4j2配置文件 [英] log4j2 configuration file not found with java 9

查看:559
本文介绍了使用java 9找不到log4j2配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java 8项目,我的配置文件在资源文件夹中,一切正常。现在我切换到Java 9,添加了对 log4j.api 的要求,并且再也找不到配置文件了。

I had a Java 8 project and my configuration file was in resource folder and everything worked fine. Now I switched to Java 9, added requirement for log4j.api, and configuration file cannot be found anymore.

我是否需要在我的 module-info 文件中添加其他东西,以便记录器找到它的配置?

Do I need to add something else in my module-info file for the logger to find it's config?

现在,它就像这样

module project.main {
    requires jdk.incubator.httpclient;
    requires jackson.databind;
    requires jackson.core;
    requires jackson.annotations;
    requires log4j.api;
}

项目结构如下:

build.gradle文件如下:

The build.gradle file is as:

推荐答案

log4j~faq 建议至少使用 log4j-api-2.x log4j-core-2.x 。最好将这些添加到 build.gradle 文件中:

The log4j~faq suggests using at least log4j-api-2.x and log4j-core-2.x. Preferably add these to your build.gradle file:

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'

并确保排除冲突的依赖关系

模块中-info.java 你还要更新(这是我在自定义maven项目

In the module-info.java further you shall update(this is what I did in a custom maven project)

requires log4j; // not log4j.api

它应该适用于你,然后使用相同的目录结构。

It should work for you then with the same directory structure as well.

提示:这是我开始调试它的地方。
为什么我会看到没有找到记录器的appender的警告和请正确配置log4j?

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"?


当默认配置文件 log4j.properties 和
log4j.xml无法找到,应用程序不执行任何显式的
配置。 log4j使用 Thread.getContextClassLoader()。getResource()
来查找默认配置文件,而不是直接检查
文件系统...

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 方法,只需保留一个图书馆寻找的资源之眼。

Placed a debug point in the ClassLoader#getResource method and just keep an eye of resources looked for by the library.

同时提出资源,如资源/ foo / bar / log4j.properties JDK-8142968



  • JDK内部资源,而不是类文件,在标准和JDK模块中不能再使用
    ClassLoader.getResourceXXX API来定位。这可能会影响依赖
    的代码,使用这些API来获取JDK内部属性文件或其他
    资源。

  • 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



  • 命名模块中的资源受 Module.getResourceAsStream 指定的封装规则
    的约束。另外,除
    外,资源的名称以.class结尾的特殊情况,
    此方法只会在
    无条件打开包时找到命名模块包中的资源(即使这个
    方法的调用者与资源在同一个模块中)。

  • 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 9找不到log4j2配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆