Log4J attach only one class to an appender

2019-04-14 07:18发布

I need to poll the JVM memory stats of my running application on a regular basis. I'm running a service that does this and writes the stats to the root Logger (I don't have much control over Root Logger usage or not).

What I want to do is route just these logging messages to a single appender. This appender should only process logging messages from this one class, and not from any other class. The other appenders shouldn't receive the messages from this one class.

So far I have the log messages only going to this 'memoryStats' appender. However, all the other logging message are also going to that appender, and I need to get rid of them, but I'm not sure how short of listing out every single class which would be a nightmare.

log4j.rootCategory=info, A1, R, MEM

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%p] %d{dd MMM yyyy HH;mm:ss,SSS} %t %c{2} (line:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=logs/ui.log

log4j.appender.R.MaxFileSize=100MB
# Keep backup files
log4j.appender.R.MaxBackupIndex=9

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%p] %d{dd MMM yyyy HH;mm:ss,SSS} %t %c{2} (line:%L) - %m%n



log4j.category.foo.bar.services.App.MemoryStats=debug, MEM
log4j.additivity.foo.bar.services.App.MemoryStats=false
log4j.appender.MEM=org.apache.log4j.RollingFileAppender
log4j.appender.MEM.File=logs/memStats.log

log4j.appender.MEM.MaxFileSize=100MB
# Keep backup files
log4j.appender.MEM.MaxBackupIndex=9

log4j.appender.MEM.layout=org.apache.log4j.PatternLayout
log4j.appender.MEM.layout.ConversionPattern=[%p] %d{dd MMM yyyy HH;mm:ss,SSS} %t %c{2} (line:%L) - %m%n

1条回答
beautiful°
2楼-- · 2019-04-14 07:34

You should remove MEM from log4j.rootCategory.

Change:

log4j.rootCategory=info, A1, R, MEM

to:

log4j.rootCategory=info, A1, R
查看更多
登录 后发表回答