-
Notifications
You must be signed in to change notification settings - Fork 13
/
Logger.java
311 lines (249 loc) · 9.88 KB
/
Logger.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.vividsolutions.jump.workbench;
import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.PatternLayout;
import com.vividsolutions.jump.workbench.ui.plugin.GenerateLogPlugIn;
/**
* a generalized logger interface for OJ package usage currently based on log4j
* v1.2
*
* TODO: - move to commons-logging to be more implementation independent
* -implement class:line logging for legacy log4j using code
*
* @author ed
*
*/
public class Logger {
public static class Level {
private String VALUE = "";
private Level(String value) {
VALUE = value;
}
public String toString() {
return VALUE;
}
final static public Level FATAL = new Level("FATAL");
final static public Level ERROR = new Level("ERROR");
final static public Level WARN = new Level("WARN");
final static public Level INFO = new Level("INFO");
final static public Level DEBUG = new Level("DEBUG");
final static public Level TRACE = new Level("TRACE");
}
private static boolean initialized = false;
private static void init(){
if (initialized)
return;
// just in case log4j init failed add a default console appender for us to see errors printed
org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
if (!rootLogger.getAllAppenders().hasMoreElements()) {
rootLogger.addAppender(new ConsoleAppender(new PatternLayout("[%p] %d{HH:mm:ss.SSS} %m%n"),"System.out"));
}
initialized = true;
}
public static void fatal(String msg) {
log(msg, null, Level.FATAL, new Exception().getStackTrace()[0]);
}
public static void error(String msg) {
log(msg, null, Level.ERROR, new Exception().getStackTrace()[0]);
}
public static void warn(String msg) {
log(msg, null, Level.WARN, new Exception().getStackTrace()[0]);
}
public static void info(String msg) {
log(msg, null, Level.INFO, new Exception().getStackTrace()[0]);
}
public static void debug(String msg) {
log(msg, null, Level.DEBUG, new Exception().getStackTrace()[0]);
}
public static void trace(String msg) {
log(msg, null, Level.TRACE, new Exception().getStackTrace()[0]);
}
public static void fatal(Throwable t) {
log(null, t, Level.FATAL, new Exception().getStackTrace()[0]);
}
public static void error(Throwable t) {
log(null, t, Level.ERROR, new Exception().getStackTrace()[0]);
}
public static void warn(Throwable t) {
log(null, t, Level.WARN, new Exception().getStackTrace()[0]);
}
public static void info(Throwable t) {
log(null, t, Level.INFO, new Exception().getStackTrace()[0]);
}
public static void debug(Throwable t) {
log(null, t, Level.DEBUG, new Exception().getStackTrace()[0]);
}
public static void trace(Throwable t) {
log(null, t, Level.TRACE, new Exception().getStackTrace()[0]);
}
public static void fatal(String msg, Throwable t) {
log(msg, t, Level.FATAL, new Exception().getStackTrace()[0]);
}
public static void error(String msg, Throwable t) {
log(msg, t, Level.ERROR, new Exception().getStackTrace()[0]);
}
public static void warn(String msg, Throwable t) {
log(msg, t, Level.WARN, new Exception().getStackTrace()[0]);
}
public static void info(String msg, Throwable t) {
log(msg, t, Level.INFO, new Exception().getStackTrace()[0]);
}
public static void debug(String msg, Throwable t) {
log(msg, t, Level.DEBUG, new Exception().getStackTrace()[0]);
}
public static void trace(String msg, Throwable t) {
log(msg, t, Level.TRACE, new Exception().getStackTrace()[0]);
}
/**
* log msg, throwable with log level from one stack before the given
* StackTraceElement code location
*
* @param msg message to log
* @param t throwable to log
* @param logLevel log level of the message
* @param calledFrom Exception stacktrace
*/
public static void log(String msg, Throwable t, Level logLevel,
StackTraceElement calledFrom) {
// get caller
StackTraceElement element = getCaller(calledFrom);
org.apache.log4j.Logger logger = null;
if (element != null) {
logger = org.apache.log4j.Logger.getLogger(element.getClassName());
}
// run our init() after first call to log4j to give it the possibility to read log4j.xml first
init();
// what's the current log level?
org.apache.log4j.Level loggerLevel = logger.getEffectiveLevel();
// only append code:line during debugging
String msgAppend = "";
if (element != null && !loggerLevel.isGreaterOrEqual(org.apache.log4j.Level.INFO))
msgAppend = " at " + element + "";
// empty log messages provoke this error line for devs to fix the cause
if (msg!=null && msg.isEmpty()) {
error("Logger: string message was empty but not null at "+element);
}
// throw error on empty log entries
if (t == null && msg == null)
throw new IllegalArgumentException(
"Logger: either message or throwable must be given. "+element);
// use throwable's data if null message was given
if (msg == null) {
msg = t.getMessage();
if (msg == null || msg.isEmpty() )
msg = t.getClass().getName();
}
logger.log(toLevel(logLevel), msg + msgAppend, t);
}
private static StackTraceElement getCaller(StackTraceElement calledFrom) {
if (calledFrom == null)
throw new IllegalArgumentException();
StackTraceElement[] stack = new Exception().getStackTrace();
boolean seenCaller = false;
// run up the stack until we are one below the calling code (calledFrom)
for (StackTraceElement element : stack) {
// we saw calledFrom in the previous run, hence this must be our origin
if (seenCaller)
return element;
seenCaller = element.equals(calledFrom);
}
return null;
}
/**
* get current file appenders, mainly for display purposes eg. in
* {@link GenerateLogPlugIn}
*
* @return files list
*/
public static List<File> getLogFiles() {
List files = new ArrayList<File>();
Enumeration<org.apache.log4j.Logger> loggers = org.apache.log4j.LogManager
.getCurrentLoggers();
org.apache.log4j.Logger rootlogger = org.apache.log4j.LogManager
.getRootLogger();
// combine all loggers to one list to iterate over
List<org.apache.log4j.Logger> list = Collections.list(loggers);
list.add(rootlogger);
for (org.apache.log4j.Logger logger : list) {
// System.out.println(logger.getName());
Enumeration<Appender> apps = logger.getAllAppenders();
while (apps.hasMoreElements()) {
Appender app = (Appender) apps.nextElement();
// System.out.println(app.getName());
if (app instanceof FileAppender) {
// System.out.println(app);
files.add(new File(((FileAppender) app).getFile()));
}
}
}
return files;
}
/**
* setting current log level for the root logger (internally use
* org.apache.log4j.Level.toLevel)
*
* @param levelString a string representing the LogLevel
*/
public static void setLevel(String levelString) {
if (! Arrays.stream(Level.class.getFields()).anyMatch( f -> f.getName().equalsIgnoreCase(levelString) ))
throw new IllegalArgumentException("unknown log verbosity level.");
org.apache.log4j.Level level = org.apache.log4j.Level.toLevel(levelString);
if (level.equals(Level.DEBUG) && !levelString.equalsIgnoreCase("debug"))
throw new IllegalArgumentException("unknown log verbosity level.");
org.apache.log4j.Logger.getRootLogger().setLevel(level);
info(new MessageFormat("Setting log level to {0}").format(new Object[]{level}));
}
private static org.apache.log4j.Level toLevel( Level level ) {
return org.apache.log4j.Level.toLevel(level.VALUE);
}
private static Level toLevel( org.apache.log4j.Level level ) {
try {
return (Level) Level.class.getDeclaredField(level.toString().toUpperCase()).get(null);
}catch (Exception e) {
throw new IllegalArgumentException("unknown log verbosity level.", e);
}
}
/**
* @return the current log level for the calling class
*/
public static Level getLevel() {
// get caller
StackTraceElement element = getCaller(new Exception().getStackTrace()[0]);
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(element
.getClassName());
return toLevel(logger.getEffectiveLevel());
}
private static boolean isLoggerLevelEnabled(org.apache.log4j.Level level) {
// get caller, this time 2 stack entries away
StackTraceElement element = getCaller(new Exception().getStackTrace()[1]);
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(element
.getClassName());
return level.isGreaterOrEqual(logger.getEffectiveLevel());
}
public static boolean isFatalEnabled() {
return isLoggerLevelEnabled(org.apache.log4j.Level.FATAL);
}
public static boolean isErrorEnabled() {
return isLoggerLevelEnabled(org.apache.log4j.Level.ERROR);
}
public static boolean isWarnEnabled() {
return isLoggerLevelEnabled(org.apache.log4j.Level.WARN);
}
public static boolean isInfoEnabled() {
return isLoggerLevelEnabled(org.apache.log4j.Level.INFO);
}
public static boolean isDebugEnabled() {
return isLoggerLevelEnabled(org.apache.log4j.Level.DEBUG);
}
public static boolean isTraceEnabled() {
return isLoggerLevelEnabled(org.apache.log4j.Level.TRACE);
}
}