Springboot整合log4j2-问题与配置

问题总结 :
1.Springboot 自身拥有日志记录jar : spring-boot-starter-logging ,首先排除loggin jar包;
2.启动报错说明

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/tony/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/tony/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.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 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
12:02:26.963 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
12:02:26.969 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
12:02:26.969 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/F:/work_intel/code/demo1/target/classes/]
Logging system failed to initialize using configuration from 'src/main/resources/log4j.properties'

**
报错说明:这是因为与logback的冲突并未解决,导致启动失败,根据上面的报错信息,找到logback-classic-1.2.3.jar和slf4j-log4j12-1.7.25.jar的相应位置删除,并在pom.xml中加入如下排除代码:

     
            org.springframework.boot
            spring-boot-starter-web
            
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
                
                    org.slf4j
                    slf4j-log4j12
                
            
        

3.mybatis 与 log4j2 日志冲突,特此记录,加入以下jar包 ,会2.出错误
还未找到解决方案 ,应该与logback有关

org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0

整体框架配置如下 :

1.pom.xml配置

**



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.icbc
    spring-shiro
    0.0.1-SNAPSHOT
    spring-shiro
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        


        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            mysql
            mysql-connector-java
            5.1.4
        
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
        
            com.alibaba
            fastjson
            1.2.8
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.4
        
        
        
            org.projectlombok
            lombok
            1.16.10
        
        
        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
            
        
        
            
                src/main/java
                
                
                    **/*.xml
                
            
        
    



2.log4j2配置文件,跟据不同级别错误生成日志文件






    
        
        
        
         D://log4j2Logs

        
        %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %l - %m%n
        
        %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %C.%M - %m%n

        
        20MB
        
        DEBUG

        
        D://all.log
        
        ${basePath}/%d{yyyy-MM}/all-%d{yyyy-MM-dd}-%i.log.gz
        
        50

        
        D://info.log
        
        D://%d{yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz
        
        10

        
        D://warn.log
        
        D://%d{yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz
        
        10

        
        D://error.log
        
        D://%d{yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz
        
        10

        
        DEBUG

    

    
    
        
        
            
            
            
            
        

        
        
            
            
            
            
            
            
                
            
        

        
        
            
            
            
            
            
                
                
            
        

        
        
            
            
            
            
            
                
                
            
        

        
        
            
            
            
            
            
                
                
            
        
    

    
    
        
        
            
        
        
        
            
            
            
            
            
        
    

3.application.properties 中配置指向

logging.config= classpath:log4j2.xml

你可能感兴趣的:(JAVA)