gpt4 book ai didi

java - 使用LoggerContext后,log4j2的控制台中显示错误日志

转载 作者:行者123 更新时间:2023-12-01 16:13:25 32 4
gpt4 key购买 nike

我正在使用 LoggerContext 在代码中设置 log4j2,xml 路径,并收到 No log4j2 configuration file found 错误日志。

代码

    LoggerContext context = (org.apache.logging.log4j.core.LoggerContext)LogManager.getContext(false);
String fileName = "\\resources\\log4j2.xml";
context.setConfigLocation(new File(fileName).toURI());
LOG.debug("This Will Be Printed On Debug");
LOG.info("This Will Be Printed On Info");
LOG.warn("This Will Be Printed On Warn");

输出

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only 
errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to
TRACE to show Log4j2 internal initialization logging.
DEBUG | 2020-06-19 14:16:21 | [main] | (Log4j2Example.java:22) - This Will Be Printed On Debug
INFO | 2020-06-19 14:16:21 | [main] | (Log4j2Example.java:23) - This Will Be Printed On Info
WARN | 2020-06-19 14:16:21 | [main] | (Log4j2Example.java:24) - This Will Be Printed On Warn

在这里我得到了预期的结果,但也在开始时得到了错误日志。如果我在 Eclipse 的类路径中设置 log4j2,xml 则不会显示错误。

如何在使用 LoggerContext 时删除错误日志?

最佳答案

当您调用 LogManager.getContext(false) 时,Log4j 将进行初始化。这会导致出现 No log4j2 configuration file found 消息,因为您尚未配置该文件。

尝试使用Configurator.initialize("My Config", "\\resources\\log4j2.xml");

关于java - 使用LoggerContext后,log4j2的控制台中显示错误日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62471277/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com