如何在Spring Boot中禁用logback ConsoleAppender

时间:2014-09-24 14:22:32

标签: logback spring-boot

我正在使用Spring Boot构建命令行应用程序。在此类应用程序中,日志记录控制台日志记录不合适。如何完全禁用控制台appender,但文件appender仍然使用default Spring Boot support

更新

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

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

5 个答案:

答案 0 :(得分:10)

只需在logback.xml中添加一个名为src/main/resources的文件,其内容类似于(除了Spring Boot&s; s来源的控制台部分之外,逐字复制):

<?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设置日志文件。

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

更新(02-04-2015)

在较新版本的Spring Boot中,您可以轻松地从Spring Boot中包含base.xml并创建以下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目录中。 该文件可能如下所示

<?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>

答案 1 :(得分:3)

使用最新的1.3.1 spring boot release进行测试..

将logback.xml放在资源文件夹

logback.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" />

    <!-- Send debug messages to a file "application.log" -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <file>application.log</file>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>application.%i.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

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

如果您之前添加了排除项,请确保从“spring-boot-starter”和“spring-boot-starter-web”依赖项中删除spring-boot-starter-logging的排除项。

不需要以下依赖

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.2</version>
</dependency>


<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.2</version>
</dependency>

application.properties

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

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

将以下内容添加到您的application.properties

logging.level.root=OFF
spring.main.banner-mode=OFF

答案 4 :(得分:0)

file-appender.xml中使用以下配置包括console-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>

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

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