如果是采用平时用的形如
log4j.appender.logfile.File=d:/log.txt
log4j.appender.logfile.MaxFileSize=1024KB
log4j.appender.logfile.MaxBackupIndex=4
则回出现一个问题,即只能保存一定数量的文件,在这里是只保存4个log文件,超过4个之后之前的信息就丢失了 ,但有时候 要求保留所有的log文件的时候这样的做法就无能为力了;这个时候就不能再用log4j.properties这个文件类配置了,而要用log4j.xml这个配置文件了;文件的大体形式如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n"/>
</layout>
</appender>
-->
<appender name="log" class="com.skywin.mps.crontab.tools.MpsTaskAppender">
<param name="file" value="log/log.txt"/>
<param name="MaxFileSize" value="1024KB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="INFO"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
<appender name="trace" class="com.skywin.mps.crontab.tools.MpsTaskAppender">
<param name="file" value="log/trace.txt"/>
<param name="MaxFileSize" value="1024KB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="DEBUG"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
<appender name="error" class="com.skywin.mps.crontab.tools.MpsTaskAppender">
<param name="file" value="log/error.txt"/>
<param name="MaxFileSize" value="1024KB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="ERROR"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
<root>
<appender-ref ref="error"/><appender-ref ref="log"/><appender-ref ref="trace"/></root>
</log4j:configuration>
其中com.skywin.mps.crontab.tools.MpsTaskAppender是自己写的类
代码如下:
package com.skywin.mps.crontab.tools;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.helpers.CountingQuietWriter;
import org.apache.log4j.helpers.LogLog;
public class MpsTaskAppender extends RollingFileAppender {
public void rollOver() {
LogLog.debug("rolling over count=".concat(String.valueOf(String
.valueOf(((CountingQuietWriter) super.qw).getCount()))));
LogLog.debug("maxBackupIndex=".concat(String.valueOf(String
.valueOf(super.maxBackupIndex))));
int index = super.fileName.lastIndexOf('.');
StringBuffer sb = new StringBuffer(super.fileName);
sb.insert(index, getTimeExt());
File target = new File(sb.toString());
closeFile();
File file = new File(super.fileName);
LogLog
.debug(String.valueOf(String.valueOf((new StringBuffer(
"Renaming file ")).append(file).append(" to ").append(
target))));
file.renameTo(target);
try {
// setFile(super.fileName, false, super.bufferedIO,
// super.bufferSize);
setFile(super.fileName, true, super.bufferedIO, super.bufferSize);
} catch (IOException e) {
LogLog.error(String.valueOf(String.valueOf((new StringBuffer(
"setFile(")).append(super.fileName).append(
", false) call failed."))), e);
}
}
private String getTimeExt() {
String tstr = "_";
// tstr = String.valueOf(tstr) + String.valueOf(StrDate.date2str(new
// Date()));
return tstr + PubTool.date2Str(new Date());
}
private String ext;
}