問題描述
log4j2 RolllingFileAppender filePattern 創建日期錯誤 (log4j2 RolllingFileAppender filePattern create a wrong date)
我在 filePattern 中使用了日期,但是 log4j 創建的日期不正確。今天是2015‑11‑23,但是log4j創建了一個名為2015‑12的文件目錄。我的 log4j2 版本是 2.4.1。這是一個快照
下面是我的 log4j2 配置。</電話>
<RollingFile name="RollingFile" fileName="logs/executor.log" append="true"
filePattern="logs/$${date:yyyy‑MM}/executor‑%d{yyyy‑‑MM‑‑dd}‑%i.log.gz">
<PatternLayout>
<Pattern>%t %d{yyyy‑MM‑dd HH:mm:ss} %c %p ‑%m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="1MB"/>
</Policies>
</RollingFile>
參考解法
方法 1:
This issue is due to your intervals. Its effectively setting up the initial rollover file to be when the end of the next rollover would be. The documentation on the RollingFileAppender for the TimeBasedTriggeringPolicy says: "How often a rollover should occur based on the most specific time unit in the date pattern." Since your most specific time unit is dd, or day of month, its setting it up for 24 days from 2015‑11‑23, or sometime in month 12, when it would rollover.
What you probably want is an interval of 1. This should rollover each file day and then once a month spills over, it would into the next subfolder because 1 day was triggered.
I've tested this with your pattern, but use a MM at the end and and a interval="24." Modulated, it rolled into 12/2016, but not modulated, it rolled over saying 11/2017! I can't say why and per my comment above, there have been issues with this feature reported.
方法 2:
Have same problem.
<RollingFile name="DailyFileAppender" fileName="logs/server.log"
filePattern="logs/server ‑ %d{yyyy‑MM‑dd} ‑ %i.log">
<PatternLayout>
<pattern>%d{yyyy‑MM‑dd HH:mm:ss,SSS}‑ %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="5"/>
<SizeBasedTriggeringPolicy size="10 KB"/>
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile>
The output file's data is wrong. example the date is "2015‑12‑01" the output date is "2015‑12‑05"
(by chow、Adam Marcionek、TinyZ)