我正在使用LoggerContext在代码中设置log4j2,xml路径并获取未找到log4j2配置文件错误日志。
代码
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时如何删除错误日志?
答案 0 :(得分:0)
调用LogManager.getContext(false)时,Log4j将初始化。这会导致出现“找不到log4j2配置文件”消息,因为您尚未进行配置。
尝试使用Configurator.initialize("My Config", "\\resources\\log4j2.xml");