SylvanasSun
7/16/2017 - 6:11 AM

The log4j xml config

The log4j xml config

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- 输出日志到控制台  ConsoleAppender -->
    <appender name="console"
              class="org.apache.log4j.ConsoleAppender">
        <param name="encoding" value="UTF-8"/>
        <layout class="org.apache.log4j.TTCCLayout">
            <param name="ConversionPattern" value="TTCCLayout"/>
        </layout>
        <!--filter过滤器设置输出的级别:ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
                    所有下面输出的是debug到warn不会有error和fatal -->
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="levelMin" value="info"/>
            <param name="levelMax" value="warn"/>
            <param name="AcceptOnMatch" value="true"/>
        </filter>
    </appender>

    <!-- 输出日志到文件  每天一个文件 -->
    <appender name="dailyRollingFile"
              class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="${webApp.root}/logs/dailyRollingFile.log"/>
        <param name="Append" value="true"/>  <!-- 是否项目重启继续保存之前日志 -->
        <param name="encoding" value="UTF-8"/>
        <param name="threshold" value="info"/>
        <param name="ImmediateFlush" value="true"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd'.log'"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] {%c:%L}-%m%n"/>
        </layout>
    </appender>

    <!-- 输出日志到文件  文件大小到达指定尺寸的时候产生一个新的文件 -->
    <appender name="DEBUG-APPENDER"
              class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="${webApp.root}/logs/debug-log.log"/>
        <param name="Append" value="true"/>
        <param name="encoding" value="UTF-8"/>
        <param name="ImmediateFlush" value="true"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd'.log'"/>
        <param name="MaxFileSize" value="30KB"/>
        <param name="MaxBackupIndex" value="100"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] {%c:%L}-%m%n"/>
        </layout>
    </appender>

    <!-- [info日志APPENDER] -->
    <appender name="INFO-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="${webApp.root}/logs/info-log.log"/>
        <param name="Append" value="false"/>
        <param name="encoding" value="UTF-8"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd'.log'"/>
        <param name="threshold" value="info"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] {%c:%L}-%m%n"/>
        </layout>
        <!-- 我只想记录info日志,就做如下设置 -->
        <!-- <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMax" value="info" />
            <param name="LevelMin" value="info" />
        </filter> -->
    </appender>

    <!-- name指定的是包名 additivity 是否继承root -->
    <logger name="com.sun.expression" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="DEBUG-APPENDER"/>
        <appender-ref ref="console"/>
    </logger>

    <!-- 所有logger的父类,记录所有的日志。 -->
    <root>
        <level value="ALL"/>
        <appender-ref ref="dailyRollingFile"/>
        <appender-ref ref="INFO-APPENDER"/>
        <appender-ref ref="console"/>
    </root>

</log4j:configuration>