SpringBoot自定义logback-spring.xml模板








<configuration scan="true" scanPeriod="60 seconds" debug="false">

    
    <jmxConfigurator/>

    
    <springProperty name="log.level" source="logging.level.root" defaultValue="INFO"/>
    
    <springProperty name="log.appName" source="spring.application.name" defaultValue="SpringBoot"/>
    
    <springProperty name="log.homePath" source="logging.file.path" defaultValue="/tmp/app/${log.appName}/logs"/>
    
    <springProperty name="log.maxHistory" source="logging.file.max-size" defaultValue="30"/>
    
    <springProperty name="log.maxSize" source="logging.file.max-history" defaultValue="15MB"/>

    
    

    
    
    
    
    
    
    
    <property name="CONSOLE_LOG_PATTERN"
              value="%yellow(%date{yyyy-MM-dd HH:mm:ss}) |%highlight(%-5level) |%blue(%thread) |%blue(%file:%line) |%green(%logger) |%cyan(%msg%n)"/>


    <springProfile name="dev, test">
        
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>
                    
                    ${CONSOLE_LOG_PATTERN}
                pattern>
            encoder>
        appender>
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        
        
        <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFOlevel>
                <onMatch>ACCEPTonMatch>
                <onMismatch>DENYonMismatch>
            filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <fileNamePattern>${log.homePath}/%d{yyyy-MM-dd}/info/${log.appName}-%i.log
                fileNamePattern>
                
                <maxHistory>${log.maxHistory}maxHistory>
                
                <MaxFileSize>${log.maxSize}MaxFileSize>
            rollingPolicy>
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%npattern>
            encoder>
        appender>
    springProfile>

    
    
    <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>WARNlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <fileNamePattern>${log.homePath}/%d{yyyy-MM-dd}/warn/${log.appName}-%i.logfileNamePattern>
            
            <maxHistory>${log.maxHistory}maxHistory>
            
            <MaxFileSize>${log.maxSize}MaxFileSize>
        rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
    appender>

    
    
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            
            <fileNamePattern>
                ${log.homePath}/%d{yyyy-MM-dd}/error/${log.appName}-%i.log
            fileNamePattern>
            
            <maxHistory>${log.maxHistory}maxHistory>
            
            <MaxFileSize>${log.maxSize}MaxFileSize>
        rollingPolicy>
        <encoder>
            <pattern>
                
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            pattern>
        encoder>
    appender>

    
    <appender name="APP" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ALLlevel>
        filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.homePath}/%d{yyyy-MM-dd}/app/${log.appName}-%i.logfileNamePattern>
            <maxHistory>${log.maxHistory}maxHistory>
            <MaxFileSize>${log.maxSize}MaxFileSize>
        rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%npattern>
        encoder>
    appender>

    
    <logger name="org.springframework.web" additivity="false">
        <appender-ref ref="WARN"/>
        <appender-ref ref="APP"/>
        <appender-ref ref="ERROR"/>
    logger>

    
    
    
    
    
    
    


    <springProfile name="dev, test">
        <root>
            
            <appender-ref ref="CONSOLE"/>
            
            
            
            <appender-ref ref="INFO"/>
        root>
    springProfile>

    
    <root level="${log.level}">
        <appender-ref ref="WARN"/>
        <appender-ref ref="ERROR"/>
        <appender-ref ref="APP"/>
    root>

configuration>

你可能感兴趣的:(SpringBoot,spring,boot,spring,logback)