package myprog.util;import java.text.SimpleDateFormat;import java.util.Date; import org.apache.log4j.FileAppender; public class DateFileAppender extends FileAppender { private String datePattern = "yyyyMMddhhmm"; public void setFile(String strFile) { String strDate; SimpleDateFormat sdf; if (datePattern!=null && strFile!=null) { sdf = new SimpleDateFormat(datePattern); strDate = sdf.format(new Date()); fileName = strFile.replaceAll("%date%", strDate); } else { System.err.println("Either File or DatePattern options are not set for appender [" + name + "]."); } } public String getDatePattern() { return datePattern; } public void setDatePattern(String datePattern) { this.datePattern = datePattern; }}
Pour l'utiliser dans le fichier log4j
log4j.appender.F1=myprog.util.DateFileAppenderlog4j.appender.F1.File=log/extract-generated-%date%.loglog4j.appender.F1.layout=org.apache.log4j.PatternLayoutlog4j.appender.F1.layout.ConversionPattern=%m%n