1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.impl;
19
20 import java.io.PrintWriter;
21 import java.io.Serializable;
22 import java.io.StringWriter;
23 import java.util.StringTokenizer;
24 import java.util.logging.Level;
25 import java.util.logging.LogRecord;
26 import java.util.logging.Logger;
27
28 import org.apache.commons.logging.Log;
29
30
31
32
33
34
35
36
37
38 @Deprecated
39 public class Jdk13LumberjackLogger implements Log, Serializable {
40
41
42 private static final long serialVersionUID = -8649807923527610591L;
43
44
45
46
47
48
49
50
51
52 @Deprecated
53 protected static final Level dummyLevel = Level.FINE;
54
55
56
57
58 protected transient Logger logger;
59
60
61
62
63 protected String name;
64
65
66 private String sourceClassName = "unknown";
67
68
69 private String sourceMethodName = "unknown";
70
71
72 private boolean classAndMethodFound;
73
74
75
76
77
78
79 public Jdk13LumberjackLogger(final String name) {
80 this.name = name;
81 logger = getLogger();
82 }
83
84
85
86
87
88
89
90 @Override
91 public void debug(final Object message) {
92 log(Level.FINE, String.valueOf(message), null);
93 }
94
95
96
97
98
99
100
101
102 @Override
103 public void debug(final Object message, final Throwable exception) {
104 log(Level.FINE, String.valueOf(message), exception);
105 }
106
107
108
109
110
111
112
113 @Override
114 public void error(final Object message) {
115 log(Level.SEVERE, String.valueOf(message), null);
116 }
117
118
119
120
121
122
123
124
125 @Override
126 public void error(final Object message, final Throwable exception) {
127 log(Level.SEVERE, String.valueOf(message), exception);
128 }
129
130
131
132
133
134
135
136 @Override
137 public void fatal(final Object message) {
138 log(Level.SEVERE, String.valueOf(message), null);
139 }
140
141
142
143
144
145
146
147
148 @Override
149 public void fatal(final Object message, final Throwable exception) {
150 log(Level.SEVERE, String.valueOf(message), exception);
151 }
152
153
154
155
156
157 private void getClassAndMethod() {
158 try {
159 final Throwable throwable = new Throwable();
160 throwable.fillInStackTrace();
161 final StringWriter stringWriter = new StringWriter();
162 final PrintWriter printWriter = new PrintWriter(stringWriter);
163 throwable.printStackTrace(printWriter);
164 final String traceString = stringWriter.toString();
165 final StringTokenizer tokenizer = new StringTokenizer(traceString, "\n");
166 tokenizer.nextToken();
167 String line = tokenizer.nextToken();
168 while (!line.contains(this.getClass().getName())) {
169 line = tokenizer.nextToken();
170 }
171 while (line.contains(this.getClass().getName())) {
172 line = tokenizer.nextToken();
173 }
174 final int start = line.indexOf("at ") + 3;
175 final int end = line.indexOf('(');
176 final String temp = line.substring(start, end);
177 final int lastPeriod = temp.lastIndexOf('.');
178 sourceClassName = temp.substring(0, lastPeriod);
179 sourceMethodName = temp.substring(lastPeriod + 1);
180 } catch (final Exception ex) {
181
182 }
183 classAndMethodFound = true;
184 }
185
186
187
188
189
190
191 public Logger getLogger() {
192 if (logger == null) {
193 logger = Logger.getLogger(name);
194 }
195 return logger;
196 }
197
198
199
200
201
202
203
204 @Override
205 public void info(final Object message) {
206 log(Level.INFO, String.valueOf(message), null);
207 }
208
209
210
211
212
213
214
215
216 @Override
217 public void info(final Object message, final Throwable exception) {
218 log(Level.INFO, String.valueOf(message), exception);
219 }
220
221
222
223
224 @Override
225 public boolean isDebugEnabled() {
226 return getLogger().isLoggable(Level.FINE);
227 }
228
229
230
231
232 @Override
233 public boolean isErrorEnabled() {
234 return getLogger().isLoggable(Level.SEVERE);
235 }
236
237
238
239
240 @Override
241 public boolean isFatalEnabled() {
242 return getLogger().isLoggable(Level.SEVERE);
243 }
244
245
246
247
248 @Override
249 public boolean isInfoEnabled() {
250 return getLogger().isLoggable(Level.INFO);
251 }
252
253
254
255
256 @Override
257 public boolean isTraceEnabled() {
258 return getLogger().isLoggable(Level.FINEST);
259 }
260
261
262
263
264 @Override
265 public boolean isWarnEnabled() {
266 return getLogger().isLoggable(Level.WARNING);
267 }
268
269 private void log( final Level level, final String msg, final Throwable ex ) {
270 if ( getLogger().isLoggable(level) ) {
271 final LogRecord record = new LogRecord(level, msg);
272 if ( !classAndMethodFound ) {
273 getClassAndMethod();
274 }
275 record.setSourceClassName(sourceClassName);
276 record.setSourceMethodName(sourceMethodName);
277 if ( ex != null ) {
278 record.setThrown(ex);
279 }
280 getLogger().log(record);
281 }
282 }
283
284
285
286
287
288
289
290 @Override
291 public void trace(final Object message) {
292 log(Level.FINEST, String.valueOf(message), null);
293 }
294
295
296
297
298
299
300
301
302 @Override
303 public void trace(final Object message, final Throwable exception) {
304 log(Level.FINEST, String.valueOf(message), exception);
305 }
306
307
308
309
310
311
312
313 @Override
314 public void warn(final Object message) {
315 log(Level.WARNING, String.valueOf(message), null);
316 }
317
318
319
320
321
322
323
324
325 @Override
326 public void warn(final Object message, final Throwable exception) {
327 log(Level.WARNING, String.valueOf(message), exception);
328 }
329 }