Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j properties: LevelMatchFilter doesn't work

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
like image 392
Michael Avatar asked Jun 28 '12 11:06

Michael


2 Answers

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 
like image 56
royalpath Avatar answered Sep 25 '22 12:09

royalpath


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
like image 38
Gabo.Egui.EC Avatar answered Sep 24 '22 12:09

Gabo.Egui.EC