首页 > 编程语言 >[日志管理] 启动程序时,因报“log4j-slf4j-impl cannot be present with log4j-to-slf4j”错误而导致程序终止运行[转发]

[日志管理] 启动程序时,因报“log4j-slf4j-impl cannot be present with log4j-to-slf4j”错误而导致程序终止运行[转发]

时间:2023-06-01 14:25:34浏览次数:50  
标签:java 程序 log4j slf4j org junit runners

此错误出现过了几次了,有必要记录一下。

1 问题描述

运行测试用例的spring-boot Java程序片段时,报如下错误:


SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/Program_Data/maven_repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.3/log4j-slf4j-impl-2.13.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/Program_Data/maven_repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

java.lang.ExceptionInInitializerError
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:250)
	at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:260)
	at org.junit.runners.BlockJUnit4ClassRunner$2.runReflectiveCall(BlockJUnit4ClassRunner.java:309)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
	at org.apache.logging.slf4j.Log4jLoggerFactory.validateContext(Log4jLoggerFactory.java:49)
	at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:39)
	at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:30)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:54)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
	at bugs.debug.ParseNettyHttpServerLog.<clinit>(ParseNettyHttpServerLog.java:25)
	... 27 more


Process finished with exit code -1

2 问题分析

  • 错误日志翻译:Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
log4j-slf4j-impl 和 log4j-to-slf4j 这两个包不能同时存在。
  • 那么为什么这两个包不能同时存在呢?

我们先来看一下这两个包的定义:

  • log4j-slf4j-impl : 主要是 log4j 对 slf4j 接口的实现

Apache Log4j SLF4J Binding: The Apache Log4j SLF4J API binding to Log4j 2 Core

  • log4j-to-slf4j : slf4j 对 log4j 接口的适配

Apache Log4j to SLF4J Adapter: The Apache Log4j binding between Log4j 2 API and SLF4J

这两个接口不能同时存在的意思是说:

要么用 log4j 日志系统,然后同时支持 slf4j 接口的调用;
要么用其他日志系统,比如logback(logback是 slf4j 接口的实现),然后适配log4j接口。不能兼而有之!

  • spring boot 框架默认使用的logback日志系统,logback实现的是slf4j接口。因此需要适配 log4j 接口,因而会引入 log4j-to-slf4j

因此,如果您正在使用的是spring boot,则不能引入log4j-slf4j-impl。
问题解决思路:只需要看一下是哪个 pom 文件引入了 log4j-slf4j-impl,把这个包去掉就可以啦!

3 解决方法

  • 本问题中,博主的工程确实是基于spring-boot框架,但博主采纳并使用的日志框架是slf4j + log4j

即:需去除spring-boot自带的log4j-to-slf4j依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gateway-server</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-to-slf4j</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

X 参考文献

标签:java,程序,log4j,slf4j,org,junit,runners
From: https://www.cnblogs.com/johnnyzen/p/17448771.html

相关文章

  • 直播小程序源码,flutter TextField 限制输入长度,限制输入数字文字
    直播小程序源码,flutterTextField限制输入长度,限制输入数字文字//限制长度inputFormatters:[LengthLimitingTextInputFormatter(11)], //限制输入数字文字等类型inputFormatters:[WhitelistingTextInputFormatter.digitsOnly], //键盘类型keyboardType:TextInputType.tex......
  • C# WINFORM 打砖块游戏,可以进行两队PK 程序源码
    C#WINFORM打砖块游戏红色绿色阵营,球可以自定义添加,图片可以设置为网络地址的头像,可以进行与评论和弹幕进行建设。 支持球增加支持球加速支持一键初始化游戏支持pk支持积分累计程序代码还有完全开发完毕,有些小细节还需要再进行优化程序使用纯原生代码,没有使用任何第三......
  • 把任意程序注册成windows服务, 并设置开机自动启动
    背景最近有个需求让我把原本部署在linux系统上的数据库和对数据库进行CRUD的java程序部署到windows系统上来.问题mysql和tdengine这两个数据库有windows版本,直接安装就行了,并且也已经自动注册成服务,可以自己设置开机自动启动(控制面板->本地服务)但是kafka和zookeepe......
  • Flask-----轻量级的框架,快速的搭建程序(python)
     Flask是一个基于Python开发并且依赖jinja2模板和WerkzeugWSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后触发Flask框架,开发人员基于Flask框架提供的功能对请求进行相应的处理,并返回给用户,如果要返回给用户复杂的内容时,需要借......
  • IDEA中slf4j和logback冲突,快速排除(LoggerFactory is not a Logback LoggerContext but
    pom文件中右击  ctrl+f输入点击定位 选中shift+delet,直接排除  或者手动输入排除 ......
  • 微信小程序upload多张图片同时上传
    //上传图片,选择图片chooseImage:function(e){varthat=this;wx.chooseImage({sizeType:['original','compressed'],//可以指定是原图还是压缩图,默认二者都有sourceType:['album','camera'],//可以指定来源是相册还是相机......
  • Ubuntu 20.04.2 设置程序开机自启动
    Ubuntu20.04.2设置程序开机自启动Ubuntu20.04.2设置程序开机自启动关于Jar包设置开机自启动乌班图20放置开机启动文件的目录改变了首先是进入放置起动脚本的目录cd/etc/profile.droot@dell3640:/home/uadmin/huimv.hy#cd/etc/profile.droot@dell3640:/etc/profile.d......
  • [GPT4]作为一个大龄程序员,如何提升自己未来的竞争力?
    作为一个大龄程序员,提升自己的竞争力至关重要。以下是一些建议,可以帮助你在职场上保持竞争力和不断壮大技能库:持续学习学习新技术:紧跟技术趋势,如人工智能、大数据、区块链等领域。获得相关证书:如AWS、Azure、GoogleCloud等云服务提供商的认证或者PMP等项目管理认证。观看......
  • 如何使用Next.js创建全栈应用程序
    Next.js乍一看似乎令人生畏,因为有这么多新概念需要掌握。但别担心——在这个循序渐进的教程中,我将为您提供使用Next.js创建您的第一个现代全栈应用程序所需的所有基本信息。在本教程中,我将带您了解Next.js的基础知识,并指导您创建您的第一个全栈应用程序。在本教程结束时,您将......
  • 启动HBase时提示SLF4J: Class path contains multiple SLF4J bindings的解决方法
    启动hbase报错:“SLF4J:ClasspathcontainsmultipleSLF4Jbindings.”解决方法cd/home/opt/hbase-2.2.3/lib/client-facing-thirdpartymvslf4j-log4j12-1.7.25.jarslf4j-log4j12-1.7.25-copy修改了hbase中的文件名,保留了hadoop的,这个会有问题,一个当启动hbase的reg会报错这......