最近被问到一个log4j分不同文件记录不同级别日志的问题,一下给难倒了,以前没有留意过log4j的具体功能,只是了解了下。如今自己还差的远啊。╮(╯▽╰)╭。。。
参考了前段时间的那个项目中的log4j配置文件,发现那个只不过将不同模组的日志记录到不同的日志文件。并未按日志级别分文件记录。自己在动手调试的过程中结合查阅网上很多博文,终于以xml格式配置的方式调通。具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/>
</layout>
</appender>
<appender name="traceLog4Debug" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/traceLog4Debug.log"/>
<param name="maxFileSize" value="2000KB"/>
<param name="maxBackupIndex" value="20"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/>
</layout>
</appender>
<appender name="debugLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/debug.log"/>
<param name="maxFileSize" value="2000KB"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/>
</layout>
<span style="color: #ff0000;"><!-- filter作用? 配置LevelMax与LevelMin限定输出到文件的日志级别 -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="DEBUG" />
<param name="LevelMin" value="DEBUG" />
</filter></span>
</appender>
<appender name="infoLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/info.log"/>
<param name="maxFileSize" value="2000KB"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="INFO" />
<param name="LevelMin" value="INFO" />
</filter>
</appender>
<appender name="warnLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/warn.log"/>
<param name="maxFileSize" value="2000KB"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="WARN" />
<param name="LevelMin" value="WARN" />
</filter>
</appender>
<appender name="errorLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/error.log"/>
<param name="maxFileSize" value="2000KB"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy/MM/dd HH:mm:ss.sss} [%-5p] %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="ERROR" />
<param name="LevelMin" value="ERROR" />
</filter>
</appender>
<!-- 记录该包下所有日志 -->
<logger name="com.javaeye.paopaohou">
<level value="ALL"/>
<appender-ref ref="stdout"/>
<appender-ref ref="traceLog4Debug"/>
<appender-ref ref="debugLog"/>
<appender-ref ref="infoLog"/>
<appender-ref ref="warnLog"/>
<appender-ref ref="errorLog"/>
</logger>
<logger name="org.hibernate.SQL">
<level value="DEBUG"/>
<appender-ref ref="traceLog4Debug"/>
<appender-ref ref="stdout"/>
</logger>
<logger name="org.hibernate">
<level value="error"/>
<appender-ref ref="traceLog4Debug"/>
<appender-ref ref="stdout"/>
</logger>
<logger name="org.springframework">
<level value="error"/>
<appender-ref ref="traceLog4Debug"/>
<appender-ref ref="stdout"/>
</logger>
</log4j:configuration>
只不过是在配置appender的时候配置了一个LevelRangeFilter的filter,并配置LevelMax与LevelMin(最大最小级别),加个测试类:
package com.javaeye.paopaohou;
import org.apache.log4j.Logger;
public class TestLog4j {
private static final Logger log = Logger.getLogger(TestLog4j.class);
/**
* 测试log4j不同级别日志分文件记录
*
*/
public static void main(String[] args) {
log.debug("debug...");
log.info("info...");
log.warn("warn...");
log.error("error...");
}
}
转载地址:http://www.iteye.com/topic/744693