最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

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

来源:博客园


(资料图)

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

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.ExceptionInInitializerErrorat 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-slf4jat 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.(ParseNettyHttpServerLog.java:25)... 27 moreProcess 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依赖

            org.springframework.cloud            spring-cloud-gateway-server            test                                                log4j-to-slf4j                    org.apache.logging.log4j                                    

X 参考文献

  • log4j-slf4j-impl cannot be present with log4j-to-slf4j - CSDN

关键词: