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.
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:
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:
Then reference the property in your Log4j 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:
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
fileNameplus date-basedfilePatternfor 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,
RollingFileplusfilePatternis 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
- setting image pull policy using kubectl
- Setting queueSize parameter for ch.qos.logback.classic.AsyncAppender
- Setting secrets as environment variables in deployment file
- Setting the capability for aws cloudformation template-validate
- Setting active profile and config location from command line in Spring Boot
- Setting active profile and config location from command line in Spring Boot
- Setting up Kubernetes on NixOS
- Setup Kubernetes Pods via API Call using Go and Operator SDK

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.