### 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()); } } }
原文地址:http://blog.csdn.net/wobendiankun/article/details/38427627