log4j:在我的日志文件中仅显示INFO(不是DEBUG)

时间:2016-11-20 16:30:42

标签: java netbeans log4j

我能够在NetBeans上设置并运行log4j。我理解级别中的优先级链,现在这是我的属性文件配置

# Root logger option
log4j.rootLogger=INFO, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=F:\\log4j-applicationWS1.log
log4j.appender.file.MaxFileSize=2MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

我想将纯INFO打印到console和我的log file? 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您正在寻找的是LevelRangeFilter

# Root logger option
log4j.rootLogger=INFO, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -- %m%n
log4j.appender.stdout.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.stdout.filter.a.levelMin=INFO
log4j.appender.stdout.filter.a.levelMax=INFO
log4j.appender.stdout.filter.a.acceptOnMatch=true

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log4j-applicationWS1.log
log4j.appender.file.MaxFileSize=2MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -- %m%n