How to limit the number of files archived by Logback

Published on 06/06/2025

How can I limit the maximum number of log files archived by Logback ?

Solution

We have to use <maxHistory> tag of TimeBasedRollingPolicy.

The unit (monthly, daily, hourly, etc) is extracted implicitly from the date format defined in <fileNamePattern>.

<appender name="RollingFile"
	class="ch.qos.logback.core.rolling.RollingFileAppender">
	<file>application.log</file>
	<encoder
		class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
		<Pattern>%d{HH:mm:ss.SSS, Europe/Rome} %-5level %logger{0} - %msg%n</Pattern>
	</encoder>

	<rollingPolicy
		class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
		<fileNamePattern>application.log.%d{yyyy-MM-dd, Europe/Rome}</fileNamePattern>
		 <maxHistory>60</maxHistory>
	</rollingPolicy>
</appender>
  • Article is published under CC-BY 4.0
  • If not explicitly declared code snippets are published under MIT license