文件名中带有时间戳的 Logback 文件未翻转

Logback file with timestamp in file name not rolledover

我正在使用以下日志配置。当文件名中包含时间戳时,文件不会滚动。这是因为时间戳中使用了 timeReference 吗?如果我从文件名中删除时间戳,那么文件会在达到 10KB 文件大小时滚动。

有什么方法可以在文件名中包含时间戳,同时使翻转有效吗?

<timestamp key="startTime" datePattern="yyyyMMdd_HHmmss" timeReference="contextBirth">
<property name="logFileName" value="xyz_${startTime}">
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${logFileName}.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${logFileName}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10KB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
        <pattern>%date %level %logger{35} %msg%n%rEx</pattern>
    </encoder>
</appender>

我解决了这个问题。有 2 个具有相同文件名的附加程序。所以重命名失败了。

https://logback.qos.ch/codes.html 网站上的以下描述对我有所帮助。

File/FileNamePattern option has the same value "..." as that given for appender [...] defined earlier.

If a FileAppender/RollingFileAppender defined earlier has the same File option as the current appender, then those two appenders are in collision as FileAppender instances cannot share the same output target. To prevent loss of data, the current appender will not start. Make sure that each appender has a unique File option.

By analogy the same restriction applies to the FileNamePattern option of RollingFileAppender. Make sure that each RollingFileAppender has a unique FileNamePattern option