废话不说,直接上代码
public class DailyRollingFileAppender extends
org.apache.log4j.DailyRollingFileAppender {
public DailyRollingFileAppender() {
super();
Runtime.getRuntime().addShutdownHook(new Log4jHockThread());
}
public DailyRollingFileAppender(Layout layout, String filename,
String datePattern) throws IOException {
super(layout, filename, datePattern);
Runtime.getRuntime().addShutdownHook(new Log4jHockThread());
}
@Override
public synchronized void setFile(String fileName, boolean append,
boolean bufferedIO, int bufferSize) throws IOException {
File logfile = new File(fileName);
logfile.getParentFile().mkdirs();
super.setFile(fileName, append, bufferedIO, bufferSize);
}
public QuietWriter getQw() {
return super.qw;
}
private class Log4jHockThread extends Thread {
@Override
public void run() {
QuietWriter qw = DailyRollingFileAppender.this.getQw();
if (qw != null) {
qw.flush();
}
}
}
}