[Log4j] using the extended package log4j RollingFileAppender generate a log file timestamped

Use log4j expansion pack RollingFileAppender generate a log file timestamped

Blog Category:   the Java

log4jApacheXMLHTML 

DailyRollingFileAppender resulting file is not time-stamped, must, after a certain point in time, my son had to rename a file-stamped. 
For example timestamps accurate to the hour, the format .yyyy-MM-dd-HH, 2011-05-05 current time is 5:00, then the logs for 
log 
after the time becomes 6:00 (6:00 and have access to the log), the log to 
log 
log.2011-05-05-05 

because log4j event is triggered if a certain period of time does not log access, even to the point of time, it will not add a timestamp to rename. For example, two days later have access to the log, then only produce log.2011-05-05-05 this file. This will cause the log statistics, statistics not see this document. 

The solution is to generate a log file when it is already stamped. Such as 5:00, when the log is already log.2011-05-05-05. Original log4j does not implement this feature, you need to implement a succession FileAppender own Appender. I looked at the source code, you can refer DailyRollingFileAppender, modify its constructor and rollOver. I did not go to experiment, do not know can not succeed. 

apache-log4j-extras log4j is extended package, which can achieve this requirement TimeBasedRollingPolicy. 
Required packages: 
log4j-1.2.15.jar 
Apache-log4j-Extras-1.0.jar 
Note: log4j 1.2.15 must be above 14 not 

log4j Properties profile can not be used, must be used xml. Configuration, see: 

Xml Code  Collection Code

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
  3. <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>  
  4.      <!-- appender -->  
  5.      <!-- STDOUT -->  
  6.      <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">  
  7.           <layout class="org.apache.log4j.PatternLayout">  
  8.                <param name="ConversionPattern" value="%d %p [%c] - %m%n" />  
  9.           </layout>  
  10.      </appender>  
  11.   
  12.      <!-- FILE -->  
  13.      <appender name="FILE"  
  14.           class="org.apache.log4j.rolling.RollingFileAppender">  
  15.           <rollingPolicy  
  16.                class="org.apache.log4j.rolling.TimeBasedRollingPolicy">  
  17.                <param name="FileNamePattern"  
  18.                     value="d:/logs/file.%d{yyyy-MM-dd-HH}" />  
  19.           </rollingPolicy>  
  20.           <layout class="org.apache.log4j.PatternLayout">  
  21.                <param name="ConversionPattern"  
  22.                     value="%m%n" />  
  23.           </layout>  
  24.      </appender>  
  25.       
  26.      <logger name="view">  
  27.           <level value="info" />  
  28.           <appender-ref ref="FILE" />  
  29.      </logger>  
  30.   
  31.      <root>  
  32.           <level value="info" />  
  33.           <appender-ref ref="STDOUT" />  
  34.      </root>  
  35. </log4j:configuration>  


Note: org.apache.log4j.rolling.RollingFileAppender is rolling under the package, rather than the original org.apache.log4j.RollingFileAppender 

test case: 

Java code  Collection Code

  1. public static void main(String[] args) {  
  2.            Logger logger = Logger.getLogger("view");  
  3.            logger.info("test");  
  4. }  



The use of reference TimeBasedRollingPolicy: 
http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html

Guess you like

Origin blog.csdn.net/suifeng629/article/details/93847726