如何从同一应用程序记录到应用程序日志和安全日志

发布于 2024-11-16 01:57:16 字数 661 浏览 2 评论 0原文

我有一个应用程序需要将“应用程序日志消息”记录到一个附加程序,并将“安全日志消息”记录到另一个附加程序(安全日志消息不能出现在应用程序日志中)。我正在考虑像这样设置我的记录器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韬韬不绝 2024-11-23 01:57:16

两年前,我接到了完全相同的任务,我提出了这一点,只有我们的记录器前缀被称为“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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文