Mercurial Hosting > luan
annotate src/goodjava/logger/RollingFileAppender.java @ 1773:b914a726061f
video multipart
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Jul 2023 11:56:40 -0600 |
parents | 97cc73664ca8 |
children |
rev | line source |
---|---|
1449 | 1 package goodjava.logger; |
2 | |
3 import java.io.File; | |
4 import java.io.FileWriter; | |
5 import java.io.IOException; | |
1473 | 6 import goodjava.io.IoUtils; |
1449 | 7 |
8 | |
9 public class RollingFileAppender extends WriterAppender { | |
1682
97cc73664ca8
improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1473
diff
changeset
|
10 protected final String[] fileNames; |
1449 | 11 protected final File file; |
12 public long maxFileSize = 10*1024*1024; | |
13 | |
1682
97cc73664ca8
improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1473
diff
changeset
|
14 public RollingFileAppender(Layout layout,String[] fileNames) throws IOException { |
1449 | 15 super(layout,null); |
1682
97cc73664ca8
improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1473
diff
changeset
|
16 this.fileNames = fileNames; |
97cc73664ca8
improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1473
diff
changeset
|
17 this.file = new File(fileNames[0]); |
1449 | 18 open(); |
19 } | |
20 | |
21 protected void open() throws IOException { | |
22 this.writer = new FileWriter(file,true); | |
23 } | |
24 | |
25 public synchronized void append(LoggingEvent event) { | |
26 super.append(event); | |
27 if( file.length() > maxFileSize ) { | |
28 rollOver(); | |
29 } | |
30 } | |
31 | |
32 protected void rollOver() { | |
33 close(); | |
1682
97cc73664ca8
improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1473
diff
changeset
|
34 File backup = new File(fileNames[fileNames.length-1]); |
1449 | 35 try { |
1473 | 36 IoUtils.delete(backup); |
1682
97cc73664ca8
improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1473
diff
changeset
|
37 for( int i=fileNames.length-2; i>=1; i-- ) { |
1473 | 38 File f = backup; |
1682
97cc73664ca8
improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents:
1473
diff
changeset
|
39 backup = new File(fileNames[i]); |
1473 | 40 IoUtils.move(backup,f); |
41 } | |
42 IoUtils.move(file,backup); | |
1449 | 43 open(); |
44 } catch(IOException e) { | |
45 throw new RuntimeException(e); | |
46 } | |
47 } | |
48 } |