废话不说,直接上代码
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();
}
}
}
}

本文介绍了一个定制的Log4j DailyRollingFileAppender类实现,该类在原有基础上增加了确保日志文件目录存在、设置缓冲区大小等功能,并在系统关闭时强制刷新日志缓存。
1008

被折叠的 条评论
为什么被折叠?



