0

I'm using log4j 1.x (with slf4j). The log rotation is working file, but it is not clearing the rotated logs.

This is the content of log4.xml -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

<appender name="app1" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="${catalina.home}/logs_folder/webint.log"/>
    <param name="Append" value="true"/>
    <param name="MaxBackupIndex" value="24"/>
    <param name="immediateFlush" value="true"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
    <param name="ConversionPattern" value="%d{yyyy-MM-dd}-%t-%x-%-5p-%-10c:%m%n" />
  </appender>

  <root>
      <level value="INFO" />
      <appender-ref ref="app1"/>
   </root>

</log4j:configuration>

For some reason, instead of 24 files, more than 300 files are getting generated. Each log file size varies from 150MB to 350MB and it gets rotated every hour.

In the pom.xml,

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.12.1</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>

Dependency tree:

\- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
    \- log4j:log4j:jar:1.2.16:compile (version managed from 1.2.17)

I need to understand -

  1. Why are more than 24 files getting generated when MaxBackupIndex is mentioned as 24?
  2. Since I have not specified any MaximumFileSize, should it not be 10MB by default?
P_user526
  • 65
  • 3
  • 11

1 Answers1

0

Instead of DailyRollingFileAppender use RollingFileAppender As max backup file doesn't works with DailyRollingFileAppender

If you want to have daily rolling with max backup index you can follow the steps as mentioned at https://www.codeproject.com/Articles/81462/DailyRollingFileAppender-with-maxBackupIndex

I hope that helps Cheers