将 log4j2 与 slf4j 一起使用:java.lang.StackOverflowError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32366586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:25:04  来源:igfitidea点击:

Using log4j2 with slf4j: java.lang.StackOverflowError

javaslf4jlog4j2

提问by Akshay

So I have tried following this(non-maven implementation) and requirements in their web sitefor adding slf4j to log4j. and tried using this code

所以我尝试在他们的网站上遵循这个(非 Maven 实现)和要求,将 slf4j 添加到 log4j。并尝试使用此代码

public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(Main.class);
        logger.info("test");
    }

and added the following to my library

并将以下内容添加到我的库中

  • log4j-api-2.3.jar
  • log4j-core-2.3.jar
  • log4j-sl4j-impl-2.3.jar
  • log4j-to-sl4j-2.3.jar
  • slf4j-api-1.7.12.jar
  • log4j-api-2.3.jar
  • log4j-core-2.3.jar
  • log4j-sl4j-impl-2.3.jar
  • log4j-to-sl4j-2.3.jar
  • slf4j-api-1.7.12.jar

when I try running it I get the following error

当我尝试运行它时,出现以下错误

Exception in thread "main" java.lang.StackOverflowError
    at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
    at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)
    at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:40)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:284)
    at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:41)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:284)...

Any idea where I am going wrong?

知道我哪里出错了吗?

采纳答案by Andreas

You're creating a call-loop with log4j-slf4j-impl-2.3.jarand log4j-to-slf4j-2.3.jar.

您正在使用log4j-slf4j-impl-2.3.jar和创建一个调用循环log4j-to-slf4j-2.3.jar

log4j-slf4j-impl-2.3.jaris the implementation of the adapter that sends slf4j calls to log4j.

log4j-slf4j-impl-2.3.jar是将 slf4j 调用发送到 log4j 的适配器的实现。

log4j-to-slf4j-2.3.jaris sending log4j calls right back to slf4j. Remove this one.

log4j-to-slf4j-2.3.jar正在将 log4j 调用发送回 slf4j。删除这个

回答by LottaLava

Just in case, if you have logbackin your classpath, it must be removed as well!

以防万一,如果您的类路径中有logback,也必须将其删除!