参照:"小樽的雨后"转载的博客 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:/WEB-INF/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:/WEB-INF/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:/WEB-INF/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:/WEB-INF/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:/WEB-INF/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.test"> <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>
测试:Main.java
package com.test;
import org.apache.log4j.Logger;
/**
* @ClassName: Main
* @Description: TODO
* @author
* @company
* @date 2013-3-8
* @version V1.0
*/
public class Main {
private static final Logger log = Logger.getLogger(Main.class);
/**@Title: main
* @Description: TODO
* @param args:
* @author
* @date 2013-3-8
*/
public static void main(String[] args) {
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
}
}
生成的文件
文件中的内容:
debug.log:
2013/03/08 11:02:20.020 [DEBUG] com.test.Main - debug
2013/03/08 11:02:20.020 [INFO ] com.test.Main - info
2013/03/08 11:02:20.020 [WARN ] com.test.Main - warn
2013/03/08 11:02:20.020 [ERROR] com.test.Main - error
error.log
2013/03/08 11:02:20.020 [ERROR] com.test.Main - error
info.log
2013/03/08 11:02:20.020 [INFO ] com.test.Main - info
traceLog4Debug.log
2013/03/08 11:02:20.020 [DEBUG] com.test.Main - debug
2013/03/08 11:02:20.020 [INFO ] com.test.Main - info
2013/03/08 11:02:20.020 [WARN ] com.test.Main - warn
2013/03/08 11:02:20.020 [ERROR] com.test.Main - error
warn.log
2013/03/08 11:02:20.020 [WARN ] com.test.Main - warn