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工程

不要勾选这个
Spring5.x整合log4j2 解决Bug[log4j-slf4j-impl cannot be present with log4j-to-slf4j]_第1张图片

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

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


<slf4j.version>1.7.30slf4j.version>
<log4j2.version>2.13.3log4j2.version>



<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>${slf4j.version}version>
dependency>


<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-coreartifactId>
    <version>${log4j2.version}version>
dependency>
<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-apiartifactId>
    <version>${log4j2.version}version>
dependency>
<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-webartifactId>
    <version>${log4j2.version}version>
dependency>


<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-slf4j-implartifactId>
    <version>${log4j2.version}version>
dependency>

第三步:配置log4j.xml


 <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'

Spring5.x整合log4j2 解决Bug[log4j-slf4j-impl cannot be present with log4j-to-slf4j]_第2张图片

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

你可能感兴趣的:(JavaWeb,java,spring,spring,boot,mybatis,maven)