如何从同一应用程序记录到应用程序日志和安全日志
我有一个应用程序需要将“应用程序日志消息”记录到一个附加程序,并将“安全日志消息”记录到另一个附加程序(安全日志消息不能出现在应用程序日志中)。我正在考虑像这样设置我的记录器:
Logger appLogger = Logger.getLogger("app." + myClassName);
Logger securityLogger = Logger.getLogger("security." + myClassName);
并像这样设置我的 log4j 配置:
log4j.logger.app = DEBUG, applicationLogAppender
log4j.additivity.app = false
log4j.logger.security = DEBUG, securityLogAppender
log4j.additivity.security = false
配置 log4j 来执行此操作的最佳方法是什么? 我相信我的解决方案将起作用,并且仍然使开发人员能够启用和禁用特定包/类的日志记录,只要他们记得在包/类名称前加上“app”前缀即可。或“安全”,但我想知道是否有更好的方法来做到这一点。
注意:该解决方案需要适用于 log4j 和 log4cxx。
I have an application that needs to log "application log messages" to an appender and "security log messages" to another appender (security log messages cannot appear in the application log). I was thinking setting up my loggers like this:
Logger appLogger = Logger.getLogger("app." + myClassName);
Logger securityLogger = Logger.getLogger("security." + myClassName);
And setting my log4j configuration like this:
log4j.logger.app = DEBUG, applicationLogAppender
log4j.additivity.app = false
log4j.logger.security = DEBUG, securityLogAppender
log4j.additivity.security = false
What is the best way to configure log4j to do this? I believe my solution will work and still gives developers the ability to enabled and disable logging for a specific package/class as long as they remember to prefix the package/class name with "app." or "security.", but I was wondering if there was a better way of doing this.
NOTE: The solution will need to work for log4j and log4cxx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两年前,我接到了完全相同的任务,我提出了这一点,只有我们的记录器前缀被称为“admin”和“debug”。您/我的建议有点创建两个独立的顶级记录器,位于根记录器下方。
我不认为 log4j/cxx 允许不配置内置根记录器。为了阻止它记录来自不遵循您的命名策略的记录器的thrash,我将其日志级别设置为“OFF”。
Two years ago I was given quite the same task and I proposed exactly this, only our logger prefixes were called "admin" and "debug". Your/mine suggestion sort of creates two independent top-level loggers, just below the root logger.
I don't think log4j/cxx allows the built-in root logger not to be configured. To block it from logging thrash from loggers that don't follow your naming policy, I'd set its log level to "OFF".