comparison src/goodjava/logger/RollingFileAppender.java @ 1682:97cc73664ca8

improve RollingFileAppender
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Jun 2022 10:51:43 -0600
parents 6c6ce14db6a8
children
comparison
equal deleted inserted replaced
1681:6061be2cd84b 1682:97cc73664ca8
5 import java.io.IOException; 5 import java.io.IOException;
6 import goodjava.io.IoUtils; 6 import goodjava.io.IoUtils;
7 7
8 8
9 public class RollingFileAppender extends WriterAppender { 9 public class RollingFileAppender extends WriterAppender {
10 protected final String fileName; 10 protected final String[] fileNames;
11 protected final File file; 11 protected final File file;
12 public long maxFileSize = 10*1024*1024; 12 public long maxFileSize = 10*1024*1024;
13 public int backups = 1;
14 13
15 public RollingFileAppender(Layout layout,String fileName) throws IOException { 14 public RollingFileAppender(Layout layout,String[] fileNames) throws IOException {
16 super(layout,null); 15 super(layout,null);
17 this.fileName = fileName; 16 this.fileNames = fileNames;
18 this.file = new File(fileName); 17 this.file = new File(fileNames[0]);
19 open(); 18 open();
20 } 19 }
21 20
22 protected void open() throws IOException { 21 protected void open() throws IOException {
23 this.writer = new FileWriter(file,true); 22 this.writer = new FileWriter(file,true);
30 } 29 }
31 } 30 }
32 31
33 protected void rollOver() { 32 protected void rollOver() {
34 close(); 33 close();
35 File backup = new File(fileName+'.'+backups); 34 File backup = new File(fileNames[fileNames.length-1]);
36 try { 35 try {
37 IoUtils.delete(backup); 36 IoUtils.delete(backup);
38 for( int i=backups-1; i>=1; i-- ) { 37 for( int i=fileNames.length-2; i>=1; i-- ) {
39 File f = backup; 38 File f = backup;
40 backup = new File(fileName+'.'+i); 39 backup = new File(fileNames[i]);
41 IoUtils.move(backup,f); 40 IoUtils.move(backup,f);
42 } 41 }
43 IoUtils.move(file,backup); 42 IoUtils.move(file,backup);
44 open(); 43 open();
45 } catch(IOException e) { 44 } catch(IOException e) {