禁止所有 Logback 输出到控制台?

发布于 2024-09-12 21:46:45 字数 1311 浏览 9 评论 0 原文

如何配置 Logback 以抑制其所有到控制台的输出(标准输出)?特别是,我希望抑制(或重定向)Logback 自己的日志消息,如下所示:

16:50:25,814 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
16:50:25,814 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml]
16:50:25,816 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
16:50:25,816 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [file:/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml]
16:50:25,816 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [file:/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml]
16:50:25,923 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
16:50:25,924 |-INFO in ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter@1a15291 - Will scan for changes in file [/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml] every 60 seconds. 

我需要禁用所有到标准输出的日志记录,因为我们的生产环境不允许应用程序将任何消息打印到标准输出。

注意我使用的是 Logback 0.9.21、SLF4J 1.6.0,我们的应用程序在 WebLogic 10.3.2 中运行。

How can I configure Logback to suppress all of its output to the console (standard output)? In particular, I wish to suppress (or redirect) Logback's own log messages such as the following:

16:50:25,814 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
16:50:25,814 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml]
16:50:25,816 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
16:50:25,816 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [file:/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml]
16:50:25,816 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [file:/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml]
16:50:25,923 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
16:50:25,924 |-INFO in ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter@1a15291 - Will scan for changes in file [/opt/dap/domains/ap0491/uat1/domain/instance-config/logback.xml] every 60 seconds. 

I need to disable all logging to standard output because our production environment disallows applications from printing any messages to standard output.

Note I'm using Logback 0.9.21, SLF4J 1.6.0, and our application runs in WebLogic 10.3.2.

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

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

发布评论

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

评论(11

听风吹 2024-09-19 21:46:45

这些消息仅在以下至少一项为真时显示:

  • 您在 logback.xml 文件中启用了调试
  • 您的配置中有错误。这里就是这种情况 - logback 抱怨找到多个配置文件。
  • 如果您的环境提供了冲突的文件,则存在类路径问题。 (这是我昨天想到的,也是这个问题的真正原因)。
  • (logback 中有一个错误 - 已经发生)

纠正问题,这些消息应该消失。

Those messages only show if at least one of the following is true:

  • you have debugging enabled in the logback.xml file
  • you have an error in your configuration. That is the case here - logback complains about multiple configuration files found.
  • there is a classpath problem if your environment provides conflicting files. (this one occurred to me yesterday and was the real cause of this question).
  • (there is a bug in logback - has happened)

Correct the issue and those messages should go away.

孤云独去闲 2024-09-19 21:46:45

Holger Hoffstätte 在他的诊断中是正确的,重复的类路径条目该消息是 Logback 计算类路径条目的方式中的错误的症状。 Robert Elliot 还在描述了该问题的线程 用户邮件列表。根据 Robert 和其他人在 SLF4J 邮件列表上的相关讨论,当使用 Logback 的应用程序在 WebLogic 容器中运行,由于 WebLogic 类加载器的运行方式,Logback 会报告 logback.xml 配置文件的重复类路径条目。然而,无论 WebLogic 类加载器是否应该只报告唯一的类路径条目,Logback 肯定应该只计算唯一的类路径条目,以便它不会打印这种令人困惑的虚假消息。

我已经为 修复 .ch/browse/LBCLASSIC-159" rel="nofollow noreferrer">LBCLASSIC-159 基本上按照 Robert Elliot 的建议进行操作,并使用集合而不是列表来保存类加载器返回的资源,从而有效地消除了任何重复的类路径资源。我已成功使用 Logback 0.9.24、SLF4J 1.6.1 和 WebLogic 10.3.2 测试了该修复。正如 Thorbjørn 在他的答案中预测的那样,修复了此问题Logback 不再将重复的类路径条目状态消息(或任何其他信息性消息)显示到标准输出。

我希望维护人员将我的修复集成到主 Logback 源代码存储库 中,并将其包含在下一个版本。

Holger Hoffstätte was correct in his diagnosis that the duplicate classpath entry message is a symptom of a bug in how Logback counts classpath entries. Robert Elliot also characterized the problem in a thread on the Logback user mailing list. According to Robert and others in this related disussion on the SLF4J mailing list, when an application that uses Logback runs in a WebLogic container, due to the way the WebLogic classloader operates, Logback reports duplicate classpath entries for the logback.xml configuration file. However, regardless of whether the WebLogic classloader should or should not report only unique classpath entries, Logback should certainly count only unique classpath entries so that it does not print this confusing, spurious message.

I have implemented a fix for LBCLASSIC-159 that essentially does what Robert Elliot recommends and uses a set instead of a list to hold the resources that the classloader returns, effectively eliminating any duplicate classpath resources. I have successfully tested the fix with Logback 0.9.24, SLF4J 1.6.1, and WebLogic 10.3.2. As Thorbjørn predicted in his answer, with this fix in place, Logback no longer displays the duplicate classpath entry status messages (or any of the other informational messages) to standard output.

I hope that the maintainers will integrate my fix into the main Logback source code repository and include it in the next release.

空城仅有旧梦在 2024-09-19 21:46:45

这是一个“我也是”的答案,抱歉!

令人高兴的是,我在下面找到了解决方案(请参阅更新)。

与其他一些答案相反,尽管没有 ERRORWARN ,我还是收到了 LogBack 配置 INFO 消息流在配置阶段。

这是我的消息:

13:39:20,457 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:39:20,457 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:39:20,457 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/carl/workspace-LSY/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/IceProfile/WEB-INF/classes/logback.xml]
13:39:20,496 |-INFO in ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter@14e2c9c - Will scan for changes in file [/home/carl/workspace-LSY/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/IceProfile/WEB-INF/classes/logback.xml] every 60 seconds. 
13:39:20,496 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
13:39:20,497 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:39:20,501 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:39:20,510 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:39:20,510 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Pushing component [encoder] on top of the object stack.
13:39:20,537 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
13:39:20,537 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:39:20,538 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [ch.qos.logback] to OFF
13:39:20,538 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [ch.qos.logback] to false
13:39:20,538 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

