Logback-test.xml配置生成两个日志文件而不是一个? [英] Logback-test.xml configuration is producing two log files instead of one?

查看:235
本文介绍了Logback-test.xml配置生成两个日志文件而不是一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I stop running my spring-boot application there are two log files produced rather than one (one is expected).

我的 Logback-test.xml 文件可能导致此错误?

What is wrong in my Logback-test.xml file below that may be causing this?

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>

正在创建的两个文件是:

The two files being created are e.g.:

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

and

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


推荐答案

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

The timestamp is generated when the configuration is parsed. Since Spring Boot reinitializes logback once during startup, two different timestamps are generated and therefore the two files.

您可以在配置中使用 timeReferene =contextBirth来获取固定时间戳。由于 LoggerContext 未被销毁,只需重新设置即可使用:

You can use a timeReferene="contextBirth" in your config to get a constant timestamp. Since the LoggerContext isn't destroyed, just resetted this should work:

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

这篇关于Logback-test.xml配置生成两个日志文件而不是一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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