Skip to content

TimeBasedRollingPolicy does not honor HALF_DAY periodicity for pattern %d{yyyy-MM-dd-a} — rolls over only at midnight instead of 00:00 and 12:00 #976

Description

@shakthifuture

When using a TimeBasedRollingPolicy with a date pattern containing the AM/PM marker (a), such as:

<fileNamePattern>/var/log/app.log.%d{yyyy-MM-dd-a}</fileNamePattern>

Logback initializes with the message:

The date pattern is 'yyyy-MM-dd-a'.
Roll-over at midnight.

and performs daily rotation only (00:00), not twice per day (00:00 and 12:00) as expected.

Expected behavior

For a pattern including the AM/PM token (a), Logback should detect HALF_DAY periodicity and rotate logs at midnight and noon.

Actual behavior

Startup message says “Roll-over at midnight.”

Only one rollover per day occurs.

The enum constant HALF_DAY exists in RollingCalendar (printPeriodicity() even prints "Roll-over at midday and midnight."), but this value is never selected by the periodicity detector.

Technical cause

From source inspection:

RollingCalendar#printPeriodicity() supports HALF_DAY.

However, RollingCalendar#innerGetEndOfNextNthPeriod() and the periodicity detector never implement logic for HALF_DAY, so detection always falls back to TOP_OF_DAY.

As a result, DefaultTimeBasedFileNamingAndTriggeringPolicy always logs “Roll-over at midnight.”

This behavior is reproducible in Logback 1.4.x (tested with 1.4.14) and appears to match the old LOGBACK-1552 report (“yyyy-MM-dd-a not working as expected”).

Steps to Reproduce

Configure:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>app.log</file>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>app.log.%d{yyyy-MM-dd-a}</fileNamePattern>
  </rollingPolicy>
</appender>

Run an app that logs periodically.

Observe startup message and verify only one rotation at 00:00.

Proposed fix

Add explicit HALF_DAY handling in:

RollingCalendar#innerGetEndOfNextNthPeriod()

RollingCalendar#getPeriodicityType(…) (so %d{…a} correctly maps to HALF_DAY)
to align detection with printPeriodicity()’s existing message and enable true twice-daily rotation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions