logback.xml

這裏在網上找了幾個模塊,學習完前面幾個博客後我已經能看懂了,之後項目中用到,根據需求改動就ok了,省得再百度。java

簡單點的redis

<?xml version="1.0" encoding="UTF-8"?>  
<!-- Logback Configuration. -->  
<configuration debug="true">  
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
        <target>System.out</target>  
        <encoding>UTF-8</encoding>  
        <filter class="com.alibaba.citrus.logconfig.logback.LevelRangeFilter">  
            <levelMax>INFO</levelMax>  
        </filter>  
        <layout class="ch.qos.logback.classic.PatternLayout">  
            <pattern><![CDATA[ 
             [%d{yyyy-MM-dd HH:mm:ss}]  %-5level %logger{0} - %m%n 
            ]]></pattern>  
        </layout>  
    </appender>  
  
    <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">  
        <target>System.err</target>  
        <encoding>UTF-8</encoding>  
        <filter class="com.alibaba.citrus.logconfig.logback.LevelRangeFilter">  
            <levelMin>WARN</levelMin>  
        </filter>  
        <layout class="ch.qos.logback.classic.PatternLayout">  
            <pattern><![CDATA[ 
[%d{yyyy-MM-dd HH:mm:ss} ] - %X{method} %X{requestURIWithQueryString} [ip=%X{remoteAddr}, ref=%X{referrer}, ua=%X{userAgent}, sid=%X{cookie.JSESSIONID}]%n  %-5level %logger{35} - %m%n 
            ]]></pattern>  
        </layout>  
    </appender>  
  
    <!-- 生成日誌文件 -->  
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <!--日誌文件輸出的文件名 -->  
        <file>${app.output}/logs/app.log</file>  
  
        <!-- 固定數量的日誌文件,防止將磁盤佔滿 -->  
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">  
            <fileNamePattern>${app.output}/logs/app.%i.log  
            </fileNamePattern>  
            <minIndex>1</minIndex>  
            <maxIndex>10</maxIndex>  
        </rollingPolicy>  
  
        <!--日誌文件最大的大小 -->  
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">  
            <MaxFileSize>500MB</MaxFileSize>  
        </triggeringPolicy>  
  
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
            <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符 -->  
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level %logger{20} -%msg%n</pattern>  
        </encoder>  
    </appender>  
  
    <appender name="thirdApiCallAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <!--日誌文件輸出的文件名 -->  
        <file>${app.output}/logs/third_api_call.log</file>  
  
        <!-- 固定數量的日誌文件,防止將磁盤佔滿 -->  
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">  
            <fileNamePattern>${app.output}/logs/third_api_call.%i.log  
            </fileNamePattern>  
            <minIndex>1</minIndex>  
            <maxIndex>10</maxIndex>  
        </rollingPolicy>  
  
        <!--日誌文件最大的大小 -->  
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">  
            <MaxFileSize>500MB</MaxFileSize>  
        </triggeringPolicy>  
  
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
            <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符 -->  
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level %logger{20} -%msg%n</pattern>  
        </encoder>  
    </appender>  
  
    <!-- redis日誌 -->  
    <appender name="redisAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <file>${app.output}/logs/redis.log</file>  
  
        <!-- 固定數量的日誌文件,防止將磁盤佔滿 -->  
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">  
            <fileNamePattern>${app.output}/logs/redis.%i.log</fileNamePattern>  
            <minIndex>1</minIndex>  
            <maxIndex>10</maxIndex>  
        </rollingPolicy>  
  
        <!--日誌文件最大的大小 -->  
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">  
            <MaxFileSize>100MB</MaxFileSize>  
        </triggeringPolicy>  
  
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
            <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符 -->  
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level %logger{20} -%msg%n</pattern>  
        </encoder>  
    </appender>  
  
    <logger name="profiler" level="DEBUG" additivity="false">  
        <appender-ref ref="profilerAppender"/>  
        <appender-ref ref="STDOUT"/>  
    </logger>  
  
    <logger name="thirdApiCall" level="DEBUG" additivity="false">  
        <appender-ref ref="thirdApiCallAppender"/>  
        <appender-ref ref="STDOUT"/>  
    </logger>  
      
    <logger name="redisLogger" level="DEBUG" additivity="false">  
        <appender-ref ref="redisAppender"/>  
        <appender-ref ref="STDOUT"/>  
    </logger>  
  
    <root>  
        <level value="${app.logging.level}" />  
        <appender-ref ref="STDOUT" />  
        <appender-ref ref="STDERR" />  
        <appender-ref ref="FILE" />  
    </root>  
</configuration>

複雜點的:spring

<?xml version="1.0" encoding="UTF-8"?>
<!-- 說明: 一、日誌級別及文件 日誌記錄採用分級記錄,級別與日誌文件名相對應,不一樣級別的日誌信息記錄到不一樣的日誌文件中 例如:error級別記錄到log_error_xxx.log或log_error.log(該文件爲當前記錄的日誌文件),而log_error_xxx.log爲歸檔日誌, 
    日誌文件按日期記錄,同一天內,若日誌文件大小等於或大於2M,則按0、一、2...順序分別命名 例如log-level-2013-12-21.0.log 
    其它級別的日誌也是如此。 二、文件路徑 若開發、測試用,在Eclipse中運行項目,則到Eclipse的安裝路徑查找logs文件夾,以相對路徑../logs。 
    若部署到Tomcat下,則在Tomcat下的logs文件中 三、Appender FILEERROR對應error級別,文件名以log-error-xxx.log形式命名 
    FILEWARN對應warn級別,文件名以log-warn-xxx.log形式命名 FILEINFO對應info級別,文件名以log-info-xxx.log形式命名 
    FILEDEBUG對應debug級別,文件名以log-debug-xxx.log形式命名 stdout將日誌信息輸出到控制上,爲方便開發測試使用 -->
