AutomaticLogFileRemoval.java
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.util.AbstractPath
* com.scene7.is.util.text.ParamAccess
* com.scene7.is.util.text.ParameterException
* com.scene7.is.util.text.ParsingException
*/
package com.scene7.is.ps.provider;
import com.scene7.is.util.AbstractPath;
import com.scene7.is.util.text.ParamAccess;
import com.scene7.is.util.text.ParameterException;
import com.scene7.is.util.text.ParsingException;
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
final class AutomaticLogFileRemoval
extends Thread {
private static final int REFRESH_INTERVAL_1HOUR = 3600000;
private File logDir;
private MaxDaysFileFilter filter;
private String loggerName;
private Logger getGlobalLogger() {
return Logger.getLogger(this.loggerName);
}
public AutomaticLogFileRemoval(String prefix, ParamAccess params) throws ParameterException, ParsingException {
super("AutomaticLogFileRemoval");
this.loggerName = prefix + ".trace";
AbstractPath loggingRoot = new AbstractPath(params.getAsString("rootPath"));
this.logDir = new File(loggingRoot.toString());
long maxDays = params.getAsInt("count");
this.filter = new MaxDaysFileFilter(maxDays);
if (maxDays > 0) {
this.start();
}
}
public void dispose() {
this.getGlobalLogger().finer("Stopping AutomaticLogFileRemoval...");
this.interrupt();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void run() {
try {
this.getGlobalLogger().log(Level.INFO, "AutomaticLogFileRemoval thread started");
while (!this.isInterrupted()) {
try {
this.removeOldLogFiles();
}
catch (Throwable e) {
this.getGlobalLogger().log(Level.SEVERE, "Exception during automatic log file removal. Ignored", e);
}
Thread.sleep(3600000);
}
}
catch (InterruptedException e) {
this.getGlobalLogger().log(Level.FINEST, "AutomaticLogFileRemoval thread interrupted", e);
}
finally {
this.getGlobalLogger().log(Level.INFO, "AutomaticLogFileRemoval thread exited");
}
}
private void removeOldLogFiles() {
File[] files = this.logDir.listFiles(this.filter);
for (int i = 0; files != null && i < files.length; ++i) {
if (files[i].delete()) {
this.getGlobalLogger().log(Level.INFO, "Removed old log file: " + files[i]);
continue;
}
this.getGlobalLogger().log(Level.WARNING, "Unable to remove old log file: " + files[i]);
}
}
private final class MaxDaysFileFilter
implements FileFilter {
private static final long NUM_MILLISECONDS_PER_DAY = 86400000;
private long maxDays;
MaxDaysFileFilter(long maxDays) {
this.maxDays = maxDays;
}
@Override
public boolean accept(File candidate) {
return candidate.isFile() && candidate.getName().endsWith(".log") && this.getAge(candidate) >= this.maxDays;
}
private long getAge(File file) {
long lastModified = file.lastModified();
if (lastModified != 0) {
long now = new Date().getTime();
return (now - lastModified) / 86400000;
}
return 0;
}
}
}