logback-spring.xml日志配置文件详解

目录

  • 前言
  • logback-spring.xml 配置

前言

打印日志是一个系统的基本功能,系统出现异常可以通过查找日志弄清楚是什么原因,从而更加快速地定位问题,修复系统。

logback-spring.xml 配置

  1. 文件位置
    logback-spring.xml日志配置文件详解_第1张图片
  2. 具体配置

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <jmxConfigurator />
    
    <contextName>myAppNamecontextName>
    
    <property name="LOG_PATH" value="./logs"/>
    
    <property name="APPDIR" value="airport_admin"/>

    
    
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
                    
    
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    
    <appender name="FILEERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/${APPDIR}/log_error.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/${APPDIR}/error/%d{yyyyMMdd}/${APPDIR}-%d{yyyyMMdd}-%d{HH:mm:ss}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>2MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>30maxHistory>
            
            <cleanHistoryOnStart>truecleanHistoryOnStart>
        rollingPolicy>
        
        <append>trueappend>
        
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L [%thread] - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>errorlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="FILEWARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/${APPDIR}/log_warn.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/${APPDIR}/warn/%d{yyyyMMdd}/${APPDIR}-%d{yyyyMMdd}-%d{HH:mm:ss}.%i.logfileNamePattern>
           
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>2MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>30maxHistory>
            <cleanHistoryOnStart>truecleanHistoryOnStart>
        rollingPolicy>
        
        <append>trueappend>
        
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L [%thread] - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warnlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="FILEINFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/${APPDIR}/log_info.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           
            <fileNamePattern>${LOG_PATH}/${APPDIR}/info/%d{yyyyMMdd}/${APPDIR}-%d{yyyyMMdd-HH:mm:ss}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>30maxHistory>
            <cleanHistoryOnStart>truecleanHistoryOnStart>
        rollingPolicy>
        
        <append>trueappend>
        
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger [%thread] - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>infolevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debuglevel>
        filter>
    appender>

    
    <logger name="com.apache.ibatis" level="TRACE"/>
    <logger name="java.sql.Connection" level="DEBUG"/>
    <logger name="java.sql.Statement" level="DEBUG"/>
    <logger name="java.sql.PreparedStatement" level="DEBUG"/>


    
    

    
    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        
        <discardingThreshold>0discardingThreshold>
        
        <queueSize>256queueSize>
        
        <appender-ref ref="FILEINFO"/>
    appender>


    

    <springProfile name="dev">
        <root level="INFO">
            <appender-ref ref="FILEERROR" />
            <appender-ref ref="FILEWARN" />
            <appender-ref ref="ASYNC" />
            
            
        root>
    springProfile>
    <springProfile name="pro">
        <root level="INFO">
            <appender-ref ref="FILEERROR" />
            <appender-ref ref="FILEWARN" />
            <appender-ref ref="ASYNC" />
            
            
        root>
    springProfile>
configuration>

你可能感兴趣的:(SpringCloud,mybatis,JAVA,logback,spring,xml)