Java 错误 StatusLogger Log4j2 找不到日志记录实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47881821/
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-12 02:42:37  来源:igfitidea点击:

ERROR StatusLogger Log4j2 could not find a logging implementation

javaeclipselog4j2

提问by Alok

I am trying to implement log4j 2but it keeps throwing the following error.

我正在尝试实现log4j 2,但它不断抛出以下错误。

> ERROR StatusLogger Log4j2 could not find a logging implementation.
> Please add log4j-core to the classpath. Using SimpleLogger to log to
> the console...  
> ERROR LogExample This Will Be Printed On Error 
> FATAL LogExample This Will Be Printed On Fatal

I have tried the solution given on the net. But the don't seem to be working for me.

我已经尝试了网上给出的解决方案。但似乎对我不起作用。

This is the codethat I am trying to run.

这是我试图运行的代码

package demo;

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

public class LogExample {

    private static final Logger LOG = LogManager.getLogger(LogExample.class);

    public static void main(String[] args) {

        LOG.debug("This Will Be Printed On Debug");
        LOG.info("This Will Be Printed On Info");
        LOG.warn("This Will Be Printed On Warn");
        LOG.error("This Will Be Printed On Error");
        LOG.fatal("This Will Be Printed On Fatal");
        LOG.info("Appending string: {}.", "Hello, World");
    }

}

Project and dependency added in the pom.xml:

在 pom.xml 中添加的项目和依赖项:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Any help is appreciated.

任何帮助表示赞赏。

采纳答案by Alok

Solved the error message by setting the system property for log4j2 configuration file. below is the below code.

通过设置 log4j2 配置文件的系统属性解决了错误信息。下面是下面的代码。

System.setProperty("log4j.configurationFile","./path_to_the_log4j2_config_file/log4j2.xml");

Logger log = LogManager.getLogger(LogExample.class.getName());

回答by rupweb

To get around a similar issue with Spring Boot I added

为了解决 Spring Boot 的类似问题,我添加了

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>   

to my POM

到我的 POM

回答by Sandun Susantha

This dependency help to avoid this error from lambda.

这种依赖有助于避免 lambda 的这个错误。

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.8.2</version>
</dependency>