annotate src/goodjava/logger/RollingFileAppender.java @ 1804:b4328322d2be

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 07 May 2024 22:08:37 -0600
parents 97cc73664ca8
children
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 {
1682
97cc73664ca8 improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
10 protected final String[] fileNames;
1449
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
1682
97cc73664ca8 improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
14 public RollingFileAppender(Layout layout,String[] fileNames) throws IOException {
1449
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 open();
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 protected void open() throws IOException {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 this.writer = new FileWriter(file,true);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 public synchronized void append(LoggingEvent event) {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 super.append(event);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if( file.length() > maxFileSize ) {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 rollOver();
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 }
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 protected void rollOver() {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 try {
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
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
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
38 File f = backup;
1682
97cc73664ca8 improve RollingFileAppender
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
39 backup = new File(fileNames[i]);
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
40 IoUtils.move(backup,f);
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
41 }
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1449
diff changeset
42 IoUtils.move(file,backup);
1449
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 open();
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 } catch(IOException e) {
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 throw new RuntimeException(e);
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
dd14d2dce7ee add appenders
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }