1. 程式人生 > >Springboot中使用logback分類輸出日誌

Springboot中使用logback分類輸出日誌

<?xml version="1.0" encoding="UTF-8"?>  
<configuration>  
    <!-- 控制檯 appender -->  
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>  
            <charset>UTF-8</charset>
        </encoder>  
    </appender>  
    <!-- 出錯日誌 appender  -->  
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <!-- 按天回滾 daily -->  
            <!-- log.dir 在maven profile裡配置 -->  
            <fileNamePattern>./log/error/daotong-error-%d{yyyy-MM-dd}.log</fileNamePattern>  
            <!-- 日誌最大的歷史 60天 -->  
            <maxHistory>60</maxHistory>  
        </rollingPolicy>  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>  
            <charset>UTF-8</charset>
        </encoder>  
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印錯誤日誌 -->  
            <level>ERROR</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
    </appender>  
    <!-- 訪問日誌 appender  -->  
    <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <!-- 按天回滾 daily -->  
            <fileNamePattern>./log/warn/daotong-warn-%d{yyyy-MM-dd}.log</fileNamePattern>  
            <!-- 日誌最大的歷史 60天 -->  
            <maxHistory>60</maxHistory>  
        </rollingPolicy>  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>  
            <charset>UTF-8</charset>
        </encoder>  
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印WARN日誌 -->  
            <level>WARN</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
    </appender>  
    <!-- info日誌 appender  -->  
    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <!-- 按天回滾 daily -->  
            <fileNamePattern>./log/info/daotong-info-%d{yyyy-MM-dd}.log</fileNamePattern>  
            <!-- 日誌最大的歷史 60天 -->  
            <maxHistory>60</maxHistory>  
        </rollingPolicy>  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>  
            <charset>UTF-8</charset>
        </encoder>  
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印錯誤日誌 -->  
            <level>INFO</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
    </appender>  
    <!--日誌列印的包的範圍,及分類日誌檔案儲存 -->  
    <logger name="com.daotonginfo.manage" additivity="false">
        <level value="INFO" />  
        <appender-ref ref="STDOUT"/>  
        <appender-ref ref="ERROR"/>  
        <appender-ref ref="WARN"/>  
        <appender-ref ref="INFO" />  
    </logger>  
    <!--控制檯列印資源載入資訊-->  
    <root level="info">  
        <appender-ref ref="STDOUT" />  
    </root>  
</configuration>


連結:https://www.jianshu.com/p/184dff7dd9af
來源:簡書