从 log4j 或 logback 中调用实例继承附加程序

发布于 2024-09-01 09:33:36 字数 381 浏览 4 评论 0原文

在我的程序中,我有 2 个独立的日志事件流(为简单起见,将它们称为流,实际上是 2 个附加程序)。 Stream1 包含客户端日志记录,Stream2 包含控制日志记录。现在这似乎很容易,除了某些类可以同时出现在客户端日志记录和服务器日志记录中,具体取决于情况。使问题进一步复杂化的是,客户端想要的命令发生在 2 个独立的线程中(其中一个是从线程池中随机获取的),因此不可能使用 MDC 或 NDC 进行任何类型的跟踪。

如果记录器可以从调用实例继承附加程序,那么真正可以简化这一点。这样我就可以为 2 个记录器设置 2 个附加程序并完成。但是我不知道如何干净或轻松地做到这一点。谁能就如何做到这一点提供任何建议?

注意:如果需要传递某些内容,我确实有一个事件 bean,它会传递到链中的所有内容,以便在必要时使用。

In my program I have 2 separate streams of logging events (calling them streams for simplicity, in reality its 2 appenders). Stream1 contains client logging and Stream2 contains control logging. Now this might seem easy, except that certain classes can be both in the client logging and server logging, depending on the situation. Complicating this further is the fact that a command that a client wants takes place in 2 separate threads (one being fetched randomly from a thread pool), so any kind of tracking with MDC or NDC isn't possible.

What would really simplify this is if the logger could inherit the appenders from the calling instance. That way I can setup 2 appenders for 2 loggers and be done. However I have no idea how to do it cleanly or easily. Can anyone offer any advice on how to do so?

Note: If something needs to be passed around, I do have a event bean that gets passed to everything in the chain that can be used if necessary.

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

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

发布评论

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

评论(1

天赋异禀 2024-09-08 09:33:36

您已经提到客户端处理发生在多个线程中,因此简单的 ThreadLocal 方法可能不起作用...但是 ThreadGroupLocal 可以吗?

请参阅 [Java 中是否存在线程组局部变量?

我建议的策略是只在日志框架中注册一个附加程序。该附加程序将是您编写的东西。该实现将委托给 ThreadLocal/ThreadGroupLocal 附加程序。 那个附加器将是特定于客户端或控件的。

另请注意,不应在异步或批处理抽象下配置您的附加程序。

You've already mentioned that the client processing takes place in multiple threads, so a simple ThreadLocal approach might not work... but would a ThreadGroupLocal work?

See [Are there thread group-local variables in Java?

The tact I am suggesting is to only have one appender registered with the logging framework. That appender would be something you write. The implementation would delegate to the ThreadLocal/ThreadGroupLocal appender. The that appender would be client- or control-specific.

Also note that your appender should not be configured beneath asynchronous or batching abstractions.

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