HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j / Log4j RollingFileAppender configuration example

Log4j RollingFileAppender configuration example

Log4j RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

Generally backup of log files are created based on file size, current date or both.

1. Log4j maven dependencies

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

2. RollingFileAppender example – rollover based on log file size

This given configuration roll over th log files based on log file size. I have configured the log file size to be 10 MB. Change it as per your requirement.

2.1. log4j.properties

We can configure rolling file appender in log4j.properties in given way.

log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=${LOG_DIR}/application.log
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
log4j.appender.rollingFile.MaxFileSize=10MB
log4j.appender.rollingFile.MaxBackupIndex=5
log4j.appender.rollingFile.append=true

log4j.rootCategory=ALL, rollingFile

2.2. log4j.xml

<appender name="rollingFile" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="${LOG_DIR}/application.log" />
    <param name="MaxFileSize" value="10MB" />
    <param name="MaxBackupIndex" value="5" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n" />
        </layout>
</appender>

3. RollingFileAppender – rollover based on date time

We can roll over log files based on date time as well.

3.1. RollingFileAppender example

If using RollingFileAppender, then use TimeBasedRollingPolicy to specify when to roll over log files based on date time.

Notice the FileNamePattern property. It defines the name pattern for rolled over files. In given example, it will rename the rollover log files with date-month in log file name.

For example, pattern '{dd-MMM}' will rollover log file everyday of month. Similarly, '{MM-dd-yyyy-HH}' will rollover every hour.

We also use .gz extension so log4j will compress the log file automatically.

<appender name="rollingFile" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="${LOG_DIR}/application.%d{dd-MMM}.log.gz" />
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
    </layout>
</appender>

3.2. DailyRollingFileAppender example

To enable the daily rolling, log4j provides DailyRollingFileAppender which extends FileAppender. Use it directly, if you want to roll over your log files daily.

<appender name="rollingFile" class="org.apache.log4j.rolling.DailyRollingFileAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
    </layout>
</appender>

4. RollingFileAppender – rollover based on both – log size and date time

If you want to rollover log files based on file size and date time both, then you need to use SizeBasedTriggeringPolicy and TimeBasedRollingPolicy both.

<appender name="rollingFile" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy 
    	class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="ActiveFileName" value="${LOG_DIR}/application.log" />
        <param name="FileNamePattern" value="${LOG_DIR}/application.%d{dd-MMM}.log.gz" />
    </rollingPolicy>
    <triggeringPolicy
        class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="MaxFileSize" value="10MB" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
    </layout>
</appender>

References:

Log4j Rolling File Appender Java Doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Vasudev Patil

    March 5, 2020

    How to configure TimeBasedRollingPolicy using Properties file for automatic Compression

  2. Deepak

    February 19, 2020

    I want to roll over the files every 8 hours … Is this possible with the help of org.apache.log4j.rolling.TimeBasedRollingPolicy.

  3. Pankaj

    September 11, 2019

    Has this been tested?
    Because there is no package ‘rolling’ at that path, nor is there any class ‘TimeBasedRollingPolicy’ in log4j 1.2.17 version (or atleast I could not find it).
    Can you please correct this?

    • Lokesh Gupta

      September 12, 2019

      Its tested. Please refer to this link.

    • Deepak

      January 15, 2020

      org.apache.log4j.rolling.RollingFileAppender is present in apache-log4j-extras-1.2.17.jar

  4. Wasim Shaikh

    July 9, 2019

    Please give the property configuration for

    • Wasim Shaikh

      July 9, 2019

      Please provide the java configuration for below

       
      <appender name="rollingFile" class="org.apache.log4j.rolling.RollingFileAppender">
          <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
              <param name="FileNamePattern" value="${LOG_DIR}/application.%d{dd-MMM}.log.gz" />
          </rollingPolicy>
          <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
          </layout>
      </appender>
      
       
  5. Raul Brun Sotelo

    May 20, 2019

    Hi,
    I think you made a mistake, the class DailyRollingFileAppender extends from FileAppender, not from RollingFilleAppender, at least in current version 1.2.

    This can originate some problems if you want to manage backup files with DailyRollingFileAppender.

    • Lokesh Gupta

      May 20, 2019

      Thanks for pointing out. I updated the post.

  6. Maik

    February 6, 2019

    Hi, first, u wrote a very good tutorial, thanks!
    I think, there is a mistake:
    class=”org.apache.log4j.rolling.RollingFileAppender”
    the package org.apache.log4j.rolling is not available, so it should be class=”org.apache.log4j.RollingFileAppender” Am I right?

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces