changeset 1600:8a717f0b7f45

add DailyRollingFileAppender.deleteAfter
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 19:44:14 -0600
parents f2a663a4ba9e
children de5a81327d9c
files src/goodjava/logger/DailyRollingFileAppender.java src/goodjava/webserver/handlers/LogHandler.java src/luan/host/WebHandler.java
diffstat 3 files changed, 19 insertions(+), 3 deletions(-) [+]
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();
+		}
+	}
 }
--- a/src/goodjava/webserver/handlers/LogHandler.java	Mon Apr 05 00:13:05 2021 -0600
+++ b/src/goodjava/webserver/handlers/LogHandler.java	Fri Apr 09 19:44:14 2021 -0600
@@ -31,7 +31,7 @@
 		return logger;
 	}
 
-	public static Logger dirLogger(File dir) {
+	public static Logger dirLogger(File dir,long deleteAfter) {
 		if( dir.exists() && !dir.isDirectory() )
 			throw new RuntimeException("must be a directory");
 		try {
@@ -40,7 +40,8 @@
 			throw new RuntimeException(e);
 		}
 		DateFormat fmt = new SimpleDateFormat( "'" + dir + "/'yyyy_MM_dd'.log'" );
-		Appender appender = new DailyRollingFileAppender(layout,fmt);
+		DailyRollingFileAppender appender = new DailyRollingFileAppender(layout,fmt);
+		appender.deleteAfter = deleteAfter;
 		GoodLogger logger = new GoodLogger("HTTP");
 		logger.appender = appender;
 		logger.level = Level.INFO;
--- a/src/luan/host/WebHandler.java	Mon Apr 05 00:13:05 2021 -0600
+++ b/src/luan/host/WebHandler.java	Fri Apr 09 19:44:14 2021 -0600
@@ -27,6 +27,7 @@
 
 public class WebHandler implements Handler {
 	private static final Logger logger = LoggerFactory.getLogger(WebHandler.class);
+	private static final long days30 = 1000L*60*60*24*30;
 
 	private static final class MyHandler implements Handler {
 		private final Handler handler;
@@ -65,7 +66,7 @@
 			handler = new ListHandler( handler, notFoundHander );
 			handler = new ContentTypeHandler(handler);
 			handler = new SafeHandler(handler);
-			handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir)));
+			handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir),days30));
 
 			return new MyHandler(handler,luanHandler);
 		}