Sunday, 27 February 2022

Logback: rolling log file by time and size based policy

In this post, I am going to explain how to achieve time and size based log file archiving.

 

Using SizeAndTimeBasedRollingPolicy, we can achieve roll over by time and size.

 

Example

<rollingPolicy
	class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
	<!-- rollover daily -->
	<fileNamePattern>${LOG_FILES_HOME}/app-%d{yyyy-MM-dd}.%i.txt
	</fileNamePattern>
	<!-- each file should be at most 1MB, keep 30 days worth of history, but 
		at most 20GB -->
	<maxFileSize>1MB</maxFileSize>
	<maxHistory>30</maxHistory>
	<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy> 

 

Above snippet specifies that each log file should be at most 1MB, keep 30 days worth of history, but at most 20GB for all the log files size.

 

Find the below working application.

 

Step 1: Create new maven project ‘rolling-file-by-size-and-time-demo ‘.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>rolling-file-by-size-and-time-demo</artifactId>
	<version>1</version>

	<dependencies>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.10</version>
		</dependency>


		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.2.10</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.36</version>
		</dependency>


	</dependencies>
</project> 

 

Step 3: Create logback.xml file under src/main/resources folder.

 

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
	<property name="LOG_FILES_HOME" value="/Users/Shared/logback" />

	<appender name="consoleAppender"
		class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<charset>UTF-8</charset>
			<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n
			</Pattern>
		</encoder>
	</appender>


	<appender name="FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_FILES_HOME}/app.log</file>

		<rollingPolicy
			class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<!-- rollover daily -->
			<fileNamePattern>${LOG_FILES_HOME}/app-%d{yyyy-MM-dd}.%i.txt
			</fileNamePattern>
			<!-- each file should be at most 1MB, keep 30 days worth of history, but 
				at most 20GB -->
			<maxFileSize>1MB</maxFileSize>
			<maxHistory>30</maxHistory>
			<totalSizeCap>20GB</totalSizeCap>
		</rollingPolicy>


		<encoder>
			<charset>UTF-8</charset>
			<pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n
			</pattern>
		</encoder>
	</appender>

	<root level="DEBUG">
		<appender-ref ref="consoleAppender" />
		<appender-ref ref="FILE" />
	</root>
</configuration> 

Step 4: Define HelloWorld application.

HelloWorld.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
	private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);

	private static void sleep(int noOfSeconds) {
		try {
			TimeUnit.SECONDS.sleep(noOfSeconds);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		while (true) {
			for (int i = 0; i < 10000; i++) {
				logger.trace("Trace message");
				logger.debug("Debug message");
				logger.info("Info message");
				logger.warn("Warning message");
				logger.error("Error message");
			}

			sleep(1);

		}

	}
} 

Total project structure looks like below.




Run HelloWorld.java. You can observe that the log files are created in /Users/Shared/logback folder.

$ls -lart
total 43912
drwxrwxrwt  14 root     wheel      448 Feb 25 09:26 ..
-rw-r--r--   1 krishna  wheel  1380268 Feb 25 09:38 app-2022-02-25.0.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:38 app-2022-02-25.1.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.2.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.3.txt
-rw-r--r--   1 krishna  wheel  1218676 Feb 25 09:39 app-2022-02-25.4.txt
-rw-r--r--   1 krishna  wheel  1542028 Feb 25 09:39 app-2022-02-25.5.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.6.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.7.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.8.txt
-rw-r--r--   1 krishna  wheel  1056916 Feb 25 09:39 app-2022-02-25.9.txt
-rw-r--r--   1 krishna  wheel  1703788 Feb 25 09:39 app-2022-02-25.10.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.11.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.12.txt
-rw-r--r--   1 krishna  wheel  1380352 Feb 25 09:39 app-2022-02-25.13.txt
drwxr-xr-x  17 krishna  wheel      544 Feb 25 09:39 .
-rw-r--r--   1 krishna  wheel   895156 Feb 25 09:39 app.log

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/rolling-file-by-size-and-time-demo


 

 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment