qos-ch / logback

The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
Other
2.97k stars 1.28k forks source link

Problems in weekly rolling #807

Open lapo-luchini opened 4 months ago

lapo-luchini commented 4 months ago

I am using a TimeBasedRollingPolicy with pattern %d{yyyy-ww}.log and I am getting this file order:

2023-51
2023-52
2023-01 ←
2024-02
2024-03

So I tried to debug the issue with this code:

import java.time.Instant;

import ch.qos.logback.core.ContextBase;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.rolling.helper.RollingCalendar;

public class TestWeek {
    public static void main(String[] args) {
        ContextBase c = new ContextBase();
        FileAppender f = new FileAppender<>();
        f.setFile("/tmp/test-%d{yyyy-ww}.log");
        TimeBasedRollingPolicy p = new TimeBasedRollingPolicy();
        p.setContext(c);
        p.setParent(f);
        p.setFileNamePattern("/tmp/test2-%d{yyyy-ww}.log");
        DefaultTimeBasedFileNamingAndTriggeringPolicy t = new DefaultTimeBasedFileNamingAndTriggeringPolicy();
        p.setTimeBasedFileNamingAndTriggeringPolicy(t);
        t.setContext(c);
        t.setTimeBasedRollingPolicy(p);
        t.setCurrentTime(Instant.parse("2023-12-20T12:00:00Z").toEpochMilli());
        p.start();
        t.start();
        t.setCurrentTime(Instant.parse("2024-01-01T12:00:00Z").toEpochMilli());
        t.isTriggeringEvent(null, null);
        System.out.println(t.getElapsedPeriodsFileName());
        t.setCurrentTime(Instant.parse("2024-01-14T12:00:00Z").toEpochMilli());
        t.isTriggeringEvent(null, null);
        System.out.println(t.getElapsedPeriodsFileName());
    }
}

and this doesn't seem to roll as needed (but it might be my test's fault):

/tmp/test2-2023-51.log
/tmp/test2-2023-51.log

But then I noticed this:

RollingCalendar rc = new RollingCalendar("yyyy-ww");
System.out.println(rc.computePeriodicityType());

This prints TOP_OF_MONTH but I was expecting TOP_OF_WEEK.

ceki commented 4 months ago

Created Jira issue for tracking this https://jira.qos.ch/browse/LOGBACK-1769