-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathSystemOutLoggerFactory.java
113 lines (87 loc) · 3.21 KB
/
SystemOutLoggerFactory.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
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
112
113
package net.adoptopenjdk.icedteaweb.logging;
import net.adoptopenjdk.icedteaweb.logging.LoggerFactory.LoggerFactoryImpl;
import java.io.PrintStream;
import java.util.Date;
import static net.adoptopenjdk.icedteaweb.OutputUtils.exceptionToString;
/**
* Implementation of Logger which loges to {@link System#out}.
*/
class SystemOutLoggerFactory implements LoggerFactoryImpl {
private static final PrintStream out;
static {
final PrintStream tmp = System.out;
if (tmp.getClass().getSimpleName().contains("TeeOutputStream")) {
throw new RuntimeException("System.out is already a tee stream");
}
out = tmp;
}
@Override
public Logger getLogger(final Class<?> forClass) {
return new SystemOutLogger(forClass);
}
private static class SystemOutLogger extends BaseLogger {
private static final String DEBUG = "[DEBUG ]";
private static final String INFO = "[INFO ]";
private static final String WARNING = "[WARNING]";
private static final String ERROR = "[ERROR ]";
private final String forClass;
SystemOutLogger(final Class<?> forClass) {
this.forClass = forClass.getName();
}
@Override
public void debug(final String msg) {
log(DEBUG, msg, null);
}
@Override
public void debug(final String msg, final Object... arguments) {
log(DEBUG, doExpand(msg, arguments), null);
}
@Override
public void debug(final String msg, final Throwable t) {
log(DEBUG, msg, t);
}
@Override
public void info(final String msg) {
log(INFO, msg, null);
}
@Override
public void info(final String msg, final Object... arguments) {
log(INFO, doExpand(msg, arguments), null);
}
@Override
public void info(final String msg, final Throwable t) {
log(INFO, msg, t);
}
@Override
public void warn(final String msg) {
log(WARNING, msg, null);
}
@Override
public void warn(final String msg, final Object... arguments) {
log(WARNING, doExpand(msg, arguments), null);
}
@Override
public void warn(final String msg, final Throwable t) {
log(WARNING, msg, t);
}
@Override
public void error(final String msg) {
log(ERROR, msg, null);
}
@Override
public void error(final String msg, final Object... arguments) {
log(ERROR, doExpand(msg, arguments), null);
}
@Override
public void error(final String msg, final Throwable t) {
log(ERROR, msg, t);
}
private void log(final String level, final String msg, final Throwable t) {
final boolean isMultiLine = msg.contains("\n") || t != null;
final String separator = isMultiLine ? "\n" : " ";
final String header = new Date().toString() + " " + level + " " + forClass + ":";
final String line = header + separator + msg + (t != null ? separator + exceptionToString(t) : "");
out.println(line);
}
}
}