logback 按时间和大小生成日志不生效的问题

因为运维要求单日志文件占磁盘空间不能太大,而我们这个本身的日志是按天来分割的, 所以现在得加上在原来的基础上再加上按文件大小分割,本来这是一个很简单的需求,改下日志配置就好了。

按照官方的文档,按时间和大小生成日志:

<appender name="ScanRollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/applogs/appweb/common.log</file>
    <encoder>
        <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%thread] %c.%M \(%F:%L\) %n%msg%n</Pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <FileNamePattern>/applogs/appweb/common.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
        <maxFileSize>3000MB</maxFileSize>
        <MaxHistory>60</MaxHistory>
    </rollingPolicy>
</appender>
  1. 按天分割
  2. 日志文件超过3G大小的也分割,所以 FileNamePattern 后面的 i% 不要忘记了
  3. 最大保留历史是60个

本来这样配置上去是没有问题的,能正常打印日志,但是某项目不生成 common.log 日志,也没有报错信息。 经过排查发现该项目使用的 logback-core 版本是1.1.2, SizeAndTimeBasedRollingPolicy 是在1.1.7才出来的.

March 29th, 2016, Release of version 1.1.7
Logback is now compact3 profile compatible. This improvement was requested in LOGBACK-1071 by Axel Fontaine with Max Urech providing the relevant pull-request.

Fixed ConcurrentModificationException being thrown when the reset() method is invoked on the LoggerContext instance. This issue was reported in LOGBACK-397 by Szczepan Faber with Ross Sargant providing the relevant test case.

TimeBasedRollingPolicy now supports the totalSizeCap property which allows the user to limit the total size of archived logs.

SizeAndTimeBasedRollingPolicy offers the same functionality as SizeAndTimeBasedFNATP did previously but with a simpler configuration structure.

Archive removal by RollingFileAppender is now performed asynchronously.

Unnecessary and incompatible %i token in fileNamePattern option with RollingFileAppender/TimeBasedRollingPolicy is now detected and the user alerted to the misconfiguration problem. This fixes LOGBACK-1143.

Joran can now handle logger names ending with a $, i.e. the first character in variable substitution. This issue was raised in LOGBACK-1149 by by Stevo Slavic.

链接: https://logback.qos.ch/news.html

所以,要么使用 SizeAndTimeBasedFNATP 来解决这个问题,要么升级 logback-core 的版本。

转载于:https://my.oschina.net/110NotFound/blog/3028162

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值