Log4j - 基于项目模块记录到多个日志文件

发布于 2024-09-03 10:52:11 字数 866 浏览 2 评论 0原文

考虑这种情况:

我有一个包含两个模块和一个公共模块的项目,如下所示(包结构):

com.mysite.moduleone
com.mysite.moduletwo
com.mysite.commonmodule

在上面,commonmodule类可以被其他两个模块使用模块。

问题

我需要配置Log4J,使来自moduleonemoduletwo的日志消息转到不同的日志文件。我总是可以使用 类别

但真正的问题是当我也想记录来自 commonmodule 的消息时。因此,当从 moduleone 调用 commonmodule 类时,commonmodule 日志消息应转到 moduleone 日志文件。如果从 moduletwo 访问 commonmodule,则 commonmodule 日志消息应转到 moduletwo 日志文件。

是否可以以这种方式配置 Log4J?有什么意见吗?

PS:我想我的问题已经说清楚了。如果有任何困惑,请发表评论,我们会尽力清除。 :)

Consider this scenario:

I have a project with two modules and one common module as below (the package structure):

com.mysite.moduleone
com.mysite.moduletwo
com.mysite.commonmodule

In the above, the commonmodule classes can be used by other two modules.

The question:

I need to configureLog4J such a way that the log messages from moduleone and moduletwo goes to different log file. I can always do this using using category.

But the real problem is when I want to log the messages from the commonmodule also. So, when the commonmodule classes are called from moduleone the commonmodule log messages should go to the moduleone log file. If the commonmodule is accesse from moduletwo the commonmodule log messages should go to moduletwo log file.

Is it possible to configure Log4J in this fashion? Any comments?

PS: I think I made my question clear. If any confusion, leave a comment, wil try to clear it. :)

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

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

发布评论

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

评论(2

沉睡月亮 2024-09-10 10:52:11

是否可以以这种方式配置 Log4J?

简而言之,它不适用于 log4j,除非您编写自定义过滤器。该过滤器需要捕获并分析每个日志事件的调用堆栈,使其到达每个日志附加程序......这将是昂贵的。

Is it possible to configure Log4J in this fashion?

In short, it is not with log4j unless you write a custom filter. And that filter would need to capture and analyse the call stack for each log event that makes it to each log appender ... which would be expensive.

欢烬 2024-09-10 10:52:11

我同意 @Stephen C 的观点,即如果没有昂贵的自定义记录器就无法使用 Log4j 来做到这一点。

另一个类似的日志框架 Logback 支持两个选项,这可能足以消除对自定义附加程序的需要。它们被称为MDC(映射诊断上下文)和标记。

标记提供了一种将自定义信息添加到日志记录语句中的方法,您可以(取决于公共包的使用方式)传入要使用的标记,具体取决于使用它的模块。

可以通过在进入公共模块中的代码之前设置它们来使用 MDC,然后在离开时取消设置它们。

它还有一个 过滤器,可以通过附加程序进行过滤。

I'm in agreement with @Stephen C about not being able to do so with Log4j without an expensive custom logger.

Another similar logging framework called Logback supports two options which may be enough to remove the need for a custom appender. They are called MDC (Mapped Diagnostic Contexts) and Markers.

Markers provide a way to add a custom piece of information to a logging statement, and you could (depending on how the common package is used) pass in a Marker to use depending on which module is using it.

MDCs could be used by setting them just before going into code in the common module and then unsetting them when leaving.

It also has a filter which can filter by appender on either.

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