Logback-test.xml 配置生成两个日志文件而不是一个?

Logback-test.xml configuration is producing two log files instead of one?

当我停止 运行 我的 spring-boot 应用程序时,会生成 两个 个日志文件,而不是 一个 (预计会有一个)。

我下面的 Logback-test.xml 文件有什么问题可能导致了这个问题?

logback-test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>

    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <logger name="org.springframework.web" level="INFO"/>

    <!-- Send debug messages to System.out -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS}  - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <file>C:\path\to\my\file\myLog-${myTimestamp}.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>myLog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

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

    </appender>

    <logger name="com.my.package" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

正在创建的两个文件例如:

myLog-2016-04-22_15-47-30.126.log

and

myLog-2016-04-22_15-47-30.922.log

你的 logging.config 属性 看起来怎么样?您的问题可能是 pathing(或属性文件中缺少 属性)。例如。应该看起来像这样:

logging.config=classpath:logback-test.xml

application-test.properties.

另一种可能是您有两个活动配置文件。例如。您的 test 个人资料和 default 个人资料。在这种情况下,您可以在 logback 中使用条件配置来解决它。

有关详细信息,请参阅 。另外,请参阅对该答案的最后评论。 spring boot 1.3 包含一些关于使用 logback 处理多个配置文件的新功能:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-logback-extensions

问题可能与这一行有关:

<include resource="org/springframework/boot/logging/logback/base.xml"/>

This version of the included files is including to others and one of them have a file appender。由于您正在定义自己的附加程序(文件和控制台),因此您可能不需要包含 Spring 引导基础文件。

这个有用吗?

<appender name="FILE" class="ch.qos.logback.core.FileAppender"">

    <file>C:\path\to\my\file\myLog-${myTimestamp}.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
    </encoder>

<!--
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <FileNamePattern>myLog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
        <MinIndex>1</MinIndex>
        <MaxIndex>10</MaxIndex>
    </rollingPolicy>
-->
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <MaxFileSize>50MB</MaxFileSize>
    </triggeringPolicy>

</appender>

解析配置时生成时间戳。由于 Spring Boot 在启动期间重新初始化了一次 logback,因此生成了两个不同的时间戳,因此生成了两个文件。

您可以在配置中使用 timeReferene="contextBirth" 来获取固定时间戳。由于 LoggerContext 没有被破坏,只需重置它就可以了:

<timestamp key="myTimestamp" timeReference="contextBirth" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>