Spring boot中用logback实现日志管理

2021-05-14 12:30

阅读:326

标签:log文件   cer   you   can   pattern   日志管理   har   加载   权限   

1.导入jar包

dependency>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-loggingartifactId>
    dependency>

2.创建logback-spring.xml配置文件

xml version="1.0" encoding="UTF-8"?>

configuration scan="true" scanPeriod="60 seconds" debug="false">
    contextName>zlyPay-logcontextName>

    
    property name="log.path" value="D:\\log\\" />
    property name="log.file" value="D:\\log\\logback.log" />

    
    property name="log.path.error" value="D:\\log\\error\\" />
    property name="log.file.error" value="D:\\log\\error\\logback-error.log" />
 
    property name="log.path.warn" value="D:\\log\\warn\\" />
    property name="log.file.warn" value="D:\\log\\warn\\logback-error.log" />

    include resource="org/springframework/boot/logging/logback/base.xml"/>

    
    appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        file>${log.file}file>
        
        rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            fileNamePattern>${log.path}zlyPay-log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                maxFileSize>512MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            maxHistory>30maxHistory>
        rollingPolicy>
        append>trueappend>
        encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%npattern>
            charset>utf-8charset>
        encoder>
        
    appender>

    
    appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        file>${log.file.error}file>
        
        rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            fileNamePattern>${log.path.error}zlyPay-log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                maxFileSize>128MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            maxHistory>180maxHistory>
        rollingPolicy>
        append>trueappend>
        
        encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%npattern>
            charset>utf-8charset>
        encoder>
        
        filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            level>ERRORlevel>
            
            onMatch>ACCEPTonMatch>
            
            onMismatch>DENYonMismatch>
        filter>
    appender>

    
    appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        
        encoder>
            pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%npattern>
            charset>utf-8charset>
        encoder>
        
        filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            
            level>DEBUGlevel>
        filter>
    appender>
    
appender name="STDOUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
     
      file>${log.file.warn}file>
        rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            fileNamePattern>${log.path.warn}zlyPay-log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                maxFileSize>128MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            maxHistory>180maxHistory>
        rollingPolicy>
        append>trueappend>
        
    
    encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        
        pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %X{QTRACER} %X{MDCContextId}-%X{MDCSupplyService}-%X{MDCSupplyRetryTime} %-5level %logger{0} - %msg%npattern>
    encoder>
    filter class="ch.qos.logback.classic.filter.LevelFilter">
        level>warnlevel>
        onMatch>ACCEPTonMatch>
     onMismatch>DENYonMismatch>
    filter>
appender>
     
    logger name="org.jcut.dao" level="DEBUG">logger>

    
    logger name="com.cltx"  level="INFO"  additivity="false">
        appender-ref ref="STDOUT"/>
        appender-ref ref="INFO_FILE"/>
        appender-ref ref="ERROR_FILE"/>
    logger>

    root level="INFO">
        appender-ref ref="STDOUT" />
        appender-ref ref="INFO_FILE" />
        appender-ref ref="ERROR_FILE" />
    root> 

configuration>

3.application.properties文件中加入日志文件路径

#加载指定的日志配置文件
logging.config=classpath:logback-spring.xml
#调试环境用DEBUG,生产环境用info
logging.level.root=DEBUG

 

Spring boot中用logback实现日志管理

标签:log文件   cer   you   can   pattern   日志管理   har   加载   权限   

原文地址:https://www.cnblogs.com/huqi96/p/13124225.html


评论


亲,登录后才可以留言!