Log4j 的使用

阅读更多
1. 把 log4j 包 添加进lib 里
2. 在 code里加以下

private static Logger logger = Logger.getLogger(%YourClassName%.class);


3. 在%YourClassName% 构造函数里,设置debug 级别

public %YourClassName% (){
   logger.setLevel(Level.DEBUG); //INFO,ERROR
}
4. 在工程目录下,加上 log4j.properties, 一般格式为:

log4j.rootLogger=Error, ConsoleAppender, DetailsAppender, ErrorAppender

## Console output log
log4j.appender.ConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern=[%d] [%-5p] %m%n

## Detail debug log
log4j.appender.DetailsAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DetailsAppender.Append=false
#log4j.appender.DetailsAppender.Threshold = INFO
log4j.appender.DetailsAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DetailsAppender.layout.ConversionPattern=[%d] [%-5p] %c{1}:%M - %m%n
log4j.appender.DetailsAppender.File=logs/log.log
log4j.appender.DetailsAppender.MaxFileSize=100MB
log4j.appender.DetailsAppender.MaxBackupIndex=10


## Error log
log4j.appender.ErrorAppender = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ErrorAppender.File = logs/error.log
log4j.appender.ErrorAppender.Append = false
log4j.appender.ErrorAppender.Threshold = ERROR
log4j.appender.ErrorAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern = [%d] [%-5p] %c{1}:%M - %m%n

你可能感兴趣的:(log4j,Java)