1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j;
18
19 import org.apache.log4j.helpers.NullEnumeration;
20 import org.slf4j.LoggerFactory;
21 import org.slf4j.Marker;
22 import org.slf4j.MarkerFactory;
23 import org.slf4j.spi.LocationAwareLogger;
24
25 import java.util.Enumeration;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class Category {
44
45 private static final String CATEGORY_FQCN = Category.class.getName();
46
47 private String name;
48
49 protected org.slf4j.Logger slf4jLogger;
50 private org.slf4j.spi.LocationAwareLogger locationAwareLogger;
51
52 private static Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL");
53
54 Category(String name) {
55 this.name = name;
56 slf4jLogger = LoggerFactory.getLogger(name);
57 if (slf4jLogger instanceof LocationAwareLogger) {
58 locationAwareLogger = (LocationAwareLogger) slf4jLogger;
59 }
60 }
61
62 public static Category getInstance(Class clazz) {
63 return Log4jLoggerFactory.getLogger(clazz.getName());
64 }
65
66 public static Category getInstance(String name) {
67 return Log4jLoggerFactory.getLogger(name);
68 }
69
70
71
72
73
74
75
76 public String getName() {
77 return name;
78 }
79
80 public Enumeration getAllAppenders() {
81 return NullEnumeration.getInstance();
82 }
83
84
85
86
87
88
89
90
91
92 public Level getEffectiveLevel() {
93 if (slf4jLogger.isTraceEnabled()) {
94 return Level.TRACE;
95 }
96 if (slf4jLogger.isDebugEnabled()) {
97 return Level.DEBUG;
98 }
99 if (slf4jLogger.isInfoEnabled()) {
100 return Level.INFO;
101 }
102 if (slf4jLogger.isWarnEnabled()) {
103 return Level.WARN;
104 }
105 return Level.ERROR;
106 }
107
108
109
110
111
112
113
114 final public Level getLevel() {
115 return null;
116 }
117
118
119
120
121 final public Level getPriority() {
122 return null;
123 }
124
125
126
127
128 public boolean isDebugEnabled() {
129 return slf4jLogger.isDebugEnabled();
130 }
131
132
133
134
135 public boolean isInfoEnabled() {
136 return slf4jLogger.isInfoEnabled();
137 }
138
139
140
141
142 public boolean isWarnEnabled() {
143 return slf4jLogger.isWarnEnabled();
144 }
145
146
147
148
149 public boolean isErrorEnabled() {
150 return slf4jLogger.isErrorEnabled();
151 }
152
153
154
155
156
157
158
159
160
161
162
163 public boolean isEnabledFor(Priority p) {
164 switch (p.level) {
165 case Level.TRACE_INT:
166 return slf4jLogger.isTraceEnabled();
167 case Level.DEBUG_INT:
168 return slf4jLogger.isDebugEnabled();
169 case Level.INFO_INT:
170 return slf4jLogger.isInfoEnabled();
171 case Level.WARN_INT:
172 return slf4jLogger.isWarnEnabled();
173 case Level.ERROR_INT:
174 return slf4jLogger.isErrorEnabled();
175 case Priority.FATAL_INT:
176 return slf4jLogger.isErrorEnabled();
177 }
178 return false;
179 }
180
181 void differentiatedLog(Marker marker, String fqcn, int level, Object message,
182 Throwable t) {
183
184 String m = convertToString(message);
185 if (locationAwareLogger != null) {
186 locationAwareLogger.log(marker, fqcn, level, m, null, t);
187 } else {
188 switch (level) {
189 case LocationAwareLogger.TRACE_INT:
190 slf4jLogger.trace(marker, m);
191 break;
192 case LocationAwareLogger.DEBUG_INT:
193 slf4jLogger.debug(marker, m);
194 break;
195 case LocationAwareLogger.INFO_INT:
196 slf4jLogger.info(marker, m);
197 break;
198 case LocationAwareLogger.WARN_INT:
199 slf4jLogger.warn(marker, m);
200 break;
201 case LocationAwareLogger.ERROR_INT:
202 slf4jLogger.error(marker, m);
203 break;
204 }
205 }
206 }
207
208
209
210
211 public void debug(Object message) {
212 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT,
213 message, null);
214 }
215
216
217
218
219
220 public void debug(Object message, Throwable t) {
221 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.DEBUG_INT,
222 message, t);
223 }
224
225
226
227
228 public void info(Object message) {
229 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT,
230 message, null);
231 }
232
233
234
235
236
237 public void info(Object message, Throwable t) {
238 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.INFO_INT,
239 message, t);
240 }
241
242
243
244
245 public void warn(Object message) {
246 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT,
247 message, null);
248 }
249
250
251
252
253
254 public void warn(Object message, Throwable t) {
255 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.WARN_INT,
256 message, t);
257 }
258
259
260
261
262 public void error(Object message) {
263 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT,
264 message, null);
265 }
266
267
268
269
270
271 public void error(Object message, Throwable t) {
272 differentiatedLog(null, CATEGORY_FQCN, LocationAwareLogger.ERROR_INT,
273 message, t);
274 }
275
276
277
278
279 public void fatal(Object message) {
280 differentiatedLog(FATAL_MARKER, CATEGORY_FQCN,
281 LocationAwareLogger.ERROR_INT, message, null);
282 }
283
284
285
286
287
288 public void fatal(Object message, Throwable t) {
289 differentiatedLog(FATAL_MARKER, CATEGORY_FQCN,
290 LocationAwareLogger.ERROR_INT, message, t);
291 }
292
293
294 public void log(String FQCN, Priority p, Object msg, Throwable t) {
295 int levelInt = priorityToLevelInt(p);
296 differentiatedLog(null, FQCN, levelInt, msg, t);
297 }
298
299 public void log(Priority p, Object message, Throwable t) {
300 int levelInt = priorityToLevelInt(p);
301 differentiatedLog(null, CATEGORY_FQCN, levelInt, message, t);
302 }
303
304 public void log(Priority p, Object message) {
305 int levelInt = priorityToLevelInt(p);
306 differentiatedLog(null, CATEGORY_FQCN, levelInt, message, null);
307 }
308
309 private int priorityToLevelInt(Priority p) {
310 switch (p.level) {
311 case Level.TRACE_INT:
312 case Level.X_TRACE_INT:
313 return LocationAwareLogger.TRACE_INT;
314 case Priority.DEBUG_INT:
315 return LocationAwareLogger.DEBUG_INT;
316 case Priority.INFO_INT:
317 return LocationAwareLogger.INFO_INT;
318 case Priority.WARN_INT:
319 return LocationAwareLogger.WARN_INT;
320 case Priority.ERROR_INT:
321 return LocationAwareLogger.ERROR_INT;
322 case Priority.FATAL_INT:
323 return LocationAwareLogger.ERROR_INT;
324 default:
325 throw new IllegalStateException("Unknown Priority " + p);
326 }
327 }
328
329 protected final String convertToString(Object message) {
330 if (message == null) {
331 return (String) message;
332 } else {
333 return message.toString();
334 }
335 }
336
337 }