org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext

遇到

ClassCastException: org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext


查看log是否提示:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in...


如果是表明已经有多个logging实现,如logback/slf4j-log4j/slf4j-jcl...

确保在同一个classpath下只有一个logging实现。


另外,在Jar包根目录下的logback.xml文件是会加载到classpath里面。


参考:

https://groups.google.com/forum/#!topic/liftweb/QB_bMgsQBwU


以下内容摘自: http://www.slf4j.org/codes.html#multiple_bindings

Multiple bindings were found on the class path

SLF4J API is designed to bind with one and only one underlying logging framework at a time. If more than one binding is present on the class path, SLF4J will emit a warning, listing the location of those bindings.

When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. For example, if you have bothslf4j-simple-1.7.6.jar andslf4j-nop-1.7.6.jar on the class path and you wish to use the nop (no-operation) binding, then removeslf4j-simple-1.7.6.jar from the class path.

The list of locations that SLF4J provides in this warning usually provides sufficient information to identify the dependency transitively pulling in an unwanted SLF4J binding into your project. In your project's pom.xml file, exclude this SLF4J binding when declaring the unscrupulous dependency. For example, cassandra-all version 0.8.1 declares bothlog4j andslf4j-log4j12 as compile-time dependencies. Thus, when you includecassandra-all as a dependency in your project, thecassandra-all declaration will cause bothslf4j-log4j12.jar and log4j.jar to be pulled in as dependencies. In case you do not wish to use log4j as the the SLF4J backend, you can instruct Maven to exclude these two artifacts as shown next:

<dependencies>
  <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

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

  </dependency>
</dependencies>


Note The warning emitted by SLF4J is just that, a warning. Even when multiple bindings are present, SLF4J will pick one logging framework/implementation and bind with it. The way SLF4J picks a binding is determined by the JVM and for all practical purposes should be considered random. As of version 1.6.6, SLF4J will name the framework/implementation class it is actually bound to.

Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J's purpose. When you come across an embedded component declaring a compile-time dependency on any SLF4J binding, please take the time to contact the authors of said component/library and kindly ask them to mend their ways.


根据提供的引用内容,"logback-classic Failed to load class "org.slf4j.impl.StaticLoggerBinder"." 是一个错误信息,表明在使用logback-classic日志框架时,无法加载org.slf4j.impl.StaticLoggerBinder类。这个错误通常发生在缺少相关依赖或配置不正确的情况下。 解决这个问题的方法有以下几种: 1. 确保正确配置了相关依赖:检查项目的依赖配置文件(如pom.xml或build.gradle)中是否包含了正确的logback-classic依赖。例如,在Maven项目中,可以添加以下依赖: ```xml <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> ``` 2. 检查类路径:确保logback-classic的jar包已经正确添加到项目的类路径中。可以通过查看项目的构建配置或IDE的项目设置来确认。 3. 检查日志配置文件:logback-classic需要一个配置文件来指定日志输出的方式和级别。确保logback.xml或logback.groovy配置文件存在,并且配置正确。可以参考logback官方文档来了解如何正确配置日志文件。 4. 检查其他日志框架的冲突:如果项目中同时使用了其他日志框架(如log4jjava.util.logging),可能会导致冲突。在这种情况下,可以尝试移除其他日志框架的依赖或者进行适当的配置。 5. 检查SLF4J的版本兼容性:确保使用的logback-classic版本与SLF4J的版本兼容。可以在SLF4J的官方网站上查找版本兼容性信息。 请注意,以上方法是常见的解决方案,但具体解决方法可能因项目配置和环境而异。如果问题仍然存在,建议查阅logback-classicSLF4J的官方文档,或者在相关的开发社区中寻求帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值