1. Overview
1.概述
Usually, console logs give us the opportunity to debug our system in an easy and intuitive way. Nevertheless, there are occasions when we don’t want to enable this feature in our system.
通常情况下,控制台日志让我们有机会以一种简单直观的方式来调试我们的系统。然而,在某些情况下,我们并不想在系统中启用这一功能。
In this quick tutorial, we’ll see how to avoid logging to the console when running a Spring Boot application.
在这个快速教程中,我们将看到如何在运行Spring Boot应用程序时避免向控制台提交日志。
We’ll keep it simple with straight-to-the-point examples showing how to achieve this, whether we are using Logback, Log4js2, or even the Java Util Logging framework.
我们将通过直截了当的例子来展示如何实现这一目标,无论我们使用的是Logback、Log4js2,甚至是Java Util Logging框架,都将保持简单。
To learn more about logging frameworks for Spring Boot, we suggest having a look at our Logging in Spring Boot tutorial.
要了解有关Spring Boot的日志框架的更多信息,我们建议查看我们的Spring Boot中的日志教程。
2. How to Disable the Console Output for Logback
2.如何禁用日志回传的控制台输出
If our project uses Spring Boot starters, then the spring-boot-starter-logging dependency will be included as well.
如果我们的项目使用Spring Boot starters,那么spring-boot-starter-logging依赖项也将被包含。
This particular starter configures Logback as the default framework and initially logs only to the console by default.
这个特殊的启动器将Logback配置为默认框架,并且最初默认只记录到控制台。
This configuration can be customized by adding a logback-spring.xml file to our resources.
这种配置可以 通过在我们的资源中添加一个logback-spring.xml文件来进行定制。
For example, let’s set up the XML so as to disable the console output and log only to a file:
例如,让我们设置XML,以便禁用控制台输出,只记录到一个文件。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource=
"org/springframework/boot/logging/logback/defaults.xml" />
<include resource=
"org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Additionally, we’ll need the logging.file configuration property in our application.properties file:
此外,我们需要在application.properties文件中设置logging.file配置属性。
logging.file=baeldung-disabled-console.log
Note: what is actually disabling the console output here is the fact that we aren’t including the console-appender.xml in our XML file, so an empty configuration tag would also do the trick.
注意:这里实际上是禁用了控制台输出,因为我们没有把console-appender.xml包括在我们的XML文件中,所以一个空的configuration标签也会起到作用。
Alternatively, we can avoid creating the XML file by overriding the default configuration with application properties.
另外,我们可以通过用应用程序属性覆盖默认配置来避免创建XML文件。
For example, we can potentially make use of the logging.pattern.console property:
例如,我们可以潜在地利用logging.pattern.console属性。
logging.pattern.console=
This property is translated to the CONSOLE_LOG_PATTERN system property, which is then used by the Spring default console configuration.
这个属性被翻译成CONSOLE_LOG_PATTERN系统属性,然后被Spring默认控制台配置使用。
This approach, of course, isn’t as clean and solid as the previous one. It’s not the intended purpose of the property, thus this ‘hack’ might not be supported by Logback at some point.
当然,这种方法并不像之前的方法那样干净和牢固。这不是该属性的预期目的,因此这个’黑客’可能在某个时候不被Logback支持。
Furthermore, we can disable all logging activity by setting the value of the root logger level to OFF:
此外,我们可以通过将根记录器级别的值设置为OFF来禁用所有记录活动。
logging.level.root=OFF
3. How to Avoid Logging in the Console with Log4j2
3.如何避免用Log4j2在控制台中进行记录
As we may know, Log4j2 supports XML, JSON, YAML, or properties formats to configure its logging behavior.
我们可能知道,Log4j2支持XML、JSON、YAML或属性格式来配置其日志行为。
For the sake of simplicity, we’ll just show a simple example of a log4j2.xml file this time.
为了简单起见,这次我们只展示一个log4j2.xml文件的简单例子。
The other formats respect the same configuration structure:
其他格式尊重相同的配置结构。
<Configuration status="INFO">
<Appenders>
<File name="MyFile" fileName="baeldung.log"
immediateFlush="true" append="false">
<PatternLayout pattern=
"%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
As with the Logback setup, the reason why the framework avoids logging to the console is not the configuration ‘per se’, but the fact that the Root Logger doesn’t contain a reference to a Console Appender.
和Logback的设置一样,框架避免向控制台记录的原因不是配置 “本身”,而是根记录器不包含对控制台应用者的引用。
4. How to Disable Console Logging for Java Util Logging
4.如何禁用Java Util日志的控制台日志记录
Java Util Logging (or simply ‘JUL’) might not be the most popular logging solution for Spring Boot applications nowadays.
Java Util Logging(或简称 “JUL”)可能不是现在Spring Boot应用中最流行的日志解决方案。
Anyway, we’ll analyze how we can get rid of console logs, in case the framework is present in our project.
无论如何,我们将分析如何摆脱控制台日志,如果该框架存在于我们的项目中的话。
All we have to do is add the following values to the default logging.properties in our resources folder:
我们所要做的就是在我们的资源文件夹中的默认logging.properties中添加以下值。
handlers=java.util.logging.FileHandler
java.util.logging.FileHandler.pattern=baeldung.log
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
And include the logging.file property in our application.properties file. Any value will do the trick:
并在我们的application.properties文件中包括logging.file属性。任何值都能起到作用。
logging.file=true
5. Conclusion
5.总结
With these short examples, we can now disable console logs in our application in a hassle-free manner, no matter what logging framework we’re using.
有了这些简短的例子,我们现在可以无忧无虑地在我们的应用程序中禁用控制台日志了,无论我们使用的是什么日志框架。
As always, we can find the examples’ implementations over on Github.
一如既往,我们可以在Github上找到这些例子的实现,。