Generate new log file using logback when file size goes beyond a limit

35 views Asked by At

In my springboot application , I want to create a new log file only when file size reaches a limit (say 50KB). I am using logback and I have used FixedWindowRollingPolicy rollingPolicy and SizeBasedTriggeringPolicy as triggerPolicy.

But new log file is not created even after exceeding the limit specified by MaxFileSize. Instead , new log file is created after application restart.

I want new log file to be created only if log file size goes beyond MaxFileSize irrespective of time taken . File should not get created on application restart or day change.

Here are the files in my application :

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.logging</groupId>
    <artifactId>logging</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>logging</name>
    <description>Demo logging project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="file"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <File>logs/application.log</File>
        <encoder>
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n</pattern>
        </encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <maxIndex>10</maxIndex>
            <FileNamePattern>logs/application.log.%i</FileNamePattern>
        </rollingPolicy>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>50KB</MaxFileSize>
        </triggeringPolicy>
    </appender>
    <appender name="stdout"
        class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="file" />
        <appender-ref ref="stdout" />
    </root>
</configuration>

application.properties

spring.application.name=logging

logging.level.root = trace

I have tried various combinations but its not working.

Thanks for help in advance.

1

There are 1 answers

0
Harshal On

After debugging in logback code ,my findings are :

  1. New log file is not created if current log file is generated less than 60 sec before.
  2. And in this case , logs are pushed to current log file irrespective of its size. In this case , log file may grow beyond the size specified by maxFileSize.
  3. So , new log file is generated only if current log file is 60 sec old and file size is more than size specified in maxFileSize.