java log4j 选择要记录到哪个文件

发布于 2025-01-01 03:13:47 字数 567 浏览 2 评论 0原文

我需要将同一类的调试消息记录到不同的文件中。
我的意思是,在同一个类中,我需要一个特定的调试语句来转到 fileA,同时另一个特定的调试语句要转到 fileB

如果不清楚,我想做的是将网络消息记录到一个完全独立的文件中,而不是此类输出的其他登录消息。

如果我这样做

<logger name="com.test.modules" additivity="false" >
    <priority value="debug"/>
    <appender-ref ref="netWorkCommunication"/>  
    <appender-ref ref="generalDebug"/>  
  </logger>  

,那么我的类的日志记录将转到这两个文件(因为它来自同一个包)。

如何配置 log4j 以便可以从 com.test.modules 下的类中选择哪个日志记录语句转到哪个文件附加程序

I need to log debugging messages from the same class into different files.
What I mean is that from the same class I need a specific debug statement to go to fileA while another specific debug statement to go to fileB.

In case it is not clear, what I am trying to do is to log network messages to a completely separate file than other loggin messages that this class outputs.

If I do

<logger name="com.test.modules" additivity="false" >
    <priority value="debug"/>
    <appender-ref ref="netWorkCommunication"/>  
    <appender-ref ref="generalDebug"/>  
  </logger>  

Then the logging from my class will go to both files (since it is from the same package).

How can I configure log4j so that I can select which logging statement from classes under com.test.modules go to which file appender?

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

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

发布评论

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

评论(2

一杯敬自由 2025-01-08 03:13:47

为每个文件使用单独的 Logger:

private static Logger log = Logger.getLogger(YourClass.class);
private static Logger networkLog = Logger.getLogger("network." + YourClass.class.getName());
private static Logger httpLog = Logger.getLogger("http." + YourClass.class.getName());

并像往常一样为每个 Logger 定义附加程序。

Use a separate Logger for each file:

private static Logger log = Logger.getLogger(YourClass.class);
private static Logger networkLog = Logger.getLogger("network." + YourClass.class.getName());
private static Logger httpLog = Logger.getLogger("http." + YourClass.class.getName());

and define the appender for each Logger as usual.

海拔太高太耀眼 2025-01-08 03:13:47

这看起来确实是应该使用多个记录器的场景。为网络配置一个单独的记录器,只需为其指定一个不同的附加程序。

This really looks a scenario where multiple loggers should be used. Configure a separate logger for networking and just specify a different appender for it.

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