Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1776

SizeAndTimeBasedRollingPolicy does not take into account the initial size of an existing log file

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major Major
    • 1.5.12
    • 1.5.10
    • None
    • None
    • Dependency: logback-classic:1.5.9
      Java: 17
      System: windows 10

    Description

      Problem: when using SizeAndTimeBasedRollingPolicy and setting the maxFileSize field, if logback writes to an already existing log file (a file that was created before running the application), it will not rotate the file if the initial size is already over maxFileSize or if during execution the file exceeds maxFileSize, but it will rotate the log file only after writing maxFileSize bytes to the file, regardless of its actual size

      Reproducer

      package com.example;
      
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      
      public class App {
      
      	private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
      
      	public static void main(String[] args) {
      		for (int i = 0; i < 20000; i++) {
      			LOGGER.info("X".repeat(55));
      		}
      	}
      }
      

      logback.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE configuration>
      
      <configuration>
        <import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
        <import class="ch.qos.logback.core.rolling.RollingFileAppender"/>
        <import class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"/>
      
        <appender name="ROLLING" class="RollingFileAppender">
          <rollingPolicy class="SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
            <maxFileSize>1MB</maxFileSize>
            <maxHistory>90</maxHistory>
            <totalSizeCap>20GB</totalSizeCap>
          </rollingPolicy>
          <encoder class="PatternLayoutEncoder">
            <pattern>%msg%n</pattern>
          </encoder>
        </appender>
      
        <root level="DEBUG">
          <appender-ref ref="ROLLING"/>
        </root>
      </configuration>
      

      What happens when running the application the first time:
      If there are no existing log files following the fileNamePattern: 2 log files will be generated properly

      mylog-2024-10-11.0.txt       1025 KB
      mylog-2024-10-11.1.txt       90 KB
      

      What happens when running the application multiple times after the first:
      The existing log files keep getting written for maxFileSize bytes and no rotation occurs

      mylog-2024-10-11.0.txt       2049 KB
      mylog-2024-10-11.1.txt       179 KB
      
      mylog-2024-10-11.0.txt       3073 KB
      mylog-2024-10-11.1.txt       268 KB
      

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            ceki Ceki Gülcü
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: