Java Log4j2 配置 - 未找到 log4j2 配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25487116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:28:53  来源:igfitidea点击:

Log4j2 configuration - No log4j2 configuration file found

javalogginglog4j2

提问by Qbisiek

Lately I decided to learn how to use the log4j2 logger. I downloaded required jar files, created library, xml comfiguration file and tried to use it. Unfortunately i get this statement in console (Eclipse) :

最近我决定学习如何使用 log4j2 记录器。我下载了所需的 jar 文件,创建了库,xml 配置文件并尝试使用它。不幸的是,我在控制台(Eclipse)中得到了这个声明:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration:       logging only errors to the console.

This is my testing class code:

这是我的测试类代码:

package log4j.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4jTest
{
    static final Logger logger = LogManager.getLogger(Logger.class.getName());

    public static void main(String[] args) {    
        logger.trace("trace");
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        logger.error("error");
        logger.fatal("fatal");
    }
}

And my xml config file:

还有我的 xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test" 
               status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="log4j.test.Log4jTest" level="trace">
            <AppenderRef ref="Console"/>
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

I tried also with xml without <Logger>tags, and package specification and in various folders/packages directories, but it didn't help. Now my log4j2.xmlfile is located directly in project folder in eclipse.

我还尝试使用不带<Logger>标签的xml 、包规范和各种文件夹/包目录,但没有帮助。现在我的log4j2.xml文件直接位于 eclipse 的项目文件夹中。

回答by Tomasz W

Is this a simple eclipse java project without maven etc? In that case you will need to put the log4j2.xmlfile under srcfolder in order to be able to find it on the classpath. If you use maven put it under src/main/resourcesor src/test/resources

这是一个没有 maven 等的简单 eclipse java 项目吗?在这种情况下,您需要将log4j2.xml文件放在文件src夹下,以便能够在类路径中找到它。如果你使用 maven 把它放在src/main/resourcessrc/test/resources

回答by Abdulghaffar Al-Labadi

You need to choose one of the following solutions:

您需要选择以下解决方案之一:

  1. Put the log4j2.xml file in resource directory in your project so the log4j will locate files under class path.
  2. Use system property -Dlog4j.configurationFile=file:/path/to/file/log4j2.xml
  1. 将 log4j2.xml 文件放在项目的资源目录中,以便 log4j 将在类路径下定位文件。
  2. 使用系统属性 -Dlog4j.configurationFile=file:/path/to/file/log4j2.xml

回答by Mike Ashby

Eclipse will never see a file until you force a refresh of the IDE. Its a feature! So you can put the file all over the project and Eclipse will ignore it completely and throw these errors. Hit refresh in Eclipse project view and then it works.

在您强制刷新 IDE 之前,Eclipse 永远不会看到文件。它的一个特点!因此,您可以将文件放在整个项目中,Eclipse 将完全忽略它并抛出这些错误。在 Eclipse 项目视图中点击刷新,然后它就可以工作了。

回答by James Jithin

Was following the documentations - Apache Log4j2 Configuratoinand Apache Log4j2 Mavenin configuring log4j2 with yaml. As per the documentation, the following maven dependencies are required:

遵循文档 - Apache Log4j2 ConfiguratoinApache Log4j2 Maven使用 yaml 配置 log4j2。根据文档,需要以下 Maven 依赖项:

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.1</version>
  </dependency>

and

<dependency>
    <groupId>com.fasterxml.Hymanson.dataformat</groupId>
    <artifactId>Hymanson-dataformat-yaml</artifactId>
    <version>2.8.6</version>
</dependency>

Just adding these didn't pick the configuration and always gave error. The way of debugging configurationby adding -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACEhelped in seeing the logs. Later had to download the source using Maven and debugging helped in understanding the depended classes of log4j2. They are listed in org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory:

只是添加这些并没有选择配置并且总是出错。通过添加调试配置的方式-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE有助于查看日志。后来不得不使用 Maven 下载源代码,调试有助于理解 log4j2 的依赖类。它们列在org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory

com.fasterxml.Hymanson.databind.ObjectMapper
com.fasterxml.Hymanson.databind.JsonNode
com.fasterxml.Hymanson.core.JsonParser
com.fasterxml.Hymanson.dataformat.yaml.YAMLFactory

Adding dependency mapping for Hymanson-dataformat-yamlwill not have the first two classes. Hence, add the Hymanson-databinddependency to get yaml configuration working:

添加依赖映射Hymanson-dataformat-yaml不会有前两个类。因此,添加Hymanson-databind依赖项以使 yaml 配置工作:

<dependency>
    <groupId>com.fasterxml.Hymanson.core</groupId>
    <artifactId>Hymanson-databind</artifactId>
    <version>2.8.6</version>
</dependency>

You may add the version by referring to the Test Dependenciessection of log4j-apiversion item from MVN Repository. E.g. for 2.8.1 version of log4j-api, refer this linkand locate the Hymanson-databindversion.

您可以参考MVN RepositoryTest Dependencieslog4j-apiversion item部分添加版本。例如,对于 2.8.1 版本的 log4j-api,请参考此链接并找到版本。Hymanson-databind

Moreover, you can use the below Java code to check if the classes are available in the classpath:

此外,您可以使用以下 Java 代码检查类路径中是否有这些类:

System.out.println(ClassLoader.getSystemResource("log4j2.yml")); //Check if file is available in CP
ClassLoader cl = Thread.currentThread().getContextClassLoader(); //Code as in log4j2 API. Version: 2.8.1
 String [] classes = {"com.fasterxml.Hymanson.databind.ObjectMapper",
 "com.fasterxml.Hymanson.databind.JsonNode",
 "com.fasterxml.Hymanson.core.JsonParser",
 "com.fasterxml.Hymanson.dataformat.yaml.YAMLFactory"};

 for(String className : classes) {
     cl.loadClass(className);
 }

回答by Seweryn Habdank-Wojewódzki

Additionally to what Tomasz W wrote, by starting your application you could use settings:

除了 Tomasz W 所写的内容之外,通过启动您的应用程序,您可以使用设置:

-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE 

to get most of configuration problems.

解决大部分配置问题。

For details see Log4j2 FAQ: How do I debug my configuration?

有关详细信息,请参阅 Log4j2 常见问题解答:如何调试我的配置?

回答by Florian

In my case I had to put it in the bin folder of my project even the fact that my classpath is set to the src folder. I have no idea why, but it's worth a try.

在我的情况下,即使我的类路径设置为 src 文件夹,我也必须将它放在我项目的 bin 文件夹中。我不知道为什么,但值得一试。