繁体   English   中英

如何在 spring-boot 中禁用控制台日志记录?

[英]How to disable console logging in spring-boot?

我正在使用spring-boot的默认日志记录配置。

如何防止控制台输出,同时保持登录到使用logging.file=myfile.log配置的日志文件?

我的目标是没有控制台窗口输出,而只记录到该文件。

无需创建特定的logback.xml配置。 因为我使用spring-boot不必自己配置日志记录。

事实证明,如果我将以下属性设置为空,控制台日志记录将被禁用:

logging.pattern.console=

或者如果你使用它在xml中评论

  <!--<root level="error">-->
        <!--<appender-ref ref="console"/>-->
    <!--</root>-->

我创建了一个名为 logback.xml 的文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
    <logger name="org.hibernate" level="ERROR"/>
</configuration>

有关更多信息,请参阅此链接: https : //www.mkyong.com/spring-boot/spring-boot-test-how-to-stop-debug-logs/

  1. 在应用程序的 /resources/ 文件夹中创建文件 logback-spring.xml。
  2. 将以下内容复制到 logback-spring.xml 中
<?xml version = "1.0" encoding = "UTF-8"?>
<configuration>
    <appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%m%n</pattern>
        </encoder>
    </appender>
    <root level = "INFO">
        <appender-ref ref = "STDOUT"/>
    </root>
</configuration>

所有支持的日志系统都可以在 Spring Environment 中使用 'logging.level.*=LEVEL' 设置记录器级别,其中 'LEVEL' 是 TRACE、DEBUG、INFO、WARN、ERROR、FATAL、OFF 之一。 可以使用logging.level.root配置根记录器。 示例 application.properties:

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

检查此信息: https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

您还可以覆盖logback配置并为其提供值OFF

<configuration>
  <!-- turn OFF all logging (children can override) -->
  <root level="OFF">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

您可以在以下链接中找到更多信息: https : //logback.qos.ch/manual/configuration.html#rootElement

暂无
暂无

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

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