Java 获取异常 org.apache.logging.slf4j.SLF4JLoggerContext 无法转换为 org.apache.logging.log4j.core.LoggerContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25891737/
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 01:24:51  来源:igfitidea点击:

Getting Exception org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext

javaexceptionlog4j

提问by user3334466

My code is very simple using apache-log4j-2.0.2:

我的代码使用 apache-log4j-2.0.2 非常简单:

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class Log4jtest {
  static Logger log =Logger.getLogger(Log4jtest.class);
  public static void main(String[] args) {
    BasicConfigurator.configure();
        log.debug("This is debug message");
  }

}

But I'm getting exception like:

但我收到了如下异常:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be          cast to org.apache.logging.log4j.core.LoggerContext``
at org.apache.log4j.Logger.getLogger(Logger.java:41)
at Log4jtest.<clinit>(Log4jtest.java:11)

Why is the exception coming from a simple program?

为什么异常来自一个简单的程序?

回答by TheCodingFrog

Remove below jar's from class path and it should fix the issue -

从类路径中删除下面的 jar,它应该可以解决问题 -

log4j-to-slf4j-2.0.2.jar
log4j-to-slf4j-2.0.2-sources.jar
log4j-slf4j-impl-2.0.2.jar
log4j-slf4j-impl-2.0.2-sources.jar

I was able to replicate and fix the issue after downloading apache-log4j-2.0.2 from http://www.apache.org/dyn/closer.cgi/logging/log4j/2.0.2/apache-log4j-2.0.2-bin.zip.

http://www.apache.org/dyn/closer.cgi/logging/log4j/2.0.2/apache-log4j-2.0.2下载 apache-log4j-2.0.2 后,我能够复制并解决问题 -bin.zip

回答by Hau

I was using Maven. I found that declaring my log4j/slf4jdependencies at the top of the <dependencies>list (before Spring Boot, which used logback) fixed the issue.

我正在使用 Maven。我发现在列表顶部声明我的log4j/slf4j依赖项<dependencies>(在 Spring Boot 之前,使用logback)解决了这个问题。



To @TheCodingFrog's credit, adding exclusions to my Spring Boot dependencies also solved the problem. Like so:

对于@TheCodingFrog 的功劳,将排除项添加到我的 Spring Boot 依赖项也解决了这个问题。像这样:

<dependencies>
    <dependency>
        <groupId>...</groupId>
        <artifactId>...</artifactId>
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>*</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>*</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>


NOTE: If you care about which logging framework is actually used, one perhaps important difference is that with @TheCodingFrog's method, slf4jretained logbackas the binding:

注意:如果您关心实际使用哪个日志记录框架,一个可能重要的区别是使用@TheCodingFrog 的方法,slf4j保留logback为绑定:

SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

whereas, with the method I used, slf4j/log4jwas used:

而使用我使用的方法,slf4j/log4j被使用:

SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]


In case anyone's interested, the log4j/slf4jdependencies I used are:

如果有人感兴趣,我使用的log4j/slf4j依赖项是:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
</dependency>