简体   繁体   English

Spring Boot:如何禁用 Tomcat 启动日志记录?

[英]Spring Boot: How to disable Tomcat startup logging?

I'm using Spring Boot 2.0.x with Logback.我正在使用带有 Logback 的 Spring Boot 2.0.x。 On startup of my application (using an embedded tomcat), I see several INFORMATION log messages (written to standard error) which apparently originate directly from the embedded tomcat.在我的应用程序启动时(使用嵌入式 tomcat),我看到几个INFORMATION日志消息(写入标准错误),这些消息显然直接来自嵌入式 tomcat。

In contrast to the rest of all my logging, these messages seem to not be written by Logback.与我所有日志记录的其余部分相比,这些消息似乎不是由 Logback 编写的。 The messages have the following content:消息内容如下:

Jan 08, 2019 3:13:00 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["http-nio-8080"]
Jan 08, 2019 3:13:00 PM org.apache.catalina.core.StandardService startInternal
INFORMATION: Starting service [Tomcat]
Jan 08, 2019 3:13:00 PM org.apache.catalina.core.StandardEngine startInternal
INFORMATION: Starting Servlet Engine: Apache Tomcat/8.5.34
Jan 08, 2019 3:13:01 PM org.apache.catalina.core.ApplicationContext log
INFORMATION: Initializing Spring embedded WebApplicationContext

I'm not interested in any of this, it's just noise.我对这些都不感兴趣,这只是噪音。 How do I get rid of those logs?我如何摆脱这些日志? I tried all sorts of solutions, but nothing worked so far.我尝试了各种解决方案,但到目前为止没有任何效果。

Okay, it took me about 5 hours to figure this out:好的,我花了大约 5 个小时才弄明白:

  • Tomcat uses JULI , a derivative of java.util.logging Tomcat 使用JULI ,它是java.util.logging的衍生物
  • JULI itself is compatible with java.util.logging , so you can configure it the same way JULI 本身与java.util.logging兼容,所以你可以用同样的方式配置它
  • When starting up, the embedded tomcat will not care about any of your logging settings (at least it did not for me)启动时,嵌入式 tomcat 不会关心您的任何日志记录设置(至少对我来说不是)

The up-shot is: before I start my spring-embedded tomcat, I had to do this:结果是:在我开始我的 spring-embedded tomcat 之前,我必须这样做:

java.util.logging.Logger.getLogger("org.apache").setLevel(java.util.logging.Level.WARNING);

This will instruct java.util.logging (implemented by JULI) to only propagate warnings to the console.这将指示java.util.logging (由 JULI 实现)仅将警告传播到控制台。 This eliminates all the startup noise by tomcat, and all other logging will be performed by the logging framework of your choice anyways (logback in my case), so this should not affect your standard logging at all.这消除了 tomcat 的所有启动噪音,并且所有其他日志记录都将由您选择的日志记录框架执行(在我的情况下为 logback),因此这根本不会影响您的标准日志记录。

You can set the following in the application.properties :您可以在application.properties设置以下内容:

logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat logging.level.tomcat=OFF logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat logging.level.tomcat=OFF

Since the embedded Tomcat uses the JUL, this will set the JUL logging level regarding the Tomcat related classes to OFF .由于嵌入式 Tomcat 使用 JUL,这会将有关 Tomcat 相关类的 JUL 日志记录级别设置为OFF

The Apache juli logs can be omitted by setting java.util.logging formatter system property to empty String before SpringBootApplication start like this Apache juli 日志可以通过在 SpringBootApplication 启动之前将 java.util.logging 格式化程序系统属性设置为空字符串来省略

System.setProperty("java.util.logging.SimpleFormatter.format", ""); System.setProperty("java.util.logging.SimpleFormatter.format", "");

Look at the Loggername (in case of Log4j(2)) appearing in the console.查看出现在控制台中的Loggername (在 Log4j(2) 的情况下)。

Then, copy the name and prepend it to logging.level.然后,复制名称并将其添加到logging.level。 in you application.properties file.在您的application.properties文件中。

In the screenshot below, if I uncomment the line of DispatcherServlet the logging goes away:在下面的屏幕截图中,如果我取消注释DispatcherServlet的行,日志记录就会消失:

在此处输入图片说明

A slightly less obtrusive method is to specify a JUL configuration properties file as system property (cf. JUL JavaDoc and How to set up java logging using a properties file? (java.util.logging) ) and merely reduce the log level as required in that.一种稍微不那么突兀的方法是将 JUL 配置属性文件指定为系统属性(参见JUL JavaDoc如何使用属性文件设置 java 日志记录?(java.util.logging) ),并仅根据需要降低日志级别那。

So add such a system property: -Djava.util.logging.config.file=logging.properties所以添加这样一个系统属性: -Djava.util.logging.config.file=logging.properties

And use a configuration like this in that file:并在该文件中使用这样的配置:

.level=WARNING
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=WARNING

Using WARNING will silence all Tomcat startup logging but still show any problems.使用WARNING将使所有 Tomcat 启动日志静音,但仍会显示任何问题。 Make sure the file location is correct, I found that JUL just goes completely silent without giving any hints if it can't be found.确保文件位置正确,我发现 JUL 会完全静音,如果找不到,则不会给出任何提示。

Yet another approach would be to re-route JUL logs to log4J or Logback, cf.另一种方法是将 JUL 日志重新路由到 log4J 或 Logback,参见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM