异常信息:
Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
简要说明:
org.apache.logging.log4j.LoggingException: log4j-slf4j-impl不能与log4j-to-slf4j一起出现。
原因分析:
(1) 桥接包:log4j-slf4j-impl
桥接包log4j-slf4j-impl起到适配的作用,因为市面上的日志实现互不兼容,日志框架slf4j要想适用于日志实现log4j2,就需要使用桥接包。
slf4j使用LoggerFactory创建Logger进行日志打印,底层实际上调用了log4j-slf4j-impl的StaticLoggerBinder类创建一个Log4jLoggerFactory,然后再由这个Log4jLoggerFactory创建一个Log4j2的Logger对象,这个Logger封装在log4j-slf4j-impl中的Log4jLogger里面,最后将Log4jLogger返回给slf4j,每次slf4j进行日志打印,实际上是log4j-slf4j-impl中的Log4jLogger调用log4j2进行日志打印。
如果没有 log4j-slf4j-impl桥接包,slf4j将创建一个对象,里面都是空方法,所以不会打印出日志。
(2) SLF4J适配器:log4j-to-slf4j
log4j-to-slf4j模块的目的是将对log4j日志记录器的调用重定向到SLF4J。
Log4j to SLF4J Adapter
The Log4j 2 to SLF4J Adapter allows applications coded to the Log4j 2 API to be routed to SLF4J. Use of this adapter may cause some loss of performance as the Log4j 2 Messages must be formatted before they can be passed to SLF4J. With Log4j 2 as the implementation these would normally be formatted only when they are accessed by a Filter or Appender.
解决措施:
排除掉重复的jar包,比如下面通过exclusions元素,排除其中重复的log4j-slf4j-impl依赖包。
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version>
<exclusions>
<exclusion>
<artifactId>log4j-slf4j-impl</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
访问Spring Cloud技术专栏,了解更多的技术细节和项目代码。
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!