Log4J 仅将一个类附加到附加程序
我需要定期轮询正在运行的应用程序的 JVM 内存统计信息。我正在运行一个服务来执行此操作并将统计信息写入根记录器(我对根记录器的使用与否没有太多控制权)。
我想要做的是将这些日志消息路由到单个附加程序。该附加程序应该只处理来自该类的日志消息,而不是来自任何其他类的日志消息。其他附加程序不应接收来自这一类的消息。
到目前为止,我的日志消息仅发送到这个“memoryStats”附加程序。然而,所有其他日志消息也会发送到该附加程序,我需要删除它们,但我不确定列出每个类有多短,这将是一场噩梦。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该从
log4j.rootCategory
中删除 MEM。更改:
至:
You should remove MEM from
log4j.rootCategory
.Change:
to: