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

新手上路,请多包涵

我正在尝试实现 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

我试过网上给出的解决方案。但似乎不适合我。

这是我要运行的 代码

 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");
    }

}

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

在此处输入图像描述

在此处输入图像描述

任何帮助表示赞赏。

原文由 Alok 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

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

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

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

原文由 Alok 发布,翻译遵循 CC BY-SA 3.0 许可协议

此依赖项有助于避免 lambda 出现此错误。

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

原文由 Sandun Susantha 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题