这是我的配置:

<configuration debug="true" scan="true"> 

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <!-- encoders are  by default assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
  
  <logger name="ch.qos.logback" level="OFF" additivity="false" />
  
</configuration>

这是我不想要的垃圾邮件,我认为自己没有挑起它,并且我希望得到一些帮助来摆脱它。

我可能“有罪”的一方面是我在静态变量中初始化我的记录器;文档建议使用实例变量。

版本:

  • logback-classic-0.9.24.jar
  • logback-core-0.9.24.jar
  • slf4j-api-1.6.1.jar
  • 在 Ubuntu 11.04 下的 Tomcat 6.0 中运行的 IceFaces 2.0 应用程序中执行

更新

终于知道问题出在哪里了!

来自精美手册(以及Thorbjørn 的回答):

在元素内设置调试属性将输出状态信息,假设

  1. 找到配置文件
  2. 配置文件是格式良好的 XML。

我的错误是

<configuration debug="true" scan="true"> 

回想起来,呃!希望这些信息对其他人有帮助。

This is a "me too" answer, sorry!

Happily, I've found a solution (see UPDATE) below.

Contrary to some of the other answers, I'm getting a stream of LogBack configuration INFO messages in spite of having no ERRORs or WARNs in the configuration phase.

Here are my messages:

13:39:20,457 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:39:20,457 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:39:20,457 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/carl/workspace-LSY/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/IceProfile/WEB-INF/classes/logback.xml]
13:39:20,496 |-INFO in ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter@14e2c9c - Will scan for changes in file [/home/carl/workspace-LSY/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/IceProfile/WEB-INF/classes/logback.xml] every 60 seconds. 
13:39:20,496 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
13:39:20,497 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:39:20,501 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:39:20,510 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:39:20,510 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Pushing component [encoder] on top of the object stack.
13:39:20,537 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
13:39:20,537 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:39:20,538 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [ch.qos.logback] to OFF
13:39:20,538 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [ch.qos.logback] to false
13:39:20,538 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

