问题描述
我需要定期轮询正在运行的应用程序的JVM内存统计信息。我正在运行一个执行此操作的服务并将统计信息写入根Logger(我对Root Logger的使用情况没有太多控制权)。
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).
我想要做的是将这些日志消息路由到单个appender。此appender应该只处理来自这一个类的日志消息,而不是来自任何其他类。其他appender不应该收到来自这一个类的消息。
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.
到目前为止,我只有日志消息才会进入这个'memoryStats'appender。然而,所有其他日志消息也都会转移到那个appender,我需要摆脱它们,但我不确定列出每一个可能是一场噩梦的课程有多短。
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
推荐答案
你应该从 log4j.rootCategory $中删除MEM c $ c>。
更改:
log4j.rootCategory=info, A1, R, MEM
to:
log4j.rootCategory=info, A1, R
这篇关于Log4J只将一个类附加到appender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!