活动日志文件的通用名称

Generic name for the active log file

我的 spring 引导应用程序有以下 logback.xml 文件,我打算在其中创建每日日志文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>./logs/my-log-file.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>15</maxHistory>
            <totalSizeCap>15MB</totalSizeCap>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d [%-5thread] [%-1p] [%logger{35}] - %msg%n</Pattern>
        </layout>
    </appender>

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

文件是这样创建的:

my-log-file.2018-11-10.log
my-log-file.2018-11-11.log
my-log-file.2018-11-12.log

我希望当前的活动日志文件没有日期,所以它是通用名称,如 my-log-file.log,只有当日期滚动到新的一天时,文件才用日期重命名。

需要对 logback.xml 进行哪些更改才能启用此配置?

<appender>中添加一个<file>,像这样:

<appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>./logs/my-log-file.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>./logs/my-log-file.%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>15</maxHistory>
        <totalSizeCap>15MB</totalSizeCap>
    </rollingPolicy>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%d [%-5thread] [%-1p] [%logger{35}] - %msg%n</Pattern>
    </layout>
</appender>

来自Logback documentation:

Note that the file property in RollingFileAppender (the parent of TimeBasedRollingPolicy) can be either set or omitted. By setting the file property of the containing FileAppender, you can decouple the location of the active log file and the location of the archived log files. The current logs will be always targeted at the file specified by the file property. It follows that the name of the currently active log file will not change over time. However, if you choose to omit the file property, then the active file will be computed anew for each period based on the value of fileNamePattern.