输出?-6ren"> 输出?-知道如何对 Logback 本身的调试消息设置阈值过滤器吗? %d{HH:mm:ss.SSS} %-5level [%thread] [%logg-6ren">
gpt4 book ai didi

java - 将阈值过滤器添加到 Logback 输出?

转载 作者:行者123 更新时间:2023-12-02 06:05:17 24 4
gpt4 key购买 nike

知道如何对 Logback 本身的调试消息设置阈值过滤器吗?

<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level [%thread] [%logger{0}] [%M:%L] - %m%n</pattern>
</encoder>
</appender>

...

<root level="TRACE">
<appender-ref ref="STDOUT" />
<appender-ref ref="ASYNC_SLACK_LOGS" />
</root>
</configuration>

以上产生详细的调试输出:

16:05:16,479 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
16:05:16,480 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at [file:/W:/JavaProjects/logback-slack-appender/target/test-classes/logback-test.xml]
16:05:16,697 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:05:16,703 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
...
6:05:16,825 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@2d901eb0 - Registering current configuration as safe fallback point
16:05:17.696 INFO [main] [SlackAppenderTest] [simulateAPITrouble:19] - This will fail to post to Slack
16:05:18.256 |-ERROR in com.github.maricn.logback.SlackAppender[SLACK_LOGS] - Slack POST request failed: 404 no_team

如何将调试输出限制为仅显示 Logback 核心错误?例如:

16:05:17.696 INFO  [main] [SlackAppenderTest] [simulateAPITrouble:19] - This will fail to post to Slack
16:05:18.256 |-ERROR in com.github.maricn.logback.SlackAppender[SLACK_LOGS] - Slack POST request failed: 404 no_team

原因是因为我想捕获其他人的附加程序中各种 addError() 调用的输出(理想到文件附加程序),但我不希望有关应用程序启动的所有详细信息。

最佳答案

这看起来很有希望,但没有设置来控制控制台消息的粒度。

<configuration debug="false">
<statusListener class="ch.qos.logback.core.status.OnErrorConsoleStatusListener" />
...

相反,我创建了一个自定义概念验证状态监听器:

import ch.qos.logback.core.status.Status;
import ch.qos.logback.core.status.StatusListener;

public class ErrorStatusListener implements StatusListener
{
@Override
public void addStatusEvent( Status status )
{
if ( status.getLevel() >= Status.WARN )
System.out.println( status.getMessage() );
}
}

现在我也可以控制错误消息,并将它们反馈到 Logback 管道中,以便由文件附加程序获取。

17:40:32.336 INFO  [main] [SlackAppenderTest] [simulateAPITrouble:21] - This will fail to post to Slack
Slack POST request failed: 404, no_team

关于java - 将阈值过滤器添加到 Logback <configuration debug ="true"> 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com