Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

java - Log file name to include current date in log4j

The question is a subset of this. I want a log file to be created everyday with the log file name format as follows: downloadmanageryyyy-MM-dd.log
Using DailyRollingAppenderbut the log file is not created at all.

My lo4j.xml looks like this:

 <?xml version="1.0" encoding="UTF-8" ?>
                         <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
   <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
   <appender name="InfoLogFile" class="org.apache.log4j.DailyRollingFileAppender">
       <param name="File" value="downloadmanager.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>  
  </log4j:configuration>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...