zoukankan      html  css  js  c++  java
  • 使用Log4j2,打包后提示ERROR StatusLogger Log4j2 could not find a logging implementation.

    从Log4j切换到Log4j2,没有打包之前日志输出正常,但是打包后总是提示下面内容:

    错误一:

    ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

    错误二:

    ERROR StatusLogger LogManager returned an instance of org.apache.logging.log4j.simple.SimpleLoggerContextFactory which does not implement org.apache.logging.log4j.core.impl.Log4jContextFactory. Unable to initialize Log4j.
    被这个问题折腾了一天多,总算是找到一个解决办法,在这里记录一下。

    错误一:

    网上有人说是没有导入log4j-core,但是导入后还是出现这个错误。

    有人说log4j2有个加载配置文件的顺序(https://logging.apache.org/log4j/2.x/manual/configuration.html),所以试了各种名字,还是失败。

    这里还尝试了添加启动参数(-Dlog4j.configurationFile=log4j2.xml),以及在src下添加log4j2.component.properties文件(内容:log4j.configurationFile=log4j2.xml),结果同样失败。

    错误二:

    这个问题在网上没有多少可以参考的信息,但是在解决错误一时,看到有log4j2.component.properties这个配置文件,所以抱着试试的态度,到官方上查了一下,有这样的内容:

    https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties

    然后果断在src下添加log4j2.component.properties文件(内容:log4j2.loggerContextFactory=org.apache.logging.log4j.core.impl.Log4jContextFactory),接着打包运行,日志正常输出了。鼓掌!!!

    这里还发现了一个比较好用的配置参数,就是上面提到的log4j.configurationFile。

    尝试后发现:

    1.如果不配或者只配置名字(log4j2.xml),就会在jar包内查找log4j2.xml

    2.如果配置jar外的绝对路径,就会在jar包外查找log4j2.xml。这个好处就是配置改动后,就不用重新打包了。

    log4j2.component.properties:

    1 log4j2.loggerContextFactory=org.apache.logging.log4j.core.impl.Log4jContextFactory
    2 log4j.configurationFile=/home/*****/*****/config/log4j2.xml
    3 #log4j.configurationFile=log4j2.xml
  • 相关阅读:
    重读《从菜鸟到测试架构师》-- 职业生涯的考虑
    重读《从菜鸟到测试架构师》-- 从专家到高手
    重读《从菜鸟到测试架构师》-- 开发团队做的远不仅是开发
    重读《从菜鸟到测试架构师》-- 测试专家的第一步
    重读《从菜鸟到测试架构师》-- 前篇
    使用Nodejs+Protractor搭建测试环境
    简单的算法题之合并数组
    性能测试流程
    性能测试基础
    电脑木马
  • 原文地址:https://www.cnblogs.com/moonpool/p/6548266.html
Copyright © 2011-2022 走看看