Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

How to configure Daily Log File Rolling in Java using Log4j - DailyRollingFileAppender Example

Hello guys, today, I am going to share one small but the useful tip about logging in to your Java application. If your Java application is a weekly restart, I mean it starts on Sunday and not again on the weekday, then you really want to have separate log files for each day. This helps during troubleshooting and debugging. But If you are facing a problem where your log files are not rolling daily and becoming bigger and bigger after each passing day, making it challenging to search anything in case of any production issue, then it might be that you have not configured your Log4j properly to roll your logs daily.

In this article, I'll show you how to set DailyRollingFileAppender for the Log4j library, which will backup the current day file at midnight and create a new log file for the next day.  Btw, if you are not familiar with Log4j  and want to learn more about apache log4j, loggers, appenders, and layouts then I suggest you check out this Apache Logging Crash Course on Udemy.

Log4j DailyRollingFileAppender Example

You can use the following file to configure daily file rolling for your Java application:

# Set root category priority to INFO and its only appender to DailyRollingFileAppender.
log4j.rootCategory=INFO, LOGFILE

# Set the enterprise logger category to FATAL and its 
# only appender to DailyRollingFileAppender., LOGFILE

# LOGFILE is set to be a DailyRollingFileAppender appender using a PatternLayout.
log4j.appender.LOGFILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGFILE.Append = true
log4j.appender.LOGFILE.DatePattern =.yyy-MM-dd
log4j.appender.LOGFILE.layout = org.apache.log4j.PatternLayout
= %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

Each log file is rolled out every day, and the file without a date in its name is the current log file. Suppose today is 2020-01-04, and at midnight, Log4j will back up the app.log file into app.log.2020-01-04, and the app.log file becomes logging for the new day, 2020-01-05, and so on.

This is very useful when there is a need for tracking log files based on some interval of time. It also helps to identify the problem quickly by inspecting only the relevant log files.

How to configure Daily File Rolling in Log4j - DailyRollingFileAppender Example

In order to implement daily rolling log files, log4j provides the DailyRollingFileAppender class, which is inheriting from FileAppender class. To use this appender, we need to specify the log file name and a date pattern, for example:


Configure daily rolling log files through log4j’s XML configuration file is as the following example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration 
 PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="">
    <appender name="RollingAppender" 
       <param name="File" value="app.log" />
       <param name="DatePattern" value="'.'yyyy-MM-dd" />
       <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="[%p] %d %c %M - %m%n"/>         
        <priority value="DEBUG"/>
        <appender-ref ref="RollingAppender" />

That's all about how to configure Dialy File Rolling for your Java application using the Log4j library. All you need to do is use the DailyRollingFileAppender class. You can also configure using either properties file or XML file, as shown above. If you face any issue or problem, then please drop us a note. If you want to learn more about loggers, appenders, and layouts in Log4j then you can also see Apache Logging Crash Course on Udemy.

Other Programming Articles you may like
The Complete Java Developer RoadMap
10 Things Java Programmer Should Learn 
10 Programming languages You can Learn
10 Tools Every Java Developer Should Know
10 Reasons to Learn Java Programming languages
10 Frameworks Java and Web Developer should learn
10 Tips to become a better Java Developer
Top 5 Java Frameworks to Learn
10 Reasons to Learn Python for Beginners
10 Testing Libraries Every Java Developer Should Know
How to Crack Spring Professional Certification

Thanks for reading this article so far. If you like this article and found this log4j tip useful, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note. 


  1. how to add date as part of file name(log file name).For example, i should get me file name as log_2022-05-12. I don't know how to add like this could you help with this

  2. Just use this configuration but change the date format as you want, date format is given in the second line:

    <param name="DatePattern" value="'.'yyyy-MM-dd" />

  3. We got the first log in the properties file to roll daily but the second log we are unable to get to roll using the same config


Feel free to comment, ask questions if you have any doubt.