星期二, 十一月 28, 2006

Using Log4j 使用

One example:

Logger logger = Logger.getLogger(SZ800BusHandler.class);
PropertyConfigurator.configure("conf/log4j.properties");
logger.debug("debug"); //the lowest level
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.fatal("fatal"); //the highest level

log4j.properties:

log4j.rootLogger=info, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=log\\sinker.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

note that: If you set "info" in the log4j properties, the log will write info,warn,error,fatal log. in the other words, log will record higher level's log.

没有评论: