鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 手机系统开发 > J2ME > >

log4j不同的日志输出到不同的文件中

来源:互联网 作者:佚名 时间:2015-08-02 08:12
依赖jar包log4j-1.2.11.jar 下载地址:http://download.csdn.net/detail/xiao88xin/7320473 主类:Log4jMain.java package log4j;/*** * 依赖 log4j-1.2.11.jar */import org.apache.log4j.Logger;public class Log4jMain {/** * @param args */public stati

依赖jar包 log4j-1.2.11.jar 下载地址:http://download.csdn.net/detail/xiao88xin/7320473

主类:Log4jMain.java  


package log4j;

/***
 * 依赖 log4j-1.2.11.jar
 */
import org.apache.log4j.Logger;

public class Log4jMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/* log4j.properties中如果配置了log4j.Log4jMain 类对应的日志文件
		 * 打印到对应的文件中,否则打印到根日志文件中
		 */
		Logger log = Logger.getLogger(log4j.Log4jMain.class);
		log.info("Log4jMain");
		
		
		Test1Logger log1 = new Test1Logger();
		log1.logInfo("Test1Logger");
		
		Test2Logger log2 = new Test2Logger();
		log2.logInfo("Test2Logger");
		

	}

}



打印类: Test1Logger.java、Test2Logger.java


package log4j;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class Test1Logger {
	Logger log;
	public Test1Logger(){
		log = Logger.getLogger(log4j.Test1Logger.class);
	}
	public void logInfo(String message){
		log.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\t"+message);
	}
}

package log4j;


import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class Test2Logger {
	Logger log;
	public Test2Logger(){
		log = Logger.getLogger(log4j.Test2Logger.class);
	}
	public void logInfo(String message){
		log.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\t"+message);
	}
}



配置文件:log4j.properties
log4j.rootCategory=info,file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=F:/logstest/main.log
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= %p:%d{yyyyMMdd HH:mm:ss} %c: %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[appfuse] %p:%d{yyyyMMdd HH:mm:ss} %c: %m%n


# Test1Logger Log
log4j.additivity.log4j.Test1Logger=false
log4j.logger.log4j.Test1Logger=info,Test1Logger
log4j.appender.Test1Logger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Test1Logger.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.Test1Logger.File=F:/logstest/Test1Logger.log
log4j.appender.Test1Logger.layout = org.apache.log4j.PatternLayout
log4j.appender.Test1Logger.layout.ConversionPattern=%m%n
log4j.appender.Test1Logger.encoding=UTF-8

# Test2Logger Log
log4j.additivity.log4j.Test2Logger=false
log4j.logger.log4j.Test2Logger=info,Test2Logger
log4j.appender.Test2Logger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Test2Logger.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.Test2Logger.File=F:/logstest/Test2Logger.log
log4j.appender.Test2Logger.layout = org.apache.log4j.PatternLayout
log4j.appender.Test2Logger.layout.ConversionPattern=%m%n
log4j.appender.Test2Logger.encoding=UTF-8


# Log4jMain Log
#log4j.additivity.log4j.Log4jMain=false
#log4j.logger.log4j.Log4jMain=info,Log4jMain
#log4j.appender.Log4jMain=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.Log4jMain.DatePattern='.'yyyy-MM-dd-HH
#log4j.appender.Log4jMain.File=F:/logstest/Log4jMain.log
#log4j.appender.Log4jMain.layout = org.apache.log4j.PatternLayout
#log4j.appender.Log4jMain.layout.ConversionPattern=%m%n
#log4j.appender.Log4jMain.encoding=UTF-8


结果会在F:/logstest目录下生产3个日志文件,并做了日志记录



网友评论
<