<configuration>

    <!-- 在Eclipse中運行,請到Eclipse的安裝目錄中找log文件,Tomcat下,請到Tomcat目錄下找 -->
    <!-- <property name="LOG_PATH" value="/xebest/logs/llmj-app" /> -->
    <property name="LOG_PATH" value="E:/logs/llmj-app" />
    <!-- 日誌記錄器,日期滾動記錄 -->
    <appender name="FILEERROR"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 正在記錄的日誌文件的路徑及文件名 -->
        <file>${LOG_PATH}/log_error.log</file>
        <!-- 日誌記錄器的滾動策略,按日期,按大小記錄 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 歸檔的日誌文件的路徑,例現在天是2013-12-21日誌,當前寫的日誌文件路徑爲file節點指定,能夠將此文件與file指定文件路徑設置爲不一樣路徑,從而將當前日誌文件或歸檔日誌文件置不一樣的目錄。 
                而2013-12-21的日誌文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 -->
            <fileNamePattern>${LOG_PATH}/log-error-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <!-- 除按日誌記錄以外,還配置了日誌文件不能超過2M,若超過2M,日誌文件會以索引0開始, 命名日誌文件,例如log-error-2013-12-21.0.log -->
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>2MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <!-- 追加方式記錄日誌 -->
        <append>true</append>
        <!-- 日誌文件的格式 -->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-5p [%d][%mdc{mdc_userId}] %C:%L - %m %n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!-- 此日誌文件只記錄error級別的 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>error</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="FILEWARN"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/log_warn.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/log-warn-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>2MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <append>true</append>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-5p [%d][%mdc{mdc_userId}] %C:%L - %m %n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!-- 此日誌文件只記錄warn級別,不記錄大於warn級別的日誌 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="FILEINFO"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/log_info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/log-info-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>2MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <append>true</append>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-5p [%d][%mdc{mdc_userId}] %C:%L - %m %n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!-- 此日誌文件只記錄info級別,不記錄大於info級別的日誌 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>


    <appender name="FILEDEBUG"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/log_debug.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/log-debug-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>2MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <append>true</append>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-5p [%d][%mdc{mdc_userId}] %C:%L - %m %n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!-- 此日誌文件只記錄debug級別,不記錄大於debug級別的日誌 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <pattern>%-5p [%d][%mdc{mdc_userId}] %C:%L - %m %n</pattern>
            <charset>utf-8</charset>
        </encoder>
        <!-- 此日誌appender是爲開發使用,只配置最底級別,控制檯輸出的日誌級別是大於或等於此級別的日誌信息 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
    </appender>

    <appender name="FILTER_INFO"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_PATH}/log_filter.log</File>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} : %m%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>${LOG_PATH}/log_filter.%d{yyyy-MM-dd}</fileNamePattern>  
        </rollingPolicy>  
    </appender>
    
     <logger name="FILTER_INFO_LOGGER" additivity="false" level="INFO"> 
         <appender-ref ref="FILTER_INFO" /> 
     </logger>


    <appender name="INTEREST_BEARING_INFO"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_PATH}/log_interest_bearing.log</File>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} : %m%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>${LOG_PATH}/log_interest_bearing.%d{yyyy-MM-dd}</fileNamePattern>  
        </rollingPolicy>  
    </appender>
    
     <logger name="INTEREST_BEARING_LOGGER" additivity="true" level="INFO"> 
         <appender-ref ref="INTEREST_BEARING_INFO" /> 
     </logger>


    <!-- 爲單獨的包配置日誌級別,若root的級別大於此級別, 此處級別也會輸出 應用場景:生產環境通常不會將日誌級別設置爲trace或debug,可是爲詳細的記錄SQL語句的狀況, 
        可將hibernate的級別設置爲debug,如此一來,日誌文件中就會出現hibernate的debug級別日誌, 而其它包則會按root的級別輸出日誌 -->
    <logger name="org.springframework" level="DEBUG" />
    <logger name="com.ibatis" level="DEBUG" />  
    <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" />  
    <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" />  
    <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" />  
    <logger name="java.sql.Connection" level="DEBUG" />  
    <logger name="java.sql.Statement" level="DEBUG" />  
    <logger name="java.sql.PreparedStatement" level="DEBUG" />  


    <!-- 生產環境,將此級別配置爲適合的級別,以名日誌文件太多或影響程序性能 -->
    <root level="INFO">
        <appender-ref ref="FILEDEBUG" />
        <appender-ref ref="FILEINFO" />
        <appender-ref ref="FILEWARN" />
        <appender-ref ref="FILEERROR" />
        <!-- 生產環境將請stdout去掉 -->
         <appender-ref ref="stdout" /> 
    </root>
</configuration>
相關文章
相關標籤/搜索