java ClassCastException: org.slf4j.impl.Log4jLoggerAdapter 不能转换为 ch.qos.logback.classic.Logger

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

ClassCastException: org.slf4j.impl.Log4jLoggerAdapter cannot be cast to ch.qos.logback.classic.Logger

javalogginglog4jslf4jlogback

提问by Erando

I was following thisanswer in order to add a appender on runtime. Even though that works for the original poster, I get this exception in line Logger logger = (Logger) LoggerFactory.getLogger("abc.xyz");:

我正在关注这个答案,以便在运行时添加一个 appender。即使这适用于原始海报,我也得到了这个例外Logger logger = (Logger) LoggerFactory.getLogger("abc.xyz");

java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerAdapter cannot be cast to ch.qos.logback.classic.Logger
    de.mypackage.controller.MyController.meinOeOrte(MyController.java:335)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

Simple question... why does it work for him and not for me? :-P

一个简单的问题……为什么它对他有用而不对我有用?:-P

回答by Kyle McVay

This looks like a symptom of having multiple versions of the same dependency (slf4j) in your classpath.

这看起来像是在您的类路径中具有相同依赖项 (slf4j) 的多个版本的症状。

Look in your logs for this message:

在您的日志中查看此消息:

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Class path contains multiple SLF4J bindings.

It will default to using the first slf4j reference it finds in the classpath. In the past, I've fixed this by moving my Logback dependencies (logback-classic and logback-core) to the top of the dependencies section of my Maven pom.xml file, which places them earlier in the classpath. That's a fragile solution, and it may not work depending on your application architecture. (e.g. if your startup project contains the conflicting dependency in its pom.xml, and you reference Logback through another project and its pom.xml)

它将默认使用它在类路径中找到的第一个 slf4j 引用。过去,我通过将我的 Logback 依赖项(logback-classic 和 logback-core)移动到我的 Maven pom.xml 文件的依赖项部分的顶部来解决这个问题,这将它们放在类路径中的较早位置。这是一个脆弱的解决方案,它可能无法工作,具体取决于您的应用程序架构。(例如,如果您的启动项目在其 pom.xml 中包含冲突的依赖项,并且您通过另一个项目及其 pom.xml 引用 Logback)

回答by gursahib.singh.sahni

Due to an already existing dependency of SLF4J in your project or in some other inherited project, there might be conflicts during runtime. Adding an exclusion to my POMfile worked for me:

由于您的项目或其他一些继承项目中已经存在 SLF4J 的依赖关系,因此在运行时可能会发生冲突。向我的POM文件添加排除项对我有用:

        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>

回答by SHREYA PRASAD

In this case make sure that the library you are using for Logger and LoggerFactory should be the same For example if LoggerFactory is from slf4j then Logger should not be of log4j or java.util. Make sure it is also from slf4j. Let me know if you still face the issue.

在这种情况下,请确保您用于 Logger 和 LoggerFactory 的库应该相同。例如,如果 LoggerFactory 来自 slf4j,则 Logger 不应是 log4j 或 java.util。确保它也来自 slf4j。如果您仍然遇到此问题,请告诉我。

回答by Ankit Salunkhe

I resolved it by using logback class libraries and avoid using slf4j. slf4j is only an abstraction and its not mandatory to use. below is the code snippet.

我通过使用 logback 类库解决了它并避免使用 slf4j。slf4j 只是一个抽象,并不强制使用。下面是代码片段。

import below classes

导入以下类

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;


LoggerContext context = new LoggerContext();
Logger logger = context.getLogger("testLogger");

Here you go. you have a logger and LoggerContext.

干得好。你有一个记录器和 LoggerContext。