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

2022-09-01 00:02:31

因此,我尝试遵循(非maven实现)和他们网站中的要求,将slf4j添加到log4j。并尝试使用此代码

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

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

  • 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

当我尝试运行它时,我得到以下错误

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)...

任何想法我哪里出错?


答案 1

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

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

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


答案 2

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


推荐