log4j properties: LevelMatchFilter doesn't wor

2019-04-09 06:12发布

I was trying to route my Logging to two different files: one for INFO messages and another one for ERRORs. LevelMatchFilter seemed the right way to go. Unfortunately, all messages are logged to my info.log, not just the INFO messages. Any ideas what I did wrong?

Here's my config:

# Define the root logger with appender file
log4j.logger.com.my.class.ClassName=DEBUG, FILE, ERR, CA

# Define the info file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=info.log
log4j.appender.FILE.filter.a=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.FILE.filter.a.LevelToMatch=INFO
log4j.appender.FILE.filter.a.AcceptOnMatch=true

# Define the layout for info file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d %-5p %c - %m%n

# Define the error file appender
log4j.appender.ERR=org.apache.log4j.FileAppender
log4j.appender.ERR.File=error.log
log4j.appender.ERR.Threshold=ERROR

# Define the layout for error file appender
log4j.appender.ERR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERR.layout.conversionPattern=%d %-5p %c - %m%n


log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d %-5p %c - %m%n

3条回答
Fickle 薄情
2楼-- · 2019-04-09 06:46

I think your file appender config should look something like this:

 # Define the info file appender
log4j.appender.FILE=org.apache.log4j.FileAppender    
log4j.appender.FILE.File=info.log    
log4j.appender.FILE.Threshold=INFO    
log4j.appender.FILE.filter.a=org.apache.log4j.varia.LevelMatchFilter    
log4j.appender.FILE.filter.a.LevelToMatch=INFO    
log4j.appender.FILE.filter.a.AcceptOnMatch=true    
log4j.appender.FILE.filter.b=org.apache.log4j.varia.LevelMatchFilter    
log4j.appender.FILE.filter.b.LevelToMatch=ERROR    
log4j.appender.FILE.filter.b.AcceptOnMatch=false    
log4j.appender.FILE.filter.c=org.apache.log4j.varia.LevelMatchFilter    
log4j.appender.FILE.filter.c.LevelToMatch=FATAL    
log4j.appender.FILE.filter.c.AcceptOnMatch=false 
查看更多
看我几分像从前
3楼-- · 2019-04-09 06:49

LevelMatchFilter has nothing to do with what you need.

You need to define 2 loggers, one will have the ERR appender and the other the FILE appender:

log4j.logger.my.info.logger=INFO, FILE
log4j.logger.my.err.logger=ERROR, ERR

And in the code you need to provide the logger name when you initialize the logger:

static Logger logger = Logger.getLogger("my.info.logger");
查看更多
够拽才男人
4楼-- · 2019-04-09 06:56

I finally get a solution (using filters) to a similar problem without making an external XML file, here is the resulting code:

log4j.rootLogger=info, R, ERROR

#### only INFO
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=FacturaElectronica.log
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.R.filter.a.LevelMin=INFO
log4j.appender.R.filter.a.LevelMax=INFO


#### only ERROR
log4j.appender.ERROR=org.apache.log4j.RollingFileAppender
log4j.appender.ERROR.File=FacturaElectronicaError.txt
log4j.appender.ERROR.MaxFileSize=500KB
log4j.appender.ERROR.MaxBackupIndex=1
log4j.appender.ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.ERROR.filter.b=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.ERROR.filter.b.LevelToMatch=ERROR
log4j.appender.ERROR.filter.b.AcceptOnMatch=true
log4j.appender.ERROR.Threshold=ERROR
查看更多
登录 后发表回答