Here's my configuration:

<configuration debug="true" scan="true"> 

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <!-- encoders are  by default assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
  
  <logger name="ch.qos.logback" level="OFF" additivity="false" />
  
</configuration>

This is spam I don't want, I consider myself innocent of having provoked it, and I'd appreciate some help in getting rid of it.

One respect in which I may be "guilty" is that I'm initializing my loggers in a static variable; the docs recommend using instance variables instead.

Versions:

  • logback-classic-0.9.24.jar
  • logback-core-0.9.24.jar
  • slf4j-api-1.6.1.jar
  • executing in an IceFaces 2.0 app running in Tomcat 6.0 under Ubuntu 11.04

UPDATE

Finally figured out what the problem was!

From the fine manual (and Thorbjørn's answer):

Setting the debug attribute within the element will output status information, under the assumption that

  1. the configuration file is found
  2. the configuration file is well-formed XML.

My mistake was

<configuration debug="true" scan="true"> 

In retrospect, duh! Hopefully this information will help others.

一梦浮鱼 2024-09-19 21:46:45
<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
</configuration>

只需使用 NopStatusLinstener 类,这将停止 logback 的自记录。

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

Simplely use NopStatusLinstener class and this will stop self logging of logback.

甜味超标? 2024-09-19 21:46:45

所以我遇到了同样的问题,但发现删除不正确的 就可以了。该条目在 0.9.4 左右已被弃用,并且消息消失了...

您的附加程序应该看起来像

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

 <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  <level>info</level>
 </filter>

 <encoder>
  <pattern>%d{yyyy-MM-dd} %d{HH:mm:ss} %.-1level %thread %logger{36}: %m%n</pattern>
 </encoder>

</appender>

我在博客中写过的更多 完整对我所做的更改的对我有用的描述

So I had the same problem but found that removing the incorrect <layout /> entry which was deprecated somewhere around 0.9.4 and the messages went away...

You appender should look some thing like

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

 <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  <level>info</level>
 </filter>

 <encoder>
  <pattern>%d{yyyy-MM-dd} %d{HH:mm:ss} %.-1level %thread %logger{36}: %m%n</pattern>
 </encoder>

</appender>

I've blogged about a more complete description of what I changed that worked for me

甜宝宝 2024-09-19 21:46:45

我对 Logback 不熟悉。但如果它打印到 System.outSystem.err,这些只是 System 中的公共静态 PrintStream 变量> 类。您可以子类化 PrintStream 并将系统输出变量设置为您的子类,从而控制它的工作方式。

例如:(

public class NOPPrintStream extends PrintStream
{
    public NOPPrintStream() { super((OutputStream)null); }

    public void println(String s) { /* Do nothing */ }
    // You may or may not have to override other methods
}

public class MyClass
{
    public static void main(String[] args)
    {
        System.out = new NOPPrintStream();
        // Start program
    }
}

此代码未经测试)

I'm not familiar with Logback. But if it's printing to System.out or System.err, these are simply public static PrintStream variables in the System class. You could subclass PrintStream and set the system output variables to your subclass, thus controlling how it works.

For example:

public class NOPPrintStream extends PrintStream
{
    public NOPPrintStream() { super((OutputStream)null); }

    public void println(String s) { /* Do nothing */ }
    // You may or may not have to override other methods
}

public class MyClass
{
    public static void main(String[] args)
    {
        System.out = new NOPPrintStream();
        // Start program
    }
}

(This code is untested)

回忆追雨的时光 2024-09-19 21:46:45

就我而言,我在一个依赖项目中拥有“logback-test.xml”,该项目被部署为 webapp jar。
“logback-test.xml”文件以

<configuration debug="false" scan="true">

The 'scan="true"' attribute generated this error:

ERROR in ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter@716de067 - URL [jar:file:/C:/Local...cut.../WEB-INF/lib/My.Dev.jar!/logback-test.xml] is not of type file

开头,这导致了 67 (!) 更多 INFO 行。

通过删除“scan=true”属性,logback 日志完全消失。

In my case, I had the "logback-test.xml" in a dependent project that was being deployed as a webapp jar.
The "logback-test.xml" file started with

<configuration debug="false" scan="true">

The 'scan="true"' attribute generated this error:

ERROR in ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter@716de067 - URL [jar:file:/C:/Local...cut.../WEB-INF/lib/My.Dev.jar!/logback-test.xml] is not of type file

which resulted in 67 (!) more INFO lines.

By removing the 'scan="true"' attribute, the logback log disappeared altogether.

半窗疏影 2024-09-19 21:46:45

实际上,多次报告相同的 logback.xml 位置这一事实似乎更像是 logback 中的一个错误,而不是其他任何事情。将此报告给 logback JIRA(此处),或者首先检查有问题的 jar 是否已打开类路径多次。

Actually the fact that the same logback.xml location is reported multiple times seems more like a bug in logback than anything else. Either report this to the logback JIRA (here) or first check whether the jar in question is on the classpath multiple times.

半山落雨半山空 2024-09-19 21:46:45

您很可能在 logback.xml 中配置了一个元素。如果您不希望任何有关日志记录框架本身状态的控制台更新,您可以将其删除。不过,logback 框架建议不要出于故障排除目的禁用它。

有一个名为 StatusListenerAsList 的控制台侦听器的替代方案,它将状态消息保存为私有列表。如果需要,您可以通过 JMX 公开它,并使用一些代码。

You most likely have a element configured in your logback.xml. You can remove it, if you don't want any console updates about the state of the logging framework itself. Though, logback framework recommends against disabling it for troubleshooting purposes.

There is an alternative to the console listener called StatusListenerAsList which keeps the status messages as a private list. You can expose it via JMX, if needed, with a little bit of code.

烟柳画桥 2024-09-19 21:46:45

我只想添加有关 logback 1.0.2 中添加的默认标头消息的信息:

#logback.classic pattern: %d [%thread] %-5level %logger{36} - %msg%n

我发现在 logback news,不过确实很难找。

如果您想删除此消息,则必须将 outputPatternAsPresentationHeader 设置为 false:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
        <!-- do not print pattern as a header -->
        <outputPatternAsPresentationHeader>false</outputPatternAsPresentationHeader>
    </encoder>
</appender>

I just want to add information about default header message added in logback 1.0.2:

#logback.classic pattern: %d [%thread] %-5level %logger{36} - %msg%n

I found that in logback news, but it was really difficult to find.

If you wish to remove this message, you have to set up outputPatternAsPresentationHeader to false:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
        <!-- do not print pattern as a header -->
        <outputPatternAsPresentationHeader>false</outputPatternAsPresentationHeader>
    </encoder>
</appender>
凡间太子 2024-09-19 21:46:45

我在 Spring Boot 应用程序中收到以下错误日志:

14:37:36,455 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:46 - no applicable action for [springProfile], current ElementPath  is [[configuration][springProfile]]
14:37:36,456 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:92 - no applicable action for [include], current ElementPath  is [[configuration][springProfile][include]]

我通过以下任一方式抑制它们:

  • logback.xml 重命名为 logback-spring.xml
  • 或在 application 中定义.properties: logging.config=classpath:logback.xml

关于此主题的 Spring 文档:
弹簧描述

I got the following error logs in my spring boot application:

14:37:36,455 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:46 - no applicable action for [springProfile], current ElementPath  is [[configuration][springProfile]]
14:37:36,456 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:92 - no applicable action for [include], current ElementPath  is [[configuration][springProfile][include]]

I suppressed them by either:

  • renaming logback.xml to logback-spring.xml
  • or, define in application.properties: logging.config=classpath:logback.xml

Spring documentation on this topic:
spring description

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