log4j,log4j2,logback日志框架的使用

SpringBoot环境下使用

目标:
以debug,info,error三个日志级别为例,控制台能打印这三个日志级别,同时将不同的级别输出到不同的日志文件

log4j

  • Maven依赖
 
        
            org.springframework.boot
            spring-boot-starter-web
            2.1.3.RELEASE
            
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
        
            org.springframework.boot
            spring-boot-starter-log4j
            1.3.8.RELEASE
        
  
  • log4j.properties
# LOG4J配置
#注意:DEBUG控制全局的日志级别
log4j.rootCategory=DEBUG, console, infoFile, errorFile ,debugFile
LOG_PATH=E:/logs/learn-log/log4j
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss,SSS}]-[%p]:%m %x %n
# 控制台输出
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =${LOG_PATTERN}

##debug以上级别的日志会进入改文件
#log4j.appender.debugFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugFile =com.hx.learn.log.log4j.config.AppenderConfig
log4j.appender.debugFile.Threshold = DEBUG
log4j.appender.debugFile.File = ${LOG_PATH}/debug.log
log4j.appender.debugFile.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.debugFile.Append=true
log4j.appender.debugFile.layout = org.apache.log4j.PatternLayout
log4j.appender.debugFile.layout.ConversionPattern =${LOG_PATTERN}

##info以上级别的日志会进入改文件
#log4j.appender.infoFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.infoFile =com.hx.learn.log.log4j.config.AppenderConfig
log4j.appender.infoFile.Threshold = INFO
log4j.appender.infoFile.File = ${LOG_PATH}/log.log
log4j.appender.infoFile.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.infoFile.Append=true
log4j.appender.infoFile.layout = org.apache.log4j.PatternLayout
log4j.appender.infoFile.layout.ConversionPattern =${LOG_PATTERN}
## error以上级别的日志会进入改文件
#log4j.appender.errorFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorFile =com.hx.learn.log.log4j.config.AppenderConfig
log4j.appender.errorFile.Threshold = ERROR
log4j.appender.errorFile.File = ${LOG_PATH}/error.log
log4j.appender.errorFile.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.errorFile.Append=true
log4j.appender.errorFile.layout = org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern =${LOG_PATTERN}

log4j2

  • Maven依赖
    
        
            org.springframework.boot
            spring-boot-starter-web
            2.1.3.RELEASE
            
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
        
            org.springframework.boot
            spring-boot-starter-log4j2
            2.1.3.RELEASE
        
    
  • log4j.xml




    
        
        
        
        
        E:/logs/learn-log/log4j2
        
    
    
        
        
            
            
            
        

        
        
            
           
            
                
                
            
            
            
                
                
                
            
            
            
        

        
        
            
            
            
                
                
                
            
            
            
                
                
                
            
            
            
        

        
        
            
            
            
            
                
                
                
            
            
            
        

    

    
        
        
            
            
            
            
        
    

log4back

  • Maven依赖
    
        
            org.springframework.boot
            spring-boot-starter-web
            2.1.3.RELEASE
        
    
  • logback.xml


    logback

    
    
    
    
    

    
        
            debug
        
        
            [%date] %level [%thread] %logger{36} [%file : %line] %msg%n
            
        
    

    
        ${path}/debug.log
        
            
            ${path}/debug.log.%d{yyyy-MM-dd}-%i.zip
            ${maxHistory}
            
                ${maxFileSize}
            
        
        
            [%date] %level [%thread] %logger{36} [%file : %line] %msg%n
            
        
        
            DEBUG
            ACCEPT
            DENY
        
    

    
        ${path}/info.log
        
            
            ${path}/info.log.%d{yyyy-MM-dd}-%i.zip
            ${maxHistory}
            
                ${maxFileSize}
            
        
        
            [%date] %level [%thread] %logger{36} [%file : %line] %msg%n
            
        
        
            INFO
            ACCEPT
            DENY
        
    

    
        ${path}/warn.log
        
            
            ${path}/warn.log.%d{yyyy-MM-dd}-%i.zip
            ${maxHistory}
            
                ${maxFileSize}
            
        
        
            [%date] %level [%thread] %logger{36} [%file : %line] %msg%n
            
        
        
            WARN
            ACCEPT
            DENY
        
    


    
        ${path}/error.log
        
            
            ${path}/error.log.%d{yyyy-MM-dd}-%i.zip
            ${maxHistory}
            
                ${maxFileSize}
            
        
        
            [%date] %level [%thread] %logger{36} [%file : %line] %msg%n
            
        
        
            ERROR
            ACCEPT
            DENY
        
    

    
        
        
        
        
        
        
    



你可能感兴趣的:(log4j,log4j2,logback日志框架的使用)