Java 防止 Spring Boot 将日志打印到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31935151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:48:02  来源:igfitidea点击:

Prevent Spring Boot from printing logs to console

javaspringspring-boot

提问by Karthik

I am using spring boot for my application and I am using default spring boot logging.

我正在为我的应用程序使用 spring boot,并且我使用默认的 spring boot 日志记录。

In my application.properties, I have added file path for logging.file,

在我的application.properties,我已经添加了文件路径logging.file

        logging.file= ${logger_path}

and my pom.xmlcontains

我的pom.xml包含

       <logger_path>/tmp/app.log</logger_path>

When I start the application, it prints the logging messages to the file at /tmp/app.log, but the problem is it also prints the log messages on the console. I really don't understand why it is printing on console (though it is printing them to the specified file) when I have specified a log file.

当我启动应用程序时,它会将日志消息打印到文件中/tmp/app.log,但问题是它还在控制台上打印日志消息。我真的不明白为什么它是在控制台打印(虽然这是他们打印到指定的文件)时,我已经指定了log file

Is there any configuration to prevent spring boot from printing the log messages to console?

是否有任何配置可以防止 spring boot 将日志消息打印到控制台?

采纳答案by hovanessyan

Spring boot comes with build-in logbacklogger, which is configured to print to the console by default.

Spring Boot 带有内置logback记录器,默认情况下它被配置为打印到控制台。

You need to overwrite the logbackconfiguration (providing your own logback.xmlon the classpath). This is described here- - section 66.1

您需要覆盖logback配置(logback.xml在类路径上提供您自己的配置)。这在此处描述- - 第 66.1 节

How to disable logback logging read here-

如何在此处禁用 logback 日志记录-

as you can see you have to provide the value OFF...something like:

正如您所看到的,您必须提供价值OFF……例如:

<configuration>
  <include resource="base.xml" />
   .
   .
  <property name="root.level.console" value="OFF" />
</configuration>

Note:Keep in mind that the general idea here is that the logback configuration coming from spring-boot is minimal. The intention is that you provide your own logback configuration and completely overwrite the existing one - e.g. providing your own logback configuration with only log file-appender configured - this should be your general approach.

注意:请记住,这里的总体思路是来自 spring-boot 的 logback 配置是最小的。目的是您提供自己的 logback 配置并完全覆盖现有的配置 - 例如,提供您自己的 logback 配置,仅配置了 log file-appender - 这应该是您的通用方法。

回答by Kaliappan

I tried removing console configuration from logback.xml. But, It was still logging in console. So What I did is, I just removed the appender being added in the logging configuration by springboot. Thereby, we can stop springboot logging in console and separate log file. Add the below lines at the end of your application specific appenders are added. Your custom appender should not match any of these appender names. It worked for me.

我尝试从 logback.xml 中删除控制台配置。但是,它仍在登录控制台。所以我所做的是,我只是删除了 springboot 在日志配置中添加的附加程序。从而,我们可以在控制台中停止 springboot 登录并分离日志文件。在添加了应用程序特定附加程序的末尾添加以下行。您的自定义 appender 不应与这些 appender 名称中的任何一个匹配。它对我有用。

// get instance of your log4j instance
Logger logger = LogManager.getRootLogger();
logger.removeAppender("CONSOLE"); // stops console logging
logger.removeAppender("LOGFILE"); // stops file logging

回答by Maximilian Wollnik

A similar discussion can be found here.

可以在此处找到类似的讨论。

My logback.xml looks like that:

我的 logback.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>

Just in case you still experience an issue: I have read many discussions about that topic and it was not working at all for me. Unfortunately I have made a really stupid mistake when I have created the file logback.xml for the very first time: A space was added at the beginning of the filename. Therefore the file was never used. So just have another look on the filename, add the file in "src/main/resources" and remove any "logging.config" entries from your property files.

以防万一您仍然遇到问题:我已经阅读了许多关于该主题的讨论,但它对我来说根本不起作用。不幸的是,当我第一次创建文件 logback.xml 时,我犯了一个非常愚蠢的错误:在文件名的开头添加了一个空格。因此从未使用过该文件。因此,只需再看一下文件名,将文件添加到“src/main/resources”并从您的属性文件中删除所有“logging.config”条目。

回答by Pratap A.K

Tested with latest 1.3.1 release, with newer releases base.xml has been renamed to defaults.xml

使用最新的 1.3.1 版本进行测试,更新版本 base.xml 已重命名为 defaults.xml

Answer here

在这里回答

回答by panksy2k

Include file-appender.xmland not console-appenderwith following configuration in logback-spring.xml:

包括但file-appender.xmlconsole-appender包括以下配置logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/defaults.xml" />
        <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
        <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
        <root level="INFO">
            <appender-ref ref="FILE" />
        </root>
    </configuration>

You also need to add logging.fileto your application.properties

您还需要添加logging.file到您的application.properties

This is in compliance to what is mentioned in spring boot documentation - http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

这符合 spring boot 文档中提到的内容 - http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html