Tuesday 1 March 2022

logback: gzip rolling log files automatically

File compression format can be specified via fileNamePattern property.

 

Example

<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  <fileNamePattern>${LOG_FILES_HOME}/app-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>

  <maxFileSize>1MB</maxFileSize>
  <maxHistory>30</maxHistory>
  <totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>

 

Find the below working application.

 

Step 1: Create new maven project ‘logback-zip-log-files’.

 

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>logback-zip-log-files</artifactId>
  <version>1</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <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">
      <fileNamePattern>${LOG_FILES_HOME}/app-%d{yyyy-MM-dd}.%i.log.gz
      </fileNamePattern>

      <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 class.

 

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 application.

 

You can observe that log files are compressed to .gz format in /Users/Shared/logback folder.

 

$ls -lart
total 2456
drwxrwxrwt  15 root     wheel      480 Feb 25 15:39 ..
-rw-r--r--   1 krishna  wheel     9193 Feb 25 15:45 app-2022-02-25.0.log.gz
-rw-r--r--   1 krishna  wheel     8201 Feb 25 15:45 app-2022-02-25.1.log.gz
-rw-r--r--   1 krishna  wheel     8058 Feb 25 15:45 app-2022-02-25.2.log.gz
drwxr-xr-x   7 krishna  wheel      224 Feb 25 15:45 .
-rw-r--r--   1 krishna  wheel  1215810 Feb 25 15:45 app.log

You can download the complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-zip-log-files


Previous                                                 Next                                                 Home

No comments:

Post a Comment