Loggers
日志操作
Appenders
日志的展现形式
Layouts
日志的展现格式
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
TRACE < DEBUG < INFO < WARN < ERROR < FATAL
logger.debug(Object message) //日志请求等级为:DEBUG info(Object message) //日志请求等级为:INFO logger.error(Object message) //日志请求等级为:ERROR
日志请求等级必须>=日志等级,否则请求日志操作无效
### 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{1}:%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{1}:%L - %m%n log4j.rootLogger=warn, stdout,myfile
说明:
log4j.rootLogger=warn, stdout,myfile 定义主日志工具:rootLogger,其它日志工具可以通过日志名称从它继承 日志级别:warn 定义2两个Appender stdout: org.apache.log4j.ConsoleAppender 对应log4j.appender.stdout及log4j.appender.stdout.layout的配置 myfile: org.apache.log4j.DailyRollingFileAppender 对应log4j.appender.myfile及log4j.appender.myfile.layout的配置
代码:
public class LogTest { Logger logger=Logger.getLogger("test"); @Test public void testLog(){ logger.debug("debug..."); logger.info("info..."); logger.warn("warn..."); logger.error("error..."); logger.fatal("fatal..."); } }
运行结果:
2014-06-29 05:46:50,805 WARN test:15 - warn... 2014-06-29 05:46:50,807 ERROR test:16 - error... 2014-06-29 05:46:50,807 FATAL test:17 - fatal...
### 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=warn, stdout,myfile ### name:com.demo,level:info,Appender and layout inherit rootLogger ### log4j.logger.com.demo=info说明:log4j.logger.com.demo=info 日志名称:com.demo 日志等级为:info Appender and layout 继承 rootLogger
代码:package org.log.test; import static org.junit.Assert.*; import org.apache.log4j.Logger; import org.junit.Test; public class LogTest { Logger logger=Logger.getLogger("com.demo.test"); @Test public void testLog(){ logger.debug("debug..."); logger.info("info..."); logger.warn("warn..."); logger.error("error..."); logger.fatal("fatal..."); } }
com.demo.test 继承com.demo,通过包名形式的名称来实现
结果:
2014-06-29 06:12:13,488 INFO com.demo.test:14 - info... 2014-06-29 06:12:13,491 WARN com.demo.test:15 - warn... 2014-06-29 06:12:13,492 ERROR com.demo.test:16 - error... 2014-06-29 06:12:13,492 FATAL com.demo.test:17 - fatal...
原文地址:http://blog.csdn.net/wobendiankun/article/details/35633321