如何在 Log4J 中记录指定框架类的输出

发布于 2025-01-12 10:55:41 字数 682 浏览 3 评论 0原文

我需要将特定 Spring 类(org.springframework.core.log.LogFormatUtils)的输出记录到给定的附加程序(最后是 Graylog,但我使用 FileAppender 进行测试 - 这里并不重要)。 我知道,一般来说,这可以通过使用来完成

<Loggers>
        <Logger name="org.springframework.core.log.LogFormatUtils" level="DEBUG">
            <AppenderRef ref="FileAppender"/>
        </Logger>
        <Root level="debug" includeLocation="true">
            <AppenderRef ref="ConsoleAppender"/>
        </Root>
</Loggers>

,但是,这不起作用 - 它不输出任何内容。如果我将记录器的名称/包引用更改为“org.springframework”,它会按预期工作,但我对以这种方式出现的一堆信息不感兴趣。此外,如果我表示类似 com.myapp.mypackage 的内容,它也可以工作。 那么,在我可以记录 Spring 框架输出之前,是否需要执行一些注意事项或步骤?

I need to log the output of a specific Spring class (org.springframework.core.log.LogFormatUtils) to a given appender (Graylog in the end, but I used a FileAppender for testing purposes - doesn't matter here).
I'm aware that generally, this could be done quite simple by using

<Loggers>
        <Logger name="org.springframework.core.log.LogFormatUtils" level="DEBUG">
            <AppenderRef ref="FileAppender"/>
        </Logger>
        <Root level="debug" includeLocation="true">
            <AppenderRef ref="ConsoleAppender"/>
        </Root>
</Loggers>

However, this does not work - it outputs nothing. If I change the logger's name / package reference to "org.springframework" it works as expcted, but I'm not interested in the bunch of inforamtion coming along that way. Additionally, if I denote something like com.myapp.mypackage, it also works.
So, are there some caveats or steps to perform before I can log Spring framework output in general?

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

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

发布评论

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

评论(1

如果没有 2025-01-19 10:55:41
public static void traceDebug(Log logger, Function<Boolean, String> messageFactory) {
    if (logger.isDebugEnabled()) {
        boolean traceEnabled = logger.isTraceEnabled();
        String logMessage = messageFactory.apply(traceEnabled);
        if (traceEnabled) {
            logger.trace(logMessage);
        }
        else {
            logger.debug(logMessage);
        }
    }
}

该方法是LogFormatUtils类中唯一使用Log对象的方法,但是它使用的Log对象是从其他类传递过来的,也就是说Log对象的logger name不是>org.springframework.core.log.LogFormatUtils,您可以将logger的名称更改为org.springframework,并将%logger添加到PatternLayout中以查找真实的logger名称你 需要。

public static void traceDebug(Log logger, Function<Boolean, String> messageFactory) {
    if (logger.isDebugEnabled()) {
        boolean traceEnabled = logger.isTraceEnabled();
        String logMessage = messageFactory.apply(traceEnabled);
        if (traceEnabled) {
            logger.trace(logMessage);
        }
        else {
            logger.debug(logMessage);
        }
    }
}

The method is the only method in the LogFormatUtils class that uses the Log object, but the Log object it uses is passed from other classes, that is to say, logger name of the Log object is not org.springframework.core.log.LogFormatUtils, you can change logger's name to org.springframework and add %logger to PatternLayout to find the real logger's name you need.

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