Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1593:ea62c2316b2e | 1594:329197048ff0 |
---|---|
1 package goodjava.logger; | |
2 | |
3 import java.io.FileWriter; | |
4 import java.io.IOException; | |
5 import java.text.DateFormat; | |
6 import java.util.Date; | |
7 | |
8 | |
9 public final class DailyRollingFileAppender extends WriterAppender { | |
10 private final DateFormat format; | |
11 private String fileName; | |
12 | |
13 public DailyRollingFileAppender(Layout layout,DateFormat format) { | |
14 super(layout,null); | |
15 this.format = format; | |
16 } | |
17 | |
18 public synchronized void append(LoggingEvent event) { | |
19 String newFileName = format.format(new Date()); | |
20 if( !newFileName.equals(fileName) ) { | |
21 fileName = newFileName; | |
22 if( writer != null ) | |
23 close(); | |
24 try { | |
25 writer = new FileWriter(fileName,true); | |
26 } catch(IOException e) { | |
27 throw new RuntimeException(e); | |
28 } | |
29 } | |
30 super.append(event); | |
31 } | |
32 } |