抑制来自logback的信息消息

时间:2016-12-21 18:57:43

标签: logback slf4j

应用程序我的工作将被创建为jar文件并通过命令行运行它。没有涉及应用程序服务器。 当我运行jar文件时,来自logback的信息将打印在控制台上。如何禁止从logback包中记录所有日志记录

13:46:30,534 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:46:30,534 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:46:30,534 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/util/target/classes/logback.xml]
13:46:30,588 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:46:35,598 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:46:35,605 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [stdout]
13:46:35,621 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:46:35,629 |-WARN in ch.qos.logback.classic.encoder.PatternLayoutEncoder@326de728 - [outputPatternAsPresentationHeader] property is deprecated. Please use [outputPatternAsHeader] option instead.
13:46:35,661 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.springframework] to false
13:46:35,661 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - org.springframework level set to ERROR
13:46:35,661 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [stdout] to Logger[org.springframework]
13:46:35,661 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [ch.qos.logback] to OFF
13:46:35,661 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [ch.qos.logback] to false
13:46:35,665 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:27 - no applicable action for [priority], current ElementPath  is [[configuration][root][priority]]
13:46:35,666 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [stdout] to Logger[ROOT]
13:46:35,666 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:46:35,666 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@25618e91 - Registering current configuration as safe fallback point

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender" >
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework" additivity="false">
        <level value="error"/>
        <appender-ref ref="stdout"/>
    </logger>

    <logger name="ch.qos.logback" level="OFF" additivity="false" />

    <root>
        <priority value="warn"/>
        <appender-ref ref="stdout"/>
    </root>

</configuration>

尝试了所有选项以及线程Suppress all Logback output to console?的讨论。

没有任何帮助,任何指针都会有所帮助。我在logback版本1.0.13和slf4j 1.7.5

1 个答案:

答案 0 :(得分:1)

首先,正确的方式。

解决WARN/ERROR问题。

这是相关的logback docs。如果在init期间出现任何ERRORWARN级问题,则在回归初始化期间收集的状态消息将打印到控制台。因此,摆脱控制台输出的一个好方法是修复您收到的ERRORWARN消息。

您可以查看StaticLoggerBinder的源代码。

 if(!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
   StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
 }

它不会通过您的注册记录器,因为这些是在初始化回写之前生成的状态消息。它是一个catch-22,您需要一个正确配置的回溯上下文来记录有关它无法启动的错误消息。

您可以更改使用这些控制台消息所做的操作,但这可能是您唯一能够了解日志记录无效的原因。

在配置中修复的一件事是将根目录更改为

<root level="WARN">
    <appender-ref ref="stdout"/>
</root>

有一条错误消息告诉您优先级不是有效元素

13:46:35,665 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:27 - no applicable action for [priority], current ElementPath  is [[configuration][root][priority]]

修改或禁止状态消息记录

在logback.xml中设置statusListener

<configuration>
    <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  
</configuration>

logback提供的选项是:

  • NopStatusListener:什么都不做
  • OnConsoleStatusListener:将所有状态消息记录到stdout
  • OnErrorConsoleStatusListener:将所有状态消息记录到stderr
  • StatusListenerAsList:收集列表中的状态供您稍后访问