Setting a log file name to include current date in Log4j
DailyRollingFileAppender is what you exactly searching for.
<appender name="roll" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="application.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"/>
</layout>
</appender>
Create log file with date using log4j
As is mentioned in this StackOverflow Q&A, the purpose of a RollingFileAppender
is to automatically create a new log file at some defined interval. In the case of the DailyRollingFileAppender
, that interval is 12:00 AM of each day.
What this means is that the first file created by log4j will have the file name you specified here:
log4j.appender.FILE.File=log4j/QueryLog.log
And, from then forward, each day a new log file will be created with the date appended to it.
To always name the file with the date appended, you could use DatedFileAppender
by Geoff Mottram
Log file name to include current date in log4j
The DailyRollingFileAppender
shipping with log4j will not rename the log file until the first message is logged some time after midnight.
You may try to use DatedFileAppender
, which can be download from here. Contrary to the DailyRollingFileAppender
, it will create the log file whose filename always contains today's date.
Filename with date in Log4j
I think you could just set a system property in code to contain the current date:
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.setProperty("current.date", dateFormat.format(new Date()));
}
Then in your log4j.xml file you can use the system property when specifying the log file name in the appender:
<appender name="MYAPPENDER" class="org.apache.log4j.FileAppender">
<param name="File" value="${user.home}/myApp-${current.date}.log" />
ETA: Now that I think about it you may have to setup the system property using a static initializer to make sure the property is set before log4j is configured.
Unable to append the date to the file name using log4j DailyRollingFileAppender
If you're using log4j 1.x, we strongly recommend that you use org.apache.log4j.rolling.RollingFileAppender
1 instead of org.apache.log4j.DailyRollingFileAppender
(may lose messages, Bug 43374).
So the configuration of you appender can be:
log4j.rootLogger = DEBUG, rollingAppender
log4j.appender.rollingAppender=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.rollingAppender.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.rollingAppender.rollingPolicy.fileNamePattern=F:/temp/app%d{yyyy-MM-dd}.log
log4j.appender.rollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
Notes
- In that case, you need to add the respective jar (apache-log4j-extras-1.2.17.jar).
Related Topics
How to Create Multiple Objects Through Loops in Java
Appbar Not Showing Back Button
How to Get an Http Response Body as a String
Android Get Height of Webview Content Once Rendered
Spring Data Repository @Query - Update and Return Modified Entity
Spring Boot Return 204 No-Content When List Is Empty
How to Remove the Slashes from Json Key While Converting Object into String
Check If Two Objects Are Equal Excluding a Few Properties
How to Externalize Application.Properties in Tomcat Webserver for Spring
How to Read a File, Reverse the Order, and Write Reverse Order
Setting a Log File Name to Include Current Date in Log4J
How to Read All Classes from a Java Package in the Classpath
Jpa Repository Query - Java.Lang.Object; Cannot Be Cast to Model
How to Post Data in an Array in Android Using Retrofit 2