禁止所有 Logback 输出到控制台?
如何配置 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 中运行。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这些消息仅在以下至少一项为真时显示:
纠正问题,这些消息应该消失。
Those messages only show if at least one of the following is true:
Correct the issue and those messages should go away.
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.
这是一个“我也是”的答案,抱歉!
令人高兴的是,我在下面找到了解决方案(请参阅更新)。
与其他一些答案相反,尽管没有
ERROR
或WARN
,我还是收到了 LogBack 配置INFO
消息流在配置阶段。这是我的消息:
这是我的配置:
这是我不想要的垃圾邮件,我认为自己没有挑起它,并且我希望得到一些帮助来摆脱它。
我可能“有罪”的一方面是我在静态变量中初始化我的记录器;文档建议使用实例变量。
版本:
更新
终于知道问题出在哪里了!
来自精美手册(以及Thorbjørn 的回答):
我的错误是
回想起来,呃!希望这些信息对其他人有帮助。
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 noERROR
s orWARN
s in the configuration phase.Here are my messages:
Here's my 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:
UPDATE
Finally figured out what the problem was!
From the fine manual (and Thorbjørn's answer):
My mistake was
In retrospect, duh! Hopefully this information will help others.
只需使用 NopStatusLinstener 类,这将停止 logback 的自记录。
Simplely use NopStatusLinstener class and this will stop self logging of logback.
所以我遇到了同样的问题,但发现删除不正确的 就可以了。该条目在 0.9.4 左右已被弃用,并且消息消失了...
您的附加程序应该看起来像
我在博客中写过的更多 完整对我所做的更改的对我有用的描述
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
I've blogged about a more complete description of what I changed that worked for me
我对 Logback 不熟悉。但如果它打印到
System.out
或System.err
,这些只是System
中的公共静态PrintStream
变量> 类。您可以子类化PrintStream
并将系统输出变量设置为您的子类,从而控制它的工作方式。例如:(
此代码未经测试)
I'm not familiar with Logback. But if it's printing to
System.out
orSystem.err
, these are simply public staticPrintStream
variables in theSystem
class. You could subclassPrintStream
and set the system output variables to your subclass, thus controlling how it works.For example:
(This code is untested)
就我而言,我在一个依赖项目中拥有“logback-test.xml”,该项目被部署为 webapp jar。
“logback-test.xml”文件以
The 'scan="true"' attribute generated this error:
开头,这导致了 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
The 'scan="true"' attribute generated this error:
which resulted in 67 (!) more INFO lines.
By removing the 'scan="true"' attribute, the logback log disappeared altogether.
实际上,多次报告相同的 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.
您很可能在 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.
我只想添加有关 logback 1.0.2 中添加的默认标头消息的信息:
我发现在 logback news,不过确实很难找。
如果您想删除此消息,则必须将
outputPatternAsPresentationHeader
设置为 false:I just want to add information about default header message added in logback 1.0.2:
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:我在 Spring Boot 应用程序中收到以下错误日志:
我通过以下任一方式抑制它们:
logback.xml
重命名为logback-spring.xml
application 中定义.properties
:logging.config=classpath:logback.xml
关于此主题的 Spring 文档:
I got the following error logs in my spring boot application:
I suppressed them by either:
logback.xml
tologback-spring.xml
application.properties
:logging.config=classpath:logback.xml
Spring documentation on this topic: