本文介绍了ClassCastException:org.slf4j.impl.Log4jLoggerAdapter无法强制转换为ch.qos.logback.classic.Logger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注回答为了在运行时添加一个appender。即使这适用于原始海报,我也会在行中获得此异常 Logger logger =(Logger)LoggerFactory.getLogger(abc.xyz);

I was following this answer 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");:

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)

简单的问题......为什么它适用于他而不适合我? :-P

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

推荐答案

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

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

在日志中查找此消息:

SLF4J:类路径包含多个SLF4J绑定。

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

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)

这篇关于ClassCastException:org.slf4j.impl.Log4jLoggerAdapter无法强制转换为ch.qos.logback.classic.Logger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:55