Mercurial Hosting > luan
annotate src/goodjava/logger/DailyRollingFileAppender.java @ 1610:94ea190714dd
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 13 May 2021 17:26:34 -0600 |
parents | 8a717f0b7f45 |
children |
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; |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
4 import java.io.File; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import java.io.IOException; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import java.text.DateFormat; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 import java.util.Date; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 public final class DailyRollingFileAppender extends WriterAppender { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 private final DateFormat format; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 private String fileName; |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
13 public long deleteAfter = 0; |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 public DailyRollingFileAppender(Layout layout,DateFormat format) { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 super(layout,null); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 this.format = format; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 } |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 public synchronized void append(LoggingEvent event) { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 String newFileName = format.format(new Date()); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 if( !newFileName.equals(fileName) ) { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 fileName = newFileName; |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 if( writer != null ) |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 close(); |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
26 sweep(); |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 try { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 writer = new FileWriter(fileName,true); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 } catch(IOException e) { |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 throw new RuntimeException(e); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 } |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 } |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 super.append(event); |
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 } |
1600
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
35 |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
36 private void sweep() { |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
37 if( deleteAfter <= 0 ) |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
38 return; |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
39 long expired = System.currentTimeMillis() - deleteAfter; |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
40 File dir = new File(fileName).getParentFile(); |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
41 for( File file : dir.listFiles() ) { |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
42 if( file.lastModified() < expired ) |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
43 file.delete(); |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
44 } |
8a717f0b7f45
add DailyRollingFileAppender.deleteAfter
Franklin Schmidt <fschmidt@gmail.com>
parents:
1594
diff
changeset
|
45 } |
1594
329197048ff0
add DailyRollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 } |