Michael Michael - 1 month ago 24
Java Question

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

Answer

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