Ich habe einen Appender erstellt, der das erledigt. http://stauffer.james.googlepages.com/DateFormatFileAppender.java
package sps.log.log4j;
import java.io.IOException;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.*;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;
public class DateFormatFileAppender extends FileAppender {
public DateFormatFileAppender() {
}
public DateFormatFileAppender (Layout layout, String filename) throws IOException {
super(layout, filename, true);
}
private String fileBackup;
private boolean separate = false;
public void setFile(String file) {
super.setFile(file);
this.fileBackup = getFile();
}
public void setSeparate(boolean separate) {
this.separate = separate;
}
protected void subAppend(LoggingEvent event) {
if(separate) {
try {
setFile(getFile(), getAppend(), getBufferedIO(), getBufferSize());
} catch(IOException e) {
LogLog.error("Unable to reset fileName.");
}
}
super.subAppend(event);
}
public
synchronized
void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat(fileBackup);
String actualFileName = sdf.format(new Date());
makeDirs(actualFileName);
super.setFile(actualFileName, append, bufferedIO, bufferSize);
}
private void makeDirs (String path) {
int indexSlash = path.lastIndexOf("/");
int indexBackSlash = path.lastIndexOf("\\");
int index = Math.max(indexSlash, indexBackSlash);
if(index > 0) {
String dirs = path.substring(0, index);
File dir = new File(dirs);
if(!dir.exists()) {
boolean success = dir.mkdirs();
if(!success) {
LogLog.error("Unable to create directories for " + dirs);
}
}
}
}
}