1. 程式人生 > >logback日誌配置及說明

logback日誌配置及說明

日誌常用到的jar包,給出maven依賴,單單是logback的話,新增第一個logback-classic這個就可以,剩餘幾個是其他日誌型別可能會用到的jar包,選擇新增即可:

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
         
         <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

 下面給出一個常用的配置:可以作為一個模板:

<?xml version="1.0" encoding="utf-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!--定義引數常量:下面會用到 -->
    <property name="log.level" value="debug"/> <!--日誌級別-->
    <property name="log.maxHistory" value="30"/> <!--最長儲存日期-->
    <property name="log.filePath" value="G:/JAVA_HOME/logger/schoolshop/logs/webapps"/> <!--日誌儲存路徑-->
    <!--日誌輸出格式: 日期 執行執行緒 (空5格)日誌級別 日誌位置 日誌內容 換行 -->
    <property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/>

    <!--控制檯設定 -->
    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <!--日誌格式-->
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
    </appender>

    <!--定義DEBUG -->
    <appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"><!--日誌追加-->
        <!--檔案路徑 -->
        <file>${log.filePath}/debug.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--檔名稱 -->
            <fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <!--檔案最大儲存天數 -->
            <maxHistory>${log.maxHistory}</maxHistory>
        </rollingPolicy>
        <!--日誌格式-->
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <!--過濾只接受DEBUG級別日誌-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!--INFO -->
    <appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--檔案路徑 -->
        <file>${log.filePath}/info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--檔名稱 -->
            <fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <!--檔案最大儲存歷史時間 -->
            <maxHistory>${log.maxHistory}</maxHistory>
        </rollingPolicy>
        <!--日誌格式-->
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <!--過濾只接受INFO級別日誌-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!--ERROR -->
    <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--檔案路徑 -->
        <file>${log.filePath}/error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--檔名稱 -->
            <fileNamePattern>${log.filePath}/error/error.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <!--檔案最大儲存歷史數量 -->
            <maxHistory>${log.maxHistory}</maxHistory>
        </rollingPolicy>
        <!--日誌格式-->
        <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
        <!--過濾只接受ERROR級別日誌-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <logger name="com.scnu.o2o" level="${log.level}" additivity="true">
        <!--additivity="true" 註解作用:將root中的appender-ref 自動新增至該專案下,因此自己記錄的日誌也會列印輸出在控制檯-->
        <appender-ref ref="debugAppender"/>
        <appender-ref ref="infoAppender"/>
        <appender-ref ref="errorAppender"/>
    </logger>

    <root level="info">
        <appender-ref ref="consoleAppender"/>
    </root>

</configuration>

注意最後logger和root節點,以及additivity屬性。

推薦兩篇關於logback的配置部落格: