Mercurial Hosting > luan
comparison src/goodjava/logger/RollingFileAppender.java @ 1473:6c6ce14db6a8
add goodjava.io
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 17 Apr 2020 13:56:57 -0600 |
parents | dd14d2dce7ee |
children | 97cc73664ca8 |
comparison
equal
deleted
inserted
replaced
1472:60f6741f000a | 1473:6c6ce14db6a8 |
---|---|
1 package goodjava.logger; | 1 package goodjava.logger; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.FileWriter; | 4 import java.io.FileWriter; |
5 import java.io.IOException; | 5 import java.io.IOException; |
6 import goodjava.io.IoUtils; | |
6 | 7 |
7 | 8 |
8 public class RollingFileAppender extends WriterAppender { | 9 public class RollingFileAppender extends WriterAppender { |
9 protected final String fileName; | 10 protected final String fileName; |
10 protected final File file; | 11 protected final File file; |
30 } | 31 } |
31 | 32 |
32 protected void rollOver() { | 33 protected void rollOver() { |
33 close(); | 34 close(); |
34 File backup = new File(fileName+'.'+backups); | 35 File backup = new File(fileName+'.'+backups); |
35 if( backup.exists() ) { | |
36 if( !backup.delete() ) | |
37 debug("couldn't delete "+backup); | |
38 } | |
39 for( int i=backups-1; i>=1; i-- ) { | |
40 File f = backup; | |
41 backup = new File(fileName+'.'+i); | |
42 backup.renameTo(f); | |
43 } | |
44 if( !file.renameTo(backup) ) | |
45 debug("couldn't rename "+file+" to "+backup); | |
46 try { | 36 try { |
37 IoUtils.delete(backup); | |
38 for( int i=backups-1; i>=1; i-- ) { | |
39 File f = backup; | |
40 backup = new File(fileName+'.'+i); | |
41 IoUtils.move(backup,f); | |
42 } | |
43 IoUtils.move(file,backup); | |
47 open(); | 44 open(); |
48 } catch(IOException e) { | 45 } catch(IOException e) { |
49 throw new RuntimeException(e); | 46 throw new RuntimeException(e); |
50 } | 47 } |
51 } | 48 } |
52 | |
53 protected void debug(String msg) { | |
54 System.err.println("RollingFileAppender: "+msg); | |
55 } | |
56 } | 49 } |