diff src/goodjava/logger/DailyRollingFileAppender.java @ 1600:8a717f0b7f45

add DailyRollingFileAppender.deleteAfter
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 19:44:14 -0600
parents 329197048ff0
children
line wrap: on
line diff
--- a/src/goodjava/logger/DailyRollingFileAppender.java	Mon Apr 05 00:13:05 2021 -0600
+++ b/src/goodjava/logger/DailyRollingFileAppender.java	Fri Apr 09 19:44:14 2021 -0600
@@ -1,6 +1,7 @@
 package goodjava.logger;
 
 import java.io.FileWriter;
+import java.io.File;
 import java.io.IOException;
 import java.text.DateFormat;
 import java.util.Date;
@@ -9,6 +10,7 @@
 public final class DailyRollingFileAppender extends WriterAppender {
 	private final DateFormat format;
 	private String fileName;
+	public long deleteAfter = 0;
 
 	public DailyRollingFileAppender(Layout layout,DateFormat format) {
 		super(layout,null);
@@ -21,6 +23,7 @@
 			fileName = newFileName;
 			if( writer != null )
 				close();
+			sweep();
 			try {
 				writer = new FileWriter(fileName,true);
 			} catch(IOException e) {
@@ -29,4 +32,15 @@
 		}
 		super.append(event);
 	}
+
+	private void sweep() {
+		if( deleteAfter <= 0 )
+			return;
+		long expired = System.currentTimeMillis() - deleteAfter;
+		File dir = new File(fileName).getParentFile();
+		for( File file : dir.listFiles() ) {
+			if( file.lastModified() < expired )
+				file.delete();
+		}
+	}
 }