简体   繁体   English

如果 Java 版本为 11、11.1x 等,为什么 log4j 停止运行 MDC 逻辑

[英]Why log4j stop running MDC logic if Java version is 11, 11.1x, etc

I use MDC for helping log4j loging more information.我使用 MDC 帮助 log4j 记录更多信息。 Everything works well until I updated java version to 11. This is my log4j config (pseodo code)一切正常,直到我将 java 版本更新为 11。这是我的 log4j 配置(伪代码)

log4j.appender.R.layout.ConversionPattern = [%-4p] %d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%X{request-id}] %l %m%n

This what I did for MDC这就是我为 MDC 所做的

MDC.put("request-id","example-request-id")

By expectation, there should be request-id being logged.按照预期,应该记录 request-id。 However it doesn't work if Java version is 11.但是,如果 Java 版本为 11,则它不起作用。

Following is my findings from log4j code.以下是我从 log4j 代码中得到的结果。

There is a code in "org.apache.log4j.helpers.Loader" to set java1 , “org.apache.log4j.helpers.Loader”中有一段代码设置java1

    String prop = OptionConverter.getSystemProperty("java.version", null);
    
    if(prop != null) {
      int i = prop.indexOf('.');
      if(i != -1) { 
        if(prop.charAt(i+1) != '1')
        java1 = false;
      } 
    }

And then, some code in "org.apache.log4j.MDC#MDC" to stop logic.然后,“org.apache.log4j.MDC#MDC”中的一些代码停止逻辑。

void put0(String key, Object o) {
    if(java1 || tlm == null) {
      return;
    }
...
}

In summary, the MDC wouldn't work if the java version were 11,12,13,..., 11.1x, 12.1x,..., ect.总之,如果 java 版本是 11,12,13,..., 11.1x, 12.1x,..., MDC 将无法工作。 Isn't this look like a bug?这看起来不像一个错误吗?

This is a bug.这是一个错误。 However, Log4j is marked as end-of-life since August 5, 2015 ( http://logging.apache.org/log4j/1.2/ ) and但是,自 2015 年 8 月 5 日起,Log4j 被标记为生命周期结束 ( http://logging.apache.org/log4j/1.2/ ) 并且

Users of Log4j 1 are recommended to upgrade to Apache Log4j 2建议使用 Log4j 1 的用户升级到 Apache Log4j 2

Specifically the problem with MDC and Java 9 is well known: Log4j 1.2 is broken on Java 9 , but it is unlikely that anyone will fix this ever.特别是 MDC 和 Java 9 的问题是众所周知的: Log4j 1.2 在 Java 9 上被破坏了,但任何人都不太可能修复这个问题。

If you can't switch to log4j2 for whatever reason or can't use another log provider, then you can copy the org.apache.log4j.helpers.Loader class from log4j1 into your project keeping the original package name (org.apache.log4j.helpers).如果您出于任何原因无法切换到 log4j2 或无法使用其他日志提供程序,那么您可以将org.apache.log4j.helpers.Loader class 从 log4j1 复制到您的项目中,保留原始名称 package (org.88352839820230458) .

Then in the Loader.java inside your project:然后在你项目里面的Loader.java中:

  • comment the part of the static block that deals with the java version, here ;此处评论 static 块中处理 java 版本的部分;
  • comment the boolean java1 = true class variable;注释boolean java1 = true class 变量;
  • change the isJava1() method to always return true ;isJava1()方法更改为始终返回true
  • locations that reference the java1 variable must call the isJava1() method.引用java1变量的位置必须调用isJava1()方法。

Now MDC works on Log4j1 + Java9+.现在 MDC 适用于 Log4j1 + Java9+。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM