Log4J attach only one class to an appender

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

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:

You should remove MEM from log4j.rootCategory.

Change:

log4j.rootCategory=info, A1, R, MEM

to:

log4j.rootCategory=info, A1, R


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!