Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write a Logback RollingFileAppender configuration that rolls the file daily and when it reaches 100 MB, compresses old files, keeps 14 days and caps total size at 5 GB. Explain each setting.

We use a RollingFileAppender with a SizeAndTimeBasedRollingPolicy and a fileNamePattern such as logs/app-%d{yyyy-MM-dd}.%i.log.gz, where %d sets the daily period, %i numbers the files within one day and the .gz suffix makes Logback compress each rolled file. maxFileSize 100MB rolls within the day when the file gets too big, maxHistory 14 keeps fourteen periods, which here means days, totalSizeCap 5GB deletes the oldest archives once all of them together pass 5 GB, and cleanHistoryOnStart true also runs that cleanup at startup. In Spring Boot, the same settings are available as logging.logback.rollingpolicy properties.

Why this question is asked

On a virtual machine, a log file without rotation is a slow-motion outage: it grows until the disk is full, and then the application itself fails. A rolling configuration is the guard, but its settings interact in ways that surprise people, especially what maxHistory really counts. Interviewers ask for this configuration to see whether we can write it and explain each line. In this answer, we will recap what rolling means, write the full configuration, explain each setting and how they work together, show the Spring Boot properties for the same result, and finish with the Log4j2 version.

1A Quick Recap of Rolling

Rolling, or rotation, means that at some moment the appender closes the file it is writing, gives it a new name as an archive, and starts a fresh active file. A triggering policy decides when to roll, for example at midnight or when the file reaches a size. A rolling policy decides how: what the archive is called, whether it is compressed and when old archives are deleted. In Logback, SizeAndTimeBasedRollingPolicy is both at once, which is why it is the usual choice for 'daily and by size'.

2The Configuration

Here is the complete appender for the requirements in the question, with the active file always at logs/app.log.

XML
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/app.log</file> <!-- the active file -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/app-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>100MB</maxFileSize> <!-- roll within the day at 100 MB -->
<maxHistory>14</maxHistory> <!-- keep 14 periods (days) -->
<totalSizeCap>5GB</totalSizeCap> <!-- all archives together -->
<cleanHistoryOnStart>true</cleanHistoryOnStart> <!-- also clean at startup -->
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO"><appender-ref ref="FILE"/></root>
// After a busy 5 November and a quiet 6 November, the directory holds:
// logs/app.log <- today, still being written
// logs/app-2026-11-05.0.log.gz <- first 100 MB of the 5th
// logs/app-2026-11-05.1.log.gz <- next 100 MB
// logs/app-2026-11-05.2.log.gz <- the rest of the 5th
// logs/app-2026-11-06.0.log.gz <- the whole 6th, under 100 MB

Notice that the active file has a fixed name, which makes life easy for anyone tailing it and for a shipping agent, while archives carry the date and index.

3What Each Setting Does

fileNamePattern does three jobs. The %d{yyyy-MM-dd} token sets the period: because the most precise unit in the date pattern is the day, Logback rolls once a day at midnight. The %i token is an index that starts at 0 each period and increases every time a size roll happens within that period; it is required with SizeAndTimeBasedRollingPolicy. And the .gz ending tells Logback to compress each archive with gzip after rolling, which typically shrinks text logs by a factor of ten or more; a .zip ending works the same way with zip.

maxFileSize 100MB is the size trigger. When the active file passes 100 MB, Logback rolls it into the next index for today, even if midnight is hours away. Logback checks the size periodically rather than on every single write, so a file can slightly overshoot the limit.

maxHistory 14 keeps archives for 14 periods, and the period here is one day, so archives older than 14 days are deleted at rollover. The catch is that it counts periods, not files: one day with 30 size rolls is still one period, and all 30 files are kept. That is why totalSizeCap exists. totalSizeCap 5GB makes Logback, after each rollover, add up the size of all archives and delete the oldest ones until the total is below 5 GB; in Logback it works together with maxHistory, so set both (check your version's documentation).

cleanHistoryOnStart true runs the same cleanup when the appender starts. Without it, cleanup happens only at rollover, so a service that restarts often, or a batch job that runs for ten minutes a day and rarely crosses midnight, may never clean up.

Step throughWhat happens on each check
  1. Is it a new day?
    YesRoll to app-<yesterday>.<i>.log.gzNoNext check
  2. Is app.log over 100 MB?
    YesRoll to app-<today>.<i+1>.log.gzNoKeep writing
  3. After a roll: archives older than 14 days?
    YesDelete themNoNext check
  4. Total archive size over 5 GB?
    YesDelete oldest until under 5 GBNoDone

4 steps in order. Play them, or step through them one at a time.

Compression of the new archive happens in the background after the roll.

4The Same in Spring Boot Properties

Spring Boot's default Logback configuration already contains a rolling file appender with SizeAndTimeBasedRollingPolicy, used when a log file is configured. We can tune it with properties instead of writing logback-spring.xml.

Properties
logging.file.name=logs/app.log
logging.logback.rollingpolicy.file-name-pattern=logs/app-%d{yyyy-MM-dd}.%i.log.gz
logging.logback.rollingpolicy.max-file-size=100MB
logging.logback.rollingpolicy.max-history=14
logging.logback.rollingpolicy.total-size-cap=5GB
logging.logback.rollingpolicy.clean-history-on-start=true

Boot's default file-name pattern is based on the log file name with %d{yyyy-MM-dd}.%i.gz, and the default max-file-size is 10 MB and max-history 7 days, so check your version's defaults if you leave any property out.

5Follow-up questions

What does the Log4j2 equivalent look like with policies and a DefaultRolloverStrategy?

A RollingFile appender with fileName logs/app.log and filePattern logs/app-%d{yyyy-MM-dd}-%i.log.gz, containing Policies with a TimeBasedTriggeringPolicy and a SizeBasedTriggeringPolicy of size 100 MB. The DefaultRolloverStrategy needs a max large enough for the number of files per day, because its default of 7 means only seven indexes per period are kept. Retention is done with a Delete action inside the strategy: basePath logs, an IfFileName glob of app-*.log.gz, and either IfLastModified with age 14d or IfAccumulatedFileSize with exceeds 5 GB, nested so that both conditions apply.

Why not let the operating system's logrotate do this instead?

We can, but then Logback must not roll the same file, or the two mechanisms fight: logrotate renames or deletes a file that Logback still holds open, and the disk space is not freed. Choose one mechanism per file. Logback's own rolling is usually simpler because it knows when it is safe to switch files.

Is an hourly period better for busy services?

It can be. With %d{yyyy-MM-dd_HH}, the period becomes an hour, so maxHistory then counts hours, and 14 would mean 14 hours, not 14 days. If we change the period, we must change maxHistory with it, for example to 336 for 14 days of hourly files.

6Conclusion

In this answer, we wrote a Logback RollingFileAppender with SizeAndTimeBasedRollingPolicy that rolls daily through %d and within the day through %i at 100 MB, compresses archives because the pattern ends in .gz, keeps 14 periods with maxHistory, caps the total at 5 GB with totalSizeCap and cleans at startup with cleanHistoryOnStart. We saw that maxHistory counts periods, not files, which is why the size cap matters, expressed the same settings as Spring Boot properties, and outlined the Log4j2 version with triggering policies and a Delete action.

What to say in the interviewAbout 90 seconds aloud

Open with

I would use a RollingFileAppender writing to logs/app.log with a SizeAndTimeBasedRollingPolicy. The fileNamePattern is logs/app-%d{yyyy-MM-dd}.%i.log.gz. The %d part sets the period, and since the finest unit is the day, it rolls at midnight.

Then, if they want more

The %i is an index within the day for size-based rolls, and the .gz ending makes Logback gzip each archive after rolling. maxFileSize 100MB triggers an extra roll within the day when the active file passes 100 MB, so a busy day produces app-date.0, .1, .2 and so on. maxHistory 14 keeps fourteen periods, which here means fourteen days, but it counts periods, not files, so a day with fifty size rolls keeps all fifty.

That is why I add totalSizeCap 5GB, which after each roll deletes the oldest archives until all of them together are under 5 GB; it works together with maxHistory, so I set both. cleanHistoryOnStart true runs the same cleanup at startup, which matters for services that restart often and might never reach a rollover.

In Spring Boot I would usually not write XML at all and set logging.file.name plus the logging.logback.rollingpolicy properties: file-name-pattern, max-file-size, max-history, total-size-cap and clean-history-on-start.

In Log4j2 the same thing is a RollingFile with a time and a size triggering policy and a DefaultRolloverStrategy with a Delete action, remembering that its default max of seven files per period is too low here.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a Logback RollingFileAppender configuration that rolls the file daily and when it...": https://scrivio-interview-library.netlify.app/java/java-logging/2914f1bfb4/