0

I am trying to create a log4j file appender that includes only messages for a specific class. Here is an example message:

2015-08-06 16:41:43,773 [pool-3-thread-8] INFO ACME.log- new test message

Where I want all log messages for ACME.log to go to a specific appender. Here is code I currently have in properties file that is not working.

log4j.appender.myLog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.myLog.File = /opt/netiq/idm/apps/tomcat/logs/acme.log
log4j.appender.myLog.layout = org.apache.log4j.PatternLayout
log4j.appender.myLog.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
log4j.appender.myLog.filter.filter.ID=ACME.log
log4j.appender.myLog.filter.ID.levelMin=INFO
log4j.appender.myLog.filter.ID.levelMax=ERROR
log4j.appender.myLog.filter.2 = org.apache.log4j.varia.DenyAllFilter
Chris
  • 379
  • 1
  • 6
  • 19

1 Answers1

2

If you're using log4j 1.x, we strongly recommend that you use org.apache.log4j.rolling.RollingFileAppender 1 instead of org.apache.log4j.DailyRollingFileAppender (may lose messages, Bug 43374).

So the configuration of you appender can be:

log4j.appender.myLog=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.myLog.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.myLog.rollingPolicy.fileNamePattern=/path/acme%d{yyyy-MM-dd}.log
log4j.appender.myLog.layout=org.apache.log4j.PatternLayout
log4j.appender.myLog.layout.ConversionPattern=%d %-5p (%c.java:%L).%M - %m%n
log4j.appender.myLog.filter.A=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.myLog.filter.A.LevelMin=INFO    
log4j.appender.myLog.filter.A.LevelMax=ERROR
log4j.appender.myLog.filter.A.AcceptOnMatch=true

If you only want the log of a class (e.g. com.company.MyClass), you specify it as follows:

log4j.logger.com.company.MyClass=TRACE, myLog

Notes

  1. In that case, you need to add the respective jar (apache-log4j-extras-1.2.17.jar).
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148