更新:有趣的是我只看到下面的问题出现在Windows环境中,但是在每个Linux环境中部署它都运行正常。
我有一个带有fileAppender的Log4j 1.2.17的Spring应用程序(3.2),这里是配置
log4j.rootCategory=TRACE, ROL
log4j.appender.ROL=org.apache.log4j.RollingFileAppender
log4j.appender.ROL.Encoding=UTF-8
log4j.appender.ROL.File=C:/logs/spring-app/application-log4j.log
log4j.appender.ROL.MaxFileSize=10000KB
log4j.appender.ROL.MaxBackupIndex=10
log4j.appender.ROL.layout=org.apache.log4j.PatternLayout
log4j.appender.ROL.layout.ConversionPattern=%d -- %p -- %c -- %m%n
由于某种原因,我发现日志文件有时会停止滚动并且变得非常大,所以我添加了log4j调试模式
-Dlog4j.debug
我发现以下问题为症状
这是日志记录过程正常工作
log4j: rolling over count=10332832
log4j: maxBackupIndex=10
log4j: Renaming file C:\logs\spring-app\application-log4j.log.9 to C:\logs\spring-app\application-log4j.log.10
log4j: Renaming file C:\logs\spring-app\application-log4j.log.8 to C:\logs\spring-app\application-log4j.log.9
log4j: Renaming file C:\logs\spring-app\application-log4j.log.7 to C:\logs\spring-app\application-log4j.log.8
log4j: Renaming file C:\logs\spring-app\application-log4j.log.6 to C:\logs\spring-app\application-log4j.log.7
log4j: Renaming file C:\logs\spring-app\application-log4j.log.5 to C:\logs\spring-app\application-log4j.log.6
log4j: Renaming file C:\logs\spring-app\application-log4j.log.4 to C:\logs\spring-app\application-log4j.log.5
log4j: Renaming file C:\logs\spring-app\application-log4j.log.3 to C:\logs\spring-app\application-log4j.log.4
log4j: Renaming file C:\logs\spring-app\application-log4j.log.2 to C:\logs\spring-app\application-log4j.log.3
log4j: Renaming file C:\logs\spring-app\application-log4j.log.1 to C:\logs\spring-app\application-log4j.log.2
log4j: Renaming file C:\logs\spring-app\application-log4j.log to C:\logs\spring-app\application-log4j.log.1
log4j: setFile called: C:/STS/STS.3.2.0/logs/spring-app/application-log4j.log, true
log4j: setFile ended
这是appender失败的开始
log4j: rolling over count=10332832
log4j: maxBackupIndex=10
log4j: Renaming file C:\logs\spring-app\application-log4j.log.9 to C:\logs\spring-app\application-log4j.log.10
log4j: Renaming file C:\logs\spring-app\application-log4j.log.8 to C:\logs\spring-app\application-log4j.log.9
log4j: Renaming file C:\logs\spring-app\application-log4j.log.7 to C:\logs\spring-app\application-log4j.log.8
log4j: Renaming file C:\logs\spring-app\application-log4j.log.6 to C:\logs\spring-app\application-log4j.log.7
log4j: Renaming file C:\logs\spring-app\application-log4j.log.5 to C:\logs\spring-app\application-log4j.log.6
log4j: Renaming file C:\logs\spring-app\application-log4j.log.4 to C:\logs\spring-app\application-log4j.log.5
log4j: Renaming file C:\logs\spring-app\application-log4j.log.3 to C:\logs\spring-app\application-log4j.log.4
log4j: Renaming file C:\logs\spring-app\application-log4j.log.2 to C:\logs\spring-app\application-log4j.log.3
#Apparently fail the rename log.1 -> log.2
log4j: Renaming file C:\logs\spring-app\application-log4j.log to C:\logs\spring-app\application-log4j.log.1
log4j: setFile called: C:/STS/STS.3.2.0/logs/spring-app/application-log4j.log, true
log4j: setFile ended
这个问题使得旧的日志(log.3~log.9)逐渐被重命名消失,只留下第一个 - application-log4j.log。所以这个文件可以疯狂地增长到1 GB +,并且不会生成其他日志。
但是现在我真的没有太多的方法可以深入了解究竟是什么导致了这个问题(第二个日志文件无法重命名,看起来像是放在文件上的日志)
答案 0 :(得分:2)
log4j 1.2.17中存在org.apache.log4j.RollingFileAppender
的已知问题。例如,this bug in log4j的说明与您的问题类似。
您应该使用Apache Extras™ for Apache log4j™
中的rolling file appender,因为它是recommended in the Javadoc to do so。这是link on Maven Central to the library。
您是否尝试过以下配置以确定它是否解决了您的问题?
log4j.appender.ROL=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.ROL.Encoding=UTF-8
log4j.appender.ROL.File=C:/logs/spring-app/application-log4j.log
log4j.appender.ROL.layout = org.apache.log4j.PatternLayout
log4j.appender.ROL.layout.ConversionPattern=%d -- %p -- %c -- %m%n
log4j.appender.ROL.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.ROL.TriggeringPolicy.MaxFileSize=10000KB
log4j.appender.ROL.RollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.ROL.RollingPolicy.ActiveFileName=C:/logs/spring-app/application-log4j.log
log4j.appender.ROL.RollingPolicy.FileNamePattern=C:/logs/spring-app/application-log4j.log.%i
log4j.appender.ROL.RollingPolicy.MaxIndex=10
最后,log4j是一个旧库,而不是在积极开发中。如果可行,您应该强烈考虑升级到log4j 2.0。