diff src/goodjava/logger/DailyRollingFileAppender.java @ 1594:329197048ff0

add DailyRollingFileAppender
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 21 Mar 2021 00:59:58 -0600
parents
children 8a717f0b7f45
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/logger/DailyRollingFileAppender.java	Sun Mar 21 00:59:58 2021 -0600
@@ -0,0 +1,32 @@
+package goodjava.logger;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.util.Date;
+
+
+public final class DailyRollingFileAppender extends WriterAppender {
+	private final DateFormat format;
+	private String fileName;
+
+	public DailyRollingFileAppender(Layout layout,DateFormat format) {
+		super(layout,null);
+		this.format = format;
+	}
+
+	public synchronized void append(LoggingEvent event) {
+		String newFileName = format.format(new Date());
+		if( !newFileName.equals(fileName) ) {
+			fileName = newFileName;
+			if( writer != null )
+				close();
+			try {
+				writer = new FileWriter(fileName,true);
+			} catch(IOException e) {
+				throw new RuntimeException(e);
+			}
+		}
+		super.append(event);
+	}
+}