我在 logback.xml 中有以下滚动策略。
问题是,如果文件大小超过 10 MB,则会引发异常。
看起来它正在尝试创建一个新文件,但由于同一日期已经存在相同的文件,因此它无法执行此操作并引发异常。
例如,我们已经有一个文件 pvExport.2016-05-15.log,如果 pvExport.log 增长超过 10 MB,它将尝试创建与 pvExport.2016-05-15.log 同名的文件,并且因此会抛出异常,但不确定
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${EXPORT_LOG_HOME}/pvExport.%d{yyyy-MM-dd}.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
最佳答案
在这种情况下,您的fileNamePattern
实际上是无效的。来自 docs
Note the "%i" conversion token in addition to "%d". Both the %i and %d tokens are mandatory. Each time the current log file reaches maxFileSize before the current time period ends, it will be archived with an increasing index, starting at 0.
将 %i
转换 token 添加到您的模式中应该可以解决此问题:
<fileNamePattern>${EXPORT_LOG_HOME}/pvExport.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
关于Logback:当文件增长超过 maxFileSize 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259175/