首页 > 代码库 > 通过log4j保存异常信息
通过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()); } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。