为某些用户启用日志记录 Log4j

发布于 2024-07-21 09:20:36 字数 136 浏览 4 评论 0原文

在 log4j.properties 文件中,我将级别设置为错误。 对于某些用户,我需要将级别设置为调试。 我能够在运行时更改日志记录级别,但这将为同时访问该应用程序的所有用户启用。 还有其他方法可以为选定的用户启用日志记录吗? 任何帮助将不胜感激。

In the log4j.properties file I've set the Level to ERROR. For certain users I need to set Level to DEBUG. I was able to change the logging level at run time, but this will be enabled for all the users accessing the application at the same time.
Is there any other method by which we can enable logging for selected users? Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

明月松间行 2024-07-28 09:20:36

我假设您有一个网络应用程序或类似的应用程序,并且多个可识别的用户同时访问它?

您无法轻松更改 Log4j 中每个用户的配置。 不过,我会考虑以下内容(假设有一个 Web 服务器或类似服务器,每个用户请求都在一个单独的线程上):

  1. 一旦用户进行了调用,就识别出进行服务器调用的用户。 将该用户数据存储在 MDC
  2. 实施中自定义 Log4J appender。 对于每个来电,您可以检查 MDC 中存储的用户,并根据需要调整严重性/日志记录。

这是一些工作(考虑到上述假设),但应该有效。 显然,如果我对您的架构所做的假设不正确,那么它是无效的。

I presume you have a web application or similar, and multiple identifiable users accessing it simultaneously ?

You can't easily alter configuration per user in Log4j. However I would consider the following (this assumes a web server or similar, with each user request being on a separate thread):

  1. identify the user making a server call as soon as they make that call. Store that user data in a MDC
  2. Implement a custom Log4J appender. For each incoming call, you can inspect the user stored in the MDC, and adjust the severity/logging as required.

That's a little bit of work (given the above assumptions) but should work. Obviously it's not valid if the assumptions I've made about your architecture are incorrect.

若无相欠,怎会相见 2024-07-28 09:20:36

这个问题在 logback(log4j 的后继者)中通过 TurboFilters 得到解决。 请参阅标题为“MDCFilter 和 MarkerFilter 配置”的示例。 如果您需要进一步的帮助,请联系 logback-user 邮件列表。

This problem is solved in logback (log4j's successor) with TurboFilters. See the example entitled "MDCFilter and MarkerFilter configuration". If you need further help, contact the logback-user mailing list.

江心雾 2024-07-28 09:20:36

对,但是。

您可以使记录器以涉及的类+用户命名,而不仅仅是类,并在方法上创建记录器(或者如果您想变得更奇特,可以将记录器缓存在由用户键入的某种池中),然后配置适当地记录。

它非常混乱并且对代码有侵入性,但由于用户是运行时属性,我不知道如何(除了 AspectJ 或其表兄弟)如何避免这种混乱。

另一种选择是专门格式化日志消息并在日志消息中包含用户名,然后解析日志。 这将使每个人都能够进行调试(显然,这可能是一个性能问题),但如果更关心的是隔离用户日志记录而不是限制调试调用的数量,那么这可能是一个解决方案。

Yes, but.

You could make your loggers named with the class + user involved, rather than just the class, and create the logger on the method (or if you want to get fancy, cache loggers in some kind of pool keyed by user), and then configure logging appropriately.

It is very messy and intrusive to the code, but since the user is a runtime property, I don't see how (short of AspectJ or its cousins) how you avoid that kind of mess.

Another option is to specially format your log messages and include the user name in the log message, and then parse the logs afterwards. This would enable debugging for everyone (which may be a performance issue, obviously) but if the concern is more about isolating a users logging rather than limiting the amount of debug calls, that may be a solution.

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