annotate src/goodjava/logger/RollingFileAppender.java @ 1660:2968e43cdd44

manual work
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 19 Apr 2022 23:46:06 -0600
parents 6c6ce14db6a8
children 97cc73664ca8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1449
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.logger;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.FileWriter;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.IOException;
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
6 import goodjava.io.IoUtils;
1449
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 public class RollingFileAppender extends WriterAppender {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 protected final String fileName;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 protected final File file;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public long maxFileSize = 10*1024*1024;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 public int backups = 1;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public RollingFileAppender(Layout layout,String fileName) throws IOException {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 super(layout,null);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 this.fileName = fileName;
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 this.file = new File(fileName);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 open();
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 protected void open() throws IOException {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 this.writer = new FileWriter(file,true);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 public synchronized void append(LoggingEvent event) {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 super.append(event);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if( file.length() > maxFileSize ) {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 rollOver();
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 protected void rollOver() {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 close();
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 File backup = new File(fileName+'.'+backups);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 try {
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
37 IoUtils.delete(backup);
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
38 for( int i=backups-1; i>=1; i-- ) {
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
39 File f = backup;
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
40 backup = new File(fileName+'.'+i);
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
41 IoUtils.move(backup,f);
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
42 }
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
43 IoUtils.move(file,backup);
1449
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 open();
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 } catch(IOException e) {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 throw new RuntimeException(e);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }