码迷,mamicode.com
首页 > 其他好文 > 详细

通过log4j保存异常信息

时间:2014-08-08 02:06:35      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:log4j   异常   

log4j.properties:


### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c:%L - %m%n

### direct messages to file myfile.log ###
log4j.appender.myfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myfile.File=logs/myfile.log
log4j.appender.myfile.DatePattern='.'yyyy-MM-dd
log4j.appender.myfile.layout=org.apache.log4j.PatternLayout
log4j.appender.myfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c:%L - %m%n

log4j.rootLogger=debug, stdout,myfile

### name:com.demo,level:info,Appender and layout inherit rootLogger ###
log4j.logger.com.demo=info



测试代码:


import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;
import org.junit.Test;

public class LogInputTest {
	static Logger logger = Logger.getRootLogger();

	@Test
	public void testLog() {
		StringBuffer sb = null;
		StringWriter out=new StringWriter();
		try {
			sb.append("hello");
		} catch (NullPointerException e) {
			e.printStackTrace(new PrintWriter(out));
			logger.info(out.toString());
		}
	}
	
}


通过log4j保存异常信息,布布扣,bubuko.com

通过log4j保存异常信息

标签:log4j   异常   

原文地址:http://blog.csdn.net/wobendiankun/article/details/38427627

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!