/
JDKFormatter.java
45 lines (37 loc) · 1.4 KB
/
JDKFormatter.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
package unquietcode.tools.logmachine.impl.jdk14;
import unquietcode.tools.logmachine.core.LogEvent;
import unquietcode.tools.logmachine.core.Switchboard;
import unquietcode.tools.logmachine.core.formats.Formatter;
import unquietcode.tools.logmachine.core.formats.PlaintextFormatter;
import java.util.logging.LogRecord;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Adaptor which allows log machine formats to be used as a formatter for
* an existing {@link java.util.logging.Handler}.
*
* @author Ben Fagin
* @version 10-24-2012
*/
public class JDKFormatter extends java.util.logging.Formatter {
private Formatter formatter = new PlaintextFormatter();
private java.util.logging.Formatter fallbackFormater;
public void setFormatter(Formatter formatter) {
this.formatter = checkNotNull(formatter, "formatter cannot be null");
}
public void setFallbackFormater(java.util.logging.Formatter formatter) {
this.fallbackFormater = checkNotNull(formatter, "formatter cannot be null");
}
@Override
public String format(LogRecord event) {
String lookupKey = "_"+event.getSequenceNumber();
LogEvent _event = Switchboard.get(lookupKey);
StringBuilder data = _event != null ? formatter.format(_event) : null;
if (data != null) {
return data.append("\n").toString();
} else if (fallbackFormater != null) {
return fallbackFormater.format(event);
} else {
return "";
}
}
}