当我使用以下附加程序时
<appender name="APPLICATION"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>D:/logs/log.txt</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%p %d [%t] %L - %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>D:/logs/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
我收到以下警告:
SizeAndTimeBasedFNATP已弃用。使用SizeAndTimeBasedRollingPolicy 代替
所以我将SizeAndTimeBasedFNATP
更改为SizeAndTimeBasedRollingPolicy
这让我有了:
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
但是我得到:
未为名为RollingFileAppender的设置TriggeringPolicy 应用
这让我认为没有这样的TriggeringPolicy,所以我将TriggeringPolicy恢复为SizeAndTimeBasedFNATP
,并将滚动策略从TimeBasedRollingPolicy
更改为SizeAndTimeBasedRollingPolicy
。
我的新的rollingPolicy现在变为:
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>D:/logs/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
但是我得到:
TriggeringPolicy尚未启动。 RollingFileAppender无法启动
我试图在每个午夜或在达到最大大小后(以先到者为准)实现过渡。现在,我使用的是Logback版本:1.2.3
答案 0 :(得分:1)
最终发现maxFileSize
必须在timeBasedFileNamingAndTriggeringPolicy
之外,而SizeAndTimeBasedRollingPolicy
并不需要。因此,根据时间和大小(以先到者为准)滚动的最终logback.xml变为:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%p %d [%t] %L - %m%n</pattern>
</encoder>
</appender>
<appender name="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>D:/logs/log.txt</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%p %d [%t] %L - %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>D:/logs/log.%d{yyyy-MM-dd HH}.%i.txt</fileNamePattern>
<maxFileSize>5GB</maxFileSize>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
<logger name="javaportreader" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="application" level="DEBUG" additivity="false">
<appender-ref ref="APPLICATION"/>
</logger>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<root level="INFO">
<appender-ref ref="APPLICATION"/>
</root>
</configuration>