HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j2 / Log4j2 RollingFileAppender example

Log4j2 RollingFileAppender example

Log4j2 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. Log4j2 maven dependencies


<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.1</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>

Check the latest version in maven repository.

2. Log4j2 RollingFileAppender example – rollover based on log file size

This given configuration roll over the 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. log4j2.properties

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

name = PropertiesConfig

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${LOG_DIR}/application.log
appender.rolling.filePattern = ${LOG_DIR}/application.%d{dd-MMM}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

logger.rolling.name = rollingFile
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

2.2. log4j2.xml

<RollingFile 
	name="rollingFile"
	fileName="${LOG_DIR}/application.log"
	filePattern="${LOG_DIR}/application.%i.log.gz"
	ignoreExceptions="false">
	<PatternLayout>
	    <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
	</PatternLayout>
	<Policies>
	    <SizeBasedTriggeringPolicy size="10MB" />
	</Policies>
	<DefaultRolloverStrategy max="5" />
</RollingFile>

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 '{MM-dd-yyyy-HH}' will rollover log file every hour.

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

<RollingFile 
	name="rollingFile"
	fileName="${LOG_DIR}/application.log"
	filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
	ignoreExceptions="false">
	<PatternLayout>
	    <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
	</PatternLayout>
	<Policies>
	    <TimeBasedTriggeringPolicy filePattern="${LOG_DIR}/application.%d{dd-MMM-hh}.log.gz" />
	</Policies>
	<DefaultRolloverStrategy max="5" />
</RollingFile>

3.2. Daily roll over logs example

To enable the daily rolling, log4j2 does not DailyRollingFileAppender which was present in earlier log4j. To rollover logs on daily basis, set interval to 1 in TimeBasedTriggeringPolicy.

<RollingFile 
	name="rollingFile"
	fileName="${LOG_DIR}/application.log"
     filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
	ignoreExceptions="false">
	<PatternLayout>
	    <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
	</PatternLayout>
	<Policies>
	    <TimeBasedTriggeringPolicy interval="1"/>
	</Policies>
	<DefaultRolloverStrategy max="5" />
</RollingFile>

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.

In given example, appender is able to refer the file name pattern and time based rollover strategy using filePattern attribute which include {dd-MMM}. Size based rollover will happen at 10 MB.

<RollingFile 
	name="rollingFile"
	fileName="${LOG_DIR}/application.log"
	filePattern="${LOG_DIR}/application.%d{dd-MMM}.log.gz"
	ignoreExceptions="false">
	<PatternLayout>
	    <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n</Pattern>
	</PatternLayout>
	<Policies>
	    <OnStartupTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="10 MB" />
        <TimeBasedTriggeringPolicy />
	</Policies>
	<DefaultRolloverStrategy max="5" />
</RollingFile>

Happy Learning !!

References:

RollingFileAppender Java Doc

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

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. Vincent Rivoire

    July 18, 2020

    Example 3.2. Daily rollover logs example does not even load…

    2020-07-18 08:02:42,467 main ERROR Could not create plugin of type 
    class org.apache.logging.log4j.core.appender.RollingFileAppender for 
    element RollingFile org.apache.logging.log4j.core.config.ConfigurationException: 
    Arguments given for element RollingFile are invalid: field 'filePattern' has invalid value 'null'
    
    • Lokesh Gupta

      July 19, 2020

      Please verify it again.

  2. QuestionForYou

    November 26, 2019

    Example 3.1 sets the file name pattern twice – the fileNamePattern attribute in RollingFile and the fileNamePattern attribute in TimeBasedTriggeringPolicy. Is this necessary? Whats the difference between the two?

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – ConsoleAppender
  • 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

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)