Log4j:日志文件正确,但标准输出仅显示子集

发布于 2024-11-09 07:51:44 字数 2039 浏览 0 评论 0原文

我的 log4j 日志记录配置有问题。所有日志消息都按预期显示在 root.log 文件中,但标准输出仅显示 root.log 的子集,尽管我将其配置为相同。

编辑:确切地说,我错过了来自ab的所有消息。

编辑2:我不太确定,但我认为来自 ab 的一些消息(INFO 和 DEBUG)会记录在 stdout 上,而其他消息则不会。这可能是与线程相关的问题吗?例如,有些线程进行日志记录,有些则不进行?

以下是我的log4j.xml,最终是否有错误?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} [%t] (%C:%L) %-5p - %m%n" />
        </layout>
    </appender>

    <appender name="FILE_ALL" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="${catalina.home}/logs/root.log"/>
        <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" value="%d [%t] %-4r (%F:%L) %-5p %x - %m%n" />
        </layout>
    </appender>

    <logger name="a.b.master">
        <level value="DEBUG" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE_ALL" />
    </logger>

    <logger name="a.b.master.orm.support.HibernateSessionFilter">
        <level value="INFO" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE_ALL" />
    </logger>

    <logger name="a.b.master.aop.hibernate.OpenSessionInRequestInterceptor">
        <level value="info" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE_ALL" />
    </logger>

    <root>
        <level value="INFO"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_ALL"/>
    </root>
</log4j:configuration>

我正在使用 Commons Logging 作为实现。

感谢您的帮助!

I have a problem with the configuration of my logging with log4j. All the log messages are shown as expected in the root.log file, but the stdout, however, does only show a subset of the root.log although I configured it to be the same.

EDIT: Precisely I am missing all messages from a.b.

EDIT 2: I am not absolutely sure, but I think that some messages from a.b. (INFO and DEBUG) get loggedd on stdout, others don't. Might that be a problem related to threads? E.g. some threads doing logging, some not?

The following is my log4j.xml, is there eventually a mistake?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} [%t] (%C:%L) %-5p - %m%n" />
        </layout>
    </appender>

    <appender name="FILE_ALL" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="${catalina.home}/logs/root.log"/>
        <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" value="%d [%t] %-4r (%F:%L) %-5p %x - %m%n" />
        </layout>
    </appender>

    <logger name="a.b.master">
        <level value="DEBUG" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE_ALL" />
    </logger>

    <logger name="a.b.master.orm.support.HibernateSessionFilter">
        <level value="INFO" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE_ALL" />
    </logger>

    <logger name="a.b.master.aop.hibernate.OpenSessionInRequestInterceptor">
        <level value="info" />
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE_ALL" />
    </logger>

    <root>
        <level value="INFO"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_ALL"/>
    </root>
</log4j:configuration>

I am using Commons Logging as implementation.

Thanks for the help!

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

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

发布评论

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

评论(2

生死何惧 2024-11-16 07:51:44

您的配置与我的配置非常相似,我的日志被转储到控制台和某些远程主机中。试试这个,看看它是否适合你:-

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

    <appender name="FILE_ALL" class="org.apache.log4j.RollingFileAppender">
        ...
    </appender>

    <logger name="a.b.master">
        <level value="debug" />
    </logger>

    <logger name="a.b.master.orm.support.HibernateSessionFilter">
        <level value="info" />
    </logger>

    <logger name="a.b.master.aop.hibernate.OpenSessionInRequestInterceptor">
        <level value="info" />
    </logger>

    <root>
        <priority value="info"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_ALL"/>
    </root>
</log4j:configuration>

我认为你不需要在每个 ab 记录器中定义 appender-ref 因为它应该从 root 获取附加程序。另外,我对 root 使用 priority 而不是 logger。否则,您的配置看起来与我的相同,我能够让我的配置正常工作。如果由于某种原因这对您不起作用,我建议您从配置中删除自定义 ab 记录器,以查看是否能够在控制台中获取 ab 消息。

另外,仅供参考,我相信您已经知道这一点,但是如果您的 root 设置为使用“info”,那么实际上没有必要将自定义 ab 记录器也设置为“info”。我通常将我的根保留为“警告”,并让我的自定义记录器用“调试”或“信息”覆盖它。

Your configuration is very similar to mine where my logs are dumped into the console and to some remote host. Try this to see if it works for you:-

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

    <appender name="FILE_ALL" class="org.apache.log4j.RollingFileAppender">
        ...
    </appender>

    <logger name="a.b.master">
        <level value="debug" />
    </logger>

    <logger name="a.b.master.orm.support.HibernateSessionFilter">
        <level value="info" />
    </logger>

    <logger name="a.b.master.aop.hibernate.OpenSessionInRequestInterceptor">
        <level value="info" />
    </logger>

    <root>
        <priority value="info"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE_ALL"/>
    </root>
</log4j:configuration>

I don't think you need to define appender-ref in every a.b logger since it should pick up the appender from root. Also, I use priority instead of logger for root. Otherwise, your configuration looks identical to mine and I'm able to get mine working properly. If this doesn't work for you for some reason, I would suggest you to remove your custom a.b loggers from the configuration to see if you are able to get the a.b messages in your console.

Also, just FYI, I'm sure you already know this, but if your root is set to use "info", then there's really no need to set your custom a.b logger to "info" too. I usually leave my root to "warn" and have my custom loggers to override that with either "debug" or "info".

眼波传意 2024-11-16 07:51:44

我找到了解决方案,即使不是直接的问题。我向 ConsoleAppender 添加了一个参数,将所有内容打印到 stderr。现在这有效了。我猜这是 eclipse 或 log4j 的问题,因为如果我从终端(控制台)启动应用程序,我可以正确看到所有日志消息。

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.err" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-4r (%F:%L) %-5p %x - %m%n" />
        </layout>
    </appender> 

I found the solution even if not the problem directly. I added a parameter to my ConsoleAppender that everything is printed to the stderr. This now works. I guess its a problem of eclipse or log4j because if i am starting the app from the terminal (console) I can see all log messages properly.

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.err" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-4r (%F:%L) %-5p %x - %m%n" />
        </layout>
    </appender> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文