Video in progress

What is log rotation, and what happens without it?

Log rotation means switching the log file to a new one by size or date, compressing the old ones and deleting them after a set time. Without it, a single file grows until the disk fills up and the application, sometimes the whole server, goes down.

📖 Answer

A classic prod incident: the service ran for a few months, app.log reached 80 GB, and the disk filled up. Now the database can't write either, the application can't open a new file, and the service "goes down".

This is how rotation is configured in Logback:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logs/app.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <fileNamePattern>logs/app-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
        <maxFileSize>100MB</maxFileSize>
        <maxHistory>14</maxHistory>
        <totalSizeCap>5GB</totalSizeCap>
    </rollingPolicy>
</appender>

Here a new file is opened every day or at 100 MB, the old one is turned into a .gz, and anything older than 14 days or beyond a total of 5 GB is deleted.

In Spring Boot, the simple case doesn't need XML: there are the logging.file.name and logging.logback.rollingpolicy.* properties.

In a container environment you often don't write to a file at all: the application writes to stdout, Docker or Kubernetes does the rotation, and an agent (Fluent Bit, Promtail) does the collecting.