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:/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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值