-
-
Notifications
You must be signed in to change notification settings - Fork 975
/
Category.java
396 lines (349 loc) · 11.7 KB
/
Category.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.log4j;
import org.apache.log4j.helpers.NullEnumeration;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.slf4j.spi.LocationAwareLogger;
import java.util.Enumeration;
/**
* <p>
* This class is a minimal implementation of the original
* <code>org.apache.log4j.Category</code> class (as found in log4j 1.2) by
* delegation of all calls to a {@link org.slf4j.Logger} instance.
*
*
* <p>
* Log4j's <code>trace</code>, <code>debug()</code>, <code>info()</code>,
* <code>warn()</code>, <code>error()</code> printing methods are directly
* mapped to their SLF4J equivalents. Log4j's <code>fatal()</code> printing
* method is mapped to SLF4J's <code>error()</code> method with a FATAL marker.
*
* @author Sébastien Pennec
* @author Ceki Gülcü
*/
@SuppressWarnings("rawtypes")
public class Category {
private static final String CATEGORY_FQCN = Category.class.getName();
private final String name;
protected org.slf4j.Logger slf4jLogger;
private org.slf4j.spi.LocationAwareLogger locationAwareLogger;
private static final Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL");
Category(String name) {
this.name = name;
slf4jLogger = LoggerFactory.getLogger(name);
if (slf4jLogger instanceof LocationAwareLogger) {
locationAwareLogger = (LocationAwareLogger) slf4jLogger;
}
}
public static Category getInstance(Class clazz) {
return Log4jLoggerFactory.getLogger(clazz.getName());
}
public static Category getInstance(String name) {
return Log4jLoggerFactory.getLogger(name);
}
public final Category getParent() {
return null;
}
/**
* Returns the obvious.
*
* @return
*/
public String getName() {
return name;
}
public Appender getAppender(String name) {
return null;
}
public Enumeration getAllAppenders() {
return NullEnumeration.getInstance();
}
/**
* Return the level in effect for this category/logger.
*
* <p>
* The result is computed by simulation.
*
* @return
*/
public Level getEffectiveLevel() {
if (slf4jLogger.isTraceEnabled()) {
return Level.TRACE;
}
if (slf4jLogger.isDebugEnabled()) {
return Level.DEBUG;
}
if (slf4jLogger.isInfoEnabled()) {
return Level.INFO;
}
if (slf4jLogger.isWarnEnabled()) {
return Level.WARN;
}
return Level.ERROR;
}
/**
* Returns the assigned {@link Level}, if any, for this Category. This
* implementation always returns null.
*
* @return Level - the assigned Level, can be <code>null</code>.
*/
final public Level getLevel() {
return null;
}
/**
* @deprecated Please use {@link #getLevel} instead.
* @return a Level
*/
final public Level getPriority() {
return null;
}
/**
* Delegates to {@link org.slf4j.Logger#isDebugEnabled} method in SLF4J
*
* @return true if this logger is enabled for the level DEBUG
*
*/
public boolean isDebugEnabled() {
return slf4jLogger.isDebugEnabled();
}
/**
* Delegates to {@link org.slf4j.Logger#isInfoEnabled} method in SLF4J
*
* @return true if this logger is enabled for the level INFO
*/
public boolean isInfoEnabled() {
return slf4jLogger.isInfoEnabled();
}
/**
* Delegates to {@link org.slf4j.Logger#isWarnEnabled} method in SLF4J
*
* @return true if this logger is enabled for the level WARN
*/
public boolean isWarnEnabled() {
return slf4jLogger.isWarnEnabled();
}
/**
* Delegates to {@link org.slf4j.Logger#isErrorEnabled} method in SLF4J
*
* @return true if this logger is enabled for the level ERROR
*/
public boolean isErrorEnabled() {
return slf4jLogger.isErrorEnabled();
}
/**
* Determines whether the priority passed as parameter is enabled in the
* underlying SLF4J logger. Each log4j priority is mapped directly to its SLF4J
* equivalent, except for FATAL which is mapped as ERROR.
*
* @param p the priority to check against
* @return true if this logger is enabled for the given level, false otherwise.
*/
public boolean isEnabledFor(Priority p) {
switch (p.level) {
case Level.TRACE_INT:
return slf4jLogger.isTraceEnabled();
case Level.DEBUG_INT:
return slf4jLogger.isDebugEnabled();
case Level.INFO_INT:
return slf4jLogger.isInfoEnabled();
case Level.WARN_INT:
return slf4jLogger.isWarnEnabled();
case Level.ERROR_INT:
return slf4jLogger.isErrorEnabled();
case Priority.FATAL_INT:
return slf4jLogger.isErrorEnabled();
}
return false;
}
void differentiatedLog(Marker marker, String fqcn, int level, Object message, Throwable t) {
String m = convertToString(message);
if (locationAwareLogger != null) {
locationAwareLogger.log(marker, fqcn, level, m, null, t);
} else {
switch (level) {
case LocationAwareLogger.TRACE_INT:
slf4jLogger.trace(marker, m, (Throwable) t);
break;
case LocationAwareLogger.DEBUG_INT:
slf4jLogger.debug(marker, m, (Throwable) t);
break;
case LocationAwareLogger.INFO_INT:
slf4jLogger.info(marker, m, (Throwable) t);
break;
case LocationAwareLogger.WARN_INT:
slf4jLogger.warn(marker, m, (Throwable) t);
break;
case LocationAwareLogger.ERROR_INT:
slf4jLogger.error(marker, m, (Throwable) t);
break;
}
}
}
/**
* Delegates to {@link org.slf4j.Logger#debug(String)} method of SLF4J.
*
* @param message a message to log
*/
public void debug(Object message) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT, message, null);
}
/**
* Delegates to {@link org.slf4j.Logger#debug(String,Throwable)} method in
* SLF4J.
*
* @param message a message to log
* @param t a throwable to log
*/
public void debug(Object message, Throwable t) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT, message, t);
}
/**
* Delegates to {@link org.slf4j.Logger#info(String)} method in SLF4J.
*
* @param message a message to log
*/
public void info(Object message) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT, message, null);
}
/**
* Delegates to {@link org.slf4j.Logger#info(String,Throwable)} method in SLF4J.
*
* @param message a message to log
* @param t a throwable to log
*/
public void info(Object message, Throwable t) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT, message, t);
}
/**
* Delegates to {@link org.slf4j.Logger#warn(String)} method in SLF4J.
*
* @param message a message to log
*
*/
public void warn(Object message) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT, message, null);
}
/**
* Delegates to {@link org.slf4j.Logger#warn(String,Throwable)} method in SLF4J.
*
* @param message a message to log
* @param t a throwable to log
*
*/
public void warn(Object message, Throwable t) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT, message, t);
}
/**
* Delegates to {@link org.slf4j.Logger#error(String)} method in SLF4J.
*
* @param message a message to log
*
*/
public void error(Object message) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, null);
}
/**
* Delegates to {@link org.slf4j.Logger#error(String,Throwable)} method in
* SLF4J.
*
* @param message a message to log
* @param t a throwable to log
*
*/
public void error(Object message, Throwable t) {
differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, t);
}
/**
* Delegates to {@link org.slf4j.Logger#error(String)} method in SLF4J.
*
* @param message a message to log
*
*/
public void fatal(Object message) {
differentiatedLog(FATAL_MARKER, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, null);
}
/**
* Delegates to {@link org.slf4j.Logger#error(String,Throwable)} method in
* SLF4J. In addition, the call is marked with a marker named "FATAL".
*
* @param message a message to log
* @param t a throwable to log
*
*/
public void fatal(Object message, Throwable t) {
differentiatedLog(FATAL_MARKER, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT, message, t);
}
protected void forcedLog(String FQCN, Priority p, Object msg, Throwable t) {
log(FQCN, p, msg, t);
}
// See also http://jira.qos.ch/browse/SLF4J-159
public void log(String FQCN, Priority p, Object msg, Throwable t) {
int levelInt = priorityToLevelInt(p);
differentiatedLog(null, FQCN, levelInt, msg, t);
}
public void log(Priority p, Object message, Throwable t) {
int levelInt = priorityToLevelInt(p);
differentiatedLog(null, CATEGORY_FQCN, levelInt, message, t);
}
public void log(Priority p, Object message) {
int levelInt = priorityToLevelInt(p);
differentiatedLog(null, CATEGORY_FQCN, levelInt, message, null);
}
private int priorityToLevelInt(Priority p) {
switch (p.level) {
case Level.TRACE_INT:
case Level.X_TRACE_INT:
return LocationAwareLogger.TRACE_INT;
case Priority.DEBUG_INT:
return LocationAwareLogger.DEBUG_INT;
case Priority.INFO_INT:
return LocationAwareLogger.INFO_INT;
case Priority.WARN_INT:
return LocationAwareLogger.WARN_INT;
case Priority.ERROR_INT:
return LocationAwareLogger.ERROR_INT;
case Priority.FATAL_INT:
return LocationAwareLogger.ERROR_INT;
default:
throw new IllegalStateException("Unknown Priority " + p);
}
}
protected final String convertToString(Object message) {
if (message == null) {
return (String) message;
} else {
return message.toString();
}
}
public void setAdditivity(boolean additive) {
// nothing to do
}
public void addAppender(Appender newAppender) {
// nothing to do
}
public void setLevel(Level level) {
// nothing to do
}
public boolean getAdditivity() {
return false;
}
public void assertLog(boolean assertion, String msg) {
if (!assertion)
error(msg);
}
}