-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DebugLog.java
55 lines (43 loc) · 1.56 KB
/
DebugLog.java
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
package net.aufdemrand.denizencore.utilities.debugging;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.logging.*;
public class DebugLog extends Logger {
private final static DebugFormatter formatter = new DebugFormatter();
private FileHandler handler;
public DebugLog(String l, String f) {
super(l, null);
try {
handler = new FileHandler(f, true);
addHandler(handler);
setLevel(Level.ALL);
handler.setFormatter(formatter);
}
catch (Exception e) {
System.out.println("Error creating logger '" + l + "': ");
dB.echoError(e);
}
}
public void close() {
if (this.handler != null) {
this.handler.close();
}
}
private static class DebugFormatter extends Formatter {
private final SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
@Override
public String format(LogRecord rec) {
Throwable exception = rec.getThrown();
String out = this.date.format(rec.getMillis());
out += "[" + rec.getLevel().getName().toUpperCase() + "] ";
out += rec.getMessage() + '\n';
if (exception != null) {
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
return out + writer;
}
return out;
}
}
}