在Logback <configuration debug =“ true”>输出中添加阈值过滤器?

时间:2019-05-01 23:16:32

标签: java logback

有什么想法如何对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()调用的输出(理想情况下是文件附加程序的理想),但是我不希望获得所有详细信息应用启动。

1 个答案:

答案 0 :(得分:0)

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

<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