Spring5.x整合log4j2 解决Bug[log4j-slf4j-impl cannot be present with log4j-to-slf4j]

在Spring中对比Logback vs Log4j vs Log4j2

Logback:性能一般,支持Spring纯注解开发的日志输出
Log4j:性能良好,支持Spring纯注解开发的日志输出
Log4j2:性能极好,支持Spring纯注解开发的日志输出

由于Log4j2的优良性能以及极好的兼容性,所以项目开发中使用整合log4j2是完美的不二选择

遇到的问题

Log4j2整合的资料很少,几乎没有人详细解答如何整合Log4j2,本人也是折腾了一晚上才整合完毕测试成功

  • 日志不输出
  • Spring报错不兼容
    org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j

整合详细步骤

第一步:创建Maven工程

不要勾选这个
在这里插入图片描述

第二步:配置pom.xml依赖(最重要的一部)

除去Spring的jar包外,还要整合一下jar包,版本和必须一致,否则一定会有bug

<!--版本锁定开始-->
<slf4j.version>1.7.30</slf4j.version>
<log4j2.version>2.13.3</log4j2.version>
<!--版本锁定结束-->

<!--slf4j日志门面依赖开始-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
</dependency>
<!--slf4j日志门面依赖结束-->
<!--log4j2日志依赖开始-->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>${log4j2.version}</version>
</dependency>
<!--log4j2日志依赖结束-->
<!--!!!!!!!!!!!!!!!!!!!!!!!!这个是最重要的!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<dependency><!--提供商是log4j官方,这个是log4j和slf4j的桥接器,spring是通过slf4j输出日志的,依赖桥接器的实现-->
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>${log4j2.version}</version><!--版本这里尤为重要,必须与log4j的依赖版版本一致-->
</dependency>
<!--!!!!!!!!!!!!!!!!!!!!!!!!这个是最重要的!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->

第三步:配置log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
 <Configuration status="WARN">
   <Appenders>
     <Console name="Console" target="SYSTEM_OUT">
       <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
     </Console>
   </Appenders>
   <Loggers>
     <Root level="error">
       <AppenderRef ref="Console"/>
     </Root>
   </Loggers>
 </Configuration>

第四步:测试

public class TestAnnotation {
    @Test
    public void test1() {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        //ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
    }
}
2020-08-01 11:52:51.611 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@481a996b
2020-08-01 11:52:51.625 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2020-08-01 11:52:51.649 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2020-08-01 11:52:51.650 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2020-08-01 11:52:51.651 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2020-08-01 11:52:51.653 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2020-08-01 11:52:51.659 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'appConfig'

成功输出日志

# 完整工程下载地址
https://download.csdn.net/download/weixin_48568292/12678159

猜你喜欢

转载自blog.csdn.net/weixin_48568292/article/details/107728595