如何引导 log4j 输出,以便不同的日志级别发送到不同的附加程序?

发布于 2024-07-17 06:52:25 字数 182 浏览 4 评论 0原文

是否可以将“debug”和“info”输出写入控制台,而“info”输出仅写入某个日志文件? 例如,给定以下日志记录:

LOG.debug(fileContent);
LOG.info(fileLength);

相应的 log4j.xml 是什么样子?

Is it possible to have "debug" and "info" output written to the console while the "info" output is only written to some log file? For example, given this logging:

LOG.debug(fileContent);
LOG.info(fileLength);

What does a corresponding log4j.xml look like?

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

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

发布评论

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

评论(5

随遇而安 2024-07-24 06:52:25

好的,我现在明白了:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        ...
    </appender>

    <appender name="otherAppender"
              class="org.apache.log4j.FileAppender FileAppender">
       <param name="Threshold" value="INFO"/>
        ...
    </appender>

    <root>
        <priority     value="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="otherAppender" />
    </root>
</log4j:configuration>

谢谢您的帮助!

Ok, I've got it now:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        ...
    </appender>

    <appender name="otherAppender"
              class="org.apache.log4j.FileAppender FileAppender">
       <param name="Threshold" value="INFO"/>
        ...
    </appender>

    <root>
        <priority     value="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="otherAppender" />
    </root>
</log4j:configuration>

Thanks for your help!

私藏温柔 2024-07-24 06:52:25

这绝对是可能的。 配置看起来像这样(不检查语法正确性):

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        ...
    </appender>

    <appender name="otherAppender"
              class="org.apache.log4j.FileAppender FileAppender">
        ...
    </appender>

    <logger name="com.mycompany.mypackage.MyClass">
        <level        value="info"/>
        <appender-ref ref="otherAppender" />
    </logger>

    <root>
        <priority     value="debug" />
        <appender-ref ref="console" />
    </root>
</log4j:configuration>

所有调试和信息消息都发送到控制台附加程序。 信息消息发送至 otherAppender

That is definitely possible. The configuration would look something like this (not checked for syntactic correctness):

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        ...
    </appender>

    <appender name="otherAppender"
              class="org.apache.log4j.FileAppender FileAppender">
        ...
    </appender>

    <logger name="com.mycompany.mypackage.MyClass">
        <level        value="info"/>
        <appender-ref ref="otherAppender" />
    </logger>

    <root>
        <priority     value="debug" />
        <appender-ref ref="console" />
    </root>
</log4j:configuration>

All debug and info messages go to the console appender. Info messages go to otherAppender.

酷炫老祖宗 2024-07-24 06:52:25

转到 页面查看一些示例。

它就像向程序中添加两个不同的附加程序一样简单,一个附加程序用于您想要执行的每种类型的日志记录。

Go to this page for some examples.

It's as simple as adding two different appenders to your program, one appender for each type of logging you want to do.

蓝天 2024-07-24 06:52:25

通过 Eddie 的配置,我只能获得 MyClass 的“info”输出。 但我想要的是 MyClass 的“信息”输出转到文件,而 MyClass 的“调试”输出转到控制台。

With the configuration from Eddie I can only get the "info" output for MyClass. But what I would like to have is that the "info" output of MyClass goes to a file AND the "debug" output of MyClass goes to console.

小巷里的女流氓 2024-07-24 06:52:25

按照 rwwilden 建议的操作,但删除这部分:

<logger name="com.mycompany.mypackage.MyClass">
    <level value="info"/>
    <appender-ref ref="otherAppender" />
</logger>

并在 otherAppender 下添加

Do as rwwilden suggested but remove this part:

<logger name="com.mycompany.mypackage.MyClass">
    <level value="info"/>
    <appender-ref ref="otherAppender" />
</logger>

And add <param name="Threshold" value="INFO"/> under the otherAppender.

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