简体   繁体   中英

Log4j 2 rolling file with wrong timestamp

I have following Roling file:

<RollingFile name="mylog"
                 filePattern="${sys:server.output.dir}/logs/mylog_%d{yyyy-MM-dd}.%i.log"
                 ignoreExceptions="false">
        <PatternLayout>
            <Pattern>%d %-5p %c %m%n\n</Pattern>
        </PatternLayout>
        <Policies>
            <OnStartupTriggeringPolicy minSize="100000000" />
            <SizeBasedTriggeringPolicy size="100 MB" />
            <TimeBasedTriggeringPolicy />
        </Policies>
        <DirectWriteRolloverStrategy />
    </RollingFile>

But I am getting wrong timestamp.

If the log is created on 24th of June it is called mylog_2019-06-23.1.log and it contains files from 24th .

Last files generated to better illustrate:

mylog_2019-06-20.1.log  - generated on 21st at 23:50 - contains logs from 21st

mylog_2019-06-21.1.log  - 22nd at 23:50 - contains logs from whole 22nd

mylog_2019-06-22.1.log  - 23rd at 00:00 - contains logs from 23rd to 7 AM

mylog_2019-06-23.1.log  - 23rd at 23:50 - contains logs from 23rd 7 AM to end of day

mylog_2019-06-23.2.log  - 24th at 00:00 - contains logs from today (24th) until now

What am I doing wrong? (same happens with any interval, eg minutes - the stamp is always one unit off)

So in the end I had to add fileName attribute to RollingFile element and change strategy to and it works now. Nomax is there just not to limit number of files to keep.

The side effect is that now the logs are first logged into mylog.log and when the file gets rollovered it gets renamed to mylog_{stamp}.log.

<RollingFile name="myLog"
                     fileName="${sys:server.output.dir}/logs/mylog.log"
                     filePattern="${sys:server.output.dir}/logs/mylog_%d{yyyy-MM-dd}.%i.log"
                     ignoreExceptions="false">
            <PatternLayout>
                <Pattern>%d %-5p %c %m%n\n</Pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB" />
                <TimeBasedTriggeringPolicy/>
            </Policies>
            <DefaultRolloverStrategy fileIndex="nomax"/>
        </RollingFile>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM