Log4j
log file naming
current date
logging
Java

Setting a log file name to include current date in Log4j

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

If you want a Log4j log file name to include the current date, the first question is whether you need a dated active file or simply daily log rotation. In most applications, the better answer is to keep one stable active file and let Log4j roll it into date-stamped archive files.

Prefer Date-Based Rollover

For long-running services, a fixed active file path is easier for operational tooling. Log shippers, monitoring agents, and container sidecars often expect one path such as logs/app.log. Log4j can keep that active file while archiving older logs with the date in the rolled file name.

In Log4j 2, use a RollingFile appender with a date in filePattern:

xml
1<?xml version="1.0" encoding="UTF-8"?>
2<Configuration status="WARN">
3  <Appenders>
4    <RollingFile
5        name="AppFile"
6        fileName="logs/application.log"
7        filePattern="logs/application-%d{yyyy-MM-dd}.log.gz">
8      <PatternLayout pattern="%d{ISO8601} %-5p %c - %m%n"/>
9      <Policies>
10        <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
11      </Policies>
12    </RollingFile>
13  </Appenders>
14
15  <Loggers>
16    <Root level="info">
17      <AppenderRef ref="AppFile"/>
18    </Root>
19  </Loggers>
20</Configuration>

With this setup:

  • new log messages go to logs/application.log
  • Log4j rolls the file on the configured schedule
  • archived files are named with the current date

That usually satisfies the real requirement without making the active path move every day.

If the Active File Must Contain the Date

Some batch jobs and one-shot processes really do want the live file name to contain the run date. In that case, inject the value as a system property when the JVM starts.

Run the application like this:

bash
java -DlogDate=2026-03-08 -jar app.jar

Then reference the property in your Log4j configuration:

xml
1<?xml version="1.0" encoding="UTF-8"?>
2<Configuration status="WARN">
3  <Appenders>
4    <File name="AppFile" fileName="logs/application-${sys:logDate}.log">
5      <PatternLayout pattern="%d{ISO8601} %-5p %c - %m%n"/>
6    </File>
7  </Appenders>
8
9  <Loggers>
10    <Root level="info">
11      <AppenderRef ref="AppFile"/>
12    </Root>
13  </Loggers>
14</Configuration>

This works, but it is a startup-time value. If the process runs across midnight, the filename does not change automatically unless you also configure rollover.

Log4j 1.x and Legacy Systems

Older examples on the internet often use Log4j 1.x and DailyRollingFileAppender. That approach still appears in maintenance work, but Log4j 2 has the cleaner configuration model.

A typical legacy configuration looked like this:

properties
1log4j.rootLogger=INFO, FILE
2log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
3log4j.appender.FILE.File=logs/application.log
4log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
5log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
6log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c - %m%n

If you are maintaining an old application, this may still be relevant. For new work, prefer Log4j 2 rolling appenders.

Choosing the Right Strategy

A simple rule works well:

  • use a stable fileName plus date-based filePattern for services and web apps
  • use a startup property for dated active files in short batch runs

The choice should match how the logs are consumed. If another tool tails one specific file path, changing the active filename every day may create unnecessary complexity.

Common Pitfalls

A common mistake is putting the date into fileName and assuming Log4j will automatically create a new daily file. It will not. That value is just the active target unless rollover is configured separately.

Another mistake is copying Log4j 1.x examples into a Log4j 2 application. The names are similar, but the appenders and configuration structure differ.

A third issue is forgetting that the current date may need to be computed outside Log4j if you insist on a dated active filename. In that case, the JVM startup command or deployment script becomes part of the logging design.

Summary

  • For most applications, keep a stable active log file and use date-based rollover
  • In Log4j 2, RollingFile plus filePattern is the standard solution
  • Use a startup property only when the active filename itself must contain the date
  • Be careful not to mix Log4j 1.x and Log4j 2 configuration examples
  • Design the filename strategy around how your logs are collected and rotated

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.