annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1594
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.logger;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.FileWriter;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.IOException;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.text.DateFormat;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.Date;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 public final class DailyRollingFileAppender extends WriterAppender {
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 private final DateFormat format;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 private String fileName;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 public DailyRollingFileAppender(Layout layout,DateFormat format) {
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 super(layout,null);
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 this.format = format;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public synchronized void append(LoggingEvent event) {
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 String newFileName = format.format(new Date());
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 if( !newFileName.equals(fileName) ) {
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 fileName = newFileName;
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 if( writer != null )
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 close();
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 try {
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 writer = new FileWriter(fileName,true);
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 } catch(IOException e) {
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 throw new RuntimeException(e);
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 }
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 }
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 super.append(event);
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
329197048ff0 add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }