1. 程式人生 > >logback.xml 配置

logback.xml 配置

 需要引入 logback-core-1.1.2.jar

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <!--定義日誌檔案的儲存地址 勿在 LogBack 的配置中使用相對路徑 -->
    <property name="LOG_HOME" value="log_addr" />
    <property scope="context" name="FILE_NAME" value="app_runtime" />
    <timestamp key="byDay" datePattern="yyyy-MM-dd" />
    <!-- 控制檯輸出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- 過濾掉 TRACE 級別的日誌 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化輸出,%d:日期;%thread:執行緒名;%-5level:級別,從左顯示5個字元寬度;%msg:日誌訊息;%n:換行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <appender name="gabAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 過濾掉 TRACE 和 DEBUG 級別的日誌-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
	    </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日誌檔案輸出的檔名 -->
            <FileNamePattern>c:/${LOG_HOME}/${byDay}/gab-%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--日誌檔案保留天數 -->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化輸出,%d:日期;%thread:執行緒名;%-5level:級別,從左顯示5個字元寬度;%msg:日誌訊息;%n:換行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <!--日誌檔案最大的大小 -->
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <appender name="gabTaskAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日誌檔案輸出的檔名 -->
            <FileNamePattern>c:/${LOG_HOME}/${byDay}/gab-task-%d{yyyy-MM-dd}.log</FileNamePattern>
            <!--日誌檔案保留天數 -->
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化輸出,%d:日期;%thread:執行緒名;%-5level:級別,從左顯示5個字元寬度;%msg:日誌訊息;%n:換行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <!--日誌檔案最大的大小 -->
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>
	<!--錯誤日誌統一輸出到這裡 -->
	<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
		  <level>DEBUG</level>
		</filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日誌檔案輸出的檔名 -->
			<FileNamePattern>c:/${LOG_HOME}/${byDay}/error_%d{yyyy-MM-dd}.log
			</FileNamePattern>
			 <!--日誌檔案保留天數 -->
			<MaxHistory>30</MaxHistory>
		</rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示執行緒名,%-5level:級別從左顯示5個字元寬度%msg:日誌訊息,%n是換行符 -->
			  <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
		</encoder>
		 <!--日誌檔案最大的大小 -->
		<triggeringPolicy
			class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
			<MaxFileSize>10MB</MaxFileSize>
		</triggeringPolicy>
	</appender>
    <!--Mybatis log configure -->
    <logger name="java.sql" level="ERROR" additivity="false" >
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="error"/>
    </logger>
    <logger name="org.apache.ibatis" level="INFO" additivity="false">
        <appender-ref ref="STDOUT"/> 
        <appender-ref ref="error"/>
    </logger>
    <logger name="org.apache.commons" level="INFO" additivity="false">
        <appender-ref ref="STDOUT"/>
         <appender-ref ref="error"/>
    </logger>
    <logger name="org.apache.shiro" level="INFO" additivity="false">
        <appender-ref ref="STDOUT"/>
         <appender-ref ref="error"/>
    </logger>
    <logger name="org.mybatis.spring" level="WARN" additivity="false">
        <appender-ref ref="STDOUT"/>
         <appender-ref ref="error"/>
    </logger>
    <logger name="org.springframework" level="WARN" additivity="false">
        <appender-ref ref="STDOUT"/> 
        <appender-ref ref="error"/>
    </logger>
    <logger name="net.sf.ehcache" level="WARN" additivity="false">
        <appender-ref ref="STDOUT"/>
         <appender-ref ref="error"/>
    </logger>

    <!-- root logger -->
    <root level="debug">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="error" />
    </root>
</configuration>