如何在Spring Boot中禁用Logback ConsoleAppender [英] How to disable logback ConsoleAppender in Spring Boot

查看:115
本文介绍了如何在Spring Boot中禁用Logback ConsoleAppender的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot构建命令行应用程序.在这样的应用程序中,不适合使用logback控制台日志记录.如何完全禁用控制台附加程序,但仍使文件附加程序与

I am building a command line application using Spring Boot. In such an application, logback console logging is not appropriate. How can I completely disable the console appender, but still have the file appender working with the default Spring Boot support?

我创建了一个功能请求,以在此处更简单地支持此功能:

I have created a feature request for simpler support of this feature here:

https://github.com/spring-projects/spring-boot/issues/1612

推荐答案

只需在src/main/resources中添加一个名为logback.xml的文件,其内容如下所示(逐字复制,但Spring Boot来源的控制台部分除外):

Just add a file called logback.xml in src/main/resources with content like (copied verbatim except for console part from Spring Boot's source):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>


    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex"/>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>

</configuration>

请注意

<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>

为了支持从Spring Boot的logging.filelogging.path设置日志文件,需要

.

is needed in order to support setting the log file from Spring Boot's logging.file and logging.path.

如果您要做的只是设置一些标准日志文件,则可以在上面的属性中设置其路径.

If all you want to do is set some standard log file, you could set place it's path in property above.

更新(2015年2月4日)

在较新版本的Spring Boot中,您可以轻松地从Spring Boot中包含base.xml并创建以下logback.xml.

In newer versions of Spring Boot you can easily just include the base.xml from Spring Boot and create the following logback.xml.

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

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

更新(15-09-2017)

为了在Spring Boot 1.5.x和2.0.0.M4上运行,我添加了一个名为logback-spring.xml的文件并将其添加到resources目录中. 该文件可能看起来像这样

In order to get this working on Spring Boot 1.5.x and 2.0.0.M4 I added a file called logback-spring.xml and added it in the resources directory. The file could look like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>


    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n"/>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>

</configuration>

这篇关于如何在Spring Boot中禁用Logback ConsoleAppender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