1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.log; |
17 | |
|
18 | |
import java.net.URL; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
import org.apache.log4j.PropertyConfigurator; |
25 | |
import org.apache.log4j.xml.DOMConfigurator; |
26 | |
import org.seasar.framework.message.MessageFormatter; |
27 | |
import org.seasar.uruma.core.UrumaConstants; |
28 | |
import org.seasar.uruma.util.AssertionUtil; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public final class UrumaLogger { |
36 | |
|
37 | 4 | private static final Map<Class<?>, UrumaLogger> loggers = new HashMap<Class<?>, UrumaLogger>(); |
38 | |
|
39 | |
private static boolean initialized; |
40 | |
|
41 | |
private final Log log; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public static synchronized UrumaLogger getLogger(final Class<?> clazz) { |
51 | 2184 | if (!initialized) { |
52 | 4 | initialize(); |
53 | |
} |
54 | 2184 | UrumaLogger logger = loggers.get(clazz); |
55 | 2184 | if (logger == null) { |
56 | 324 | logger = new UrumaLogger(clazz); |
57 | 324 | loggers.put(clazz, logger); |
58 | |
} |
59 | 2184 | return logger; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static synchronized void initialize() { |
66 | 4 | initialized = true; |
67 | 4 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public synchronized static void dispose() { |
73 | 0 | LogFactory.releaseAll(); |
74 | 0 | loggers.clear(); |
75 | 0 | initialized = false; |
76 | 0 | } |
77 | |
|
78 | 324 | private UrumaLogger(final Class<?> clazz) { |
79 | 324 | log = LogFactory.getLog(clazz); |
80 | 324 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void addXmlConfig(final URL config) { |
90 | 0 | AssertionUtil.assertNotNull("config", config); |
91 | 0 | DOMConfigurator.configure(config); |
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void addPropertyConfig(final URL config) { |
102 | 0 | AssertionUtil.assertNotNull("config", config); |
103 | 0 | PropertyConfigurator.configure(config); |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public final boolean isTraceEnabled() { |
112 | 1624 | return log.isTraceEnabled(); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public final void trace(final Object message, final Throwable throwable) { |
124 | 0 | if (isTraceEnabled()) { |
125 | 0 | log.trace(message, throwable); |
126 | |
} |
127 | 0 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public final void trace(final Object message) { |
136 | 0 | if (isTraceEnabled()) { |
137 | 0 | log.trace(message); |
138 | |
} |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public final boolean isDebugEnabled() { |
147 | 7196 | return log.isDebugEnabled(); |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public final void debug(final Object message, final Throwable throwable) { |
159 | 0 | if (isDebugEnabled()) { |
160 | 0 | log.debug(message, throwable); |
161 | |
} |
162 | 0 | } |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public final void debug(final Object message) { |
171 | 0 | if (isDebugEnabled()) { |
172 | 0 | log.debug(message); |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public final boolean isInfoEnabled() { |
182 | 124 | return log.isInfoEnabled(); |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public final void info(final Object message, final Throwable throwable) { |
194 | 0 | if (isInfoEnabled()) { |
195 | 0 | log.info(message, throwable); |
196 | |
} |
197 | 0 | } |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public final void info(final Object message) { |
206 | 0 | if (isInfoEnabled()) { |
207 | 0 | log.info(message); |
208 | |
} |
209 | 0 | } |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public final void warn(final Object message, final Throwable throwable) { |
220 | 0 | log.warn(message, throwable); |
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public final void warn(final Object message) { |
230 | 0 | log.warn(message); |
231 | 0 | } |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public final void error(final Object message, final Throwable throwable) { |
242 | 0 | log.error(message, throwable); |
243 | 0 | } |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public final void error(final Object message) { |
252 | 0 | log.error(message); |
253 | 0 | } |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
public final void fatal(final Object message, final Throwable throwable) { |
264 | 0 | log.fatal(message, throwable); |
265 | 0 | } |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public final void fatal(final Object message) { |
274 | 0 | log.fatal(message); |
275 | 0 | } |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public final void log(final Throwable throwable) { |
284 | 0 | error(throwable.getMessage(), throwable); |
285 | 0 | } |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public final void log(final String messageCode, final Object... args) { |
296 | 3464 | log(messageCode, null, args); |
297 | 3464 | } |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
public final void log(final String messageCode, final Throwable throwable, |
310 | |
final Object... args) { |
311 | 3464 | char messageType = messageCode.charAt(0); |
312 | 3464 | if (isEnabledFor(messageType)) { |
313 | 124 | String message = MessageFormatter.getMessage(messageCode, args); |
314 | |
|
315 | 124 | switch (messageType) { |
316 | |
case 'T': |
317 | 0 | log.trace(message, throwable); |
318 | 0 | break; |
319 | |
case 'D': |
320 | 0 | log.debug(message, throwable); |
321 | 0 | break; |
322 | |
case 'I': |
323 | 0 | log.info(message, throwable); |
324 | 0 | break; |
325 | |
case 'W': |
326 | 116 | log.warn(message, throwable); |
327 | 116 | break; |
328 | |
case 'E': |
329 | 8 | log.error(message, throwable); |
330 | 8 | break; |
331 | |
case 'F': |
332 | 0 | log.fatal(message, throwable); |
333 | 0 | break; |
334 | |
default: |
335 | 0 | throw new IllegalArgumentException(String.valueOf(messageType)); |
336 | |
} |
337 | |
} |
338 | 3464 | } |
339 | |
|
340 | |
private boolean isEnabledFor(final char messageType) { |
341 | 3464 | switch (messageType) { |
342 | |
case 'T': |
343 | 0 | return log.isTraceEnabled(); |
344 | |
case 'D': |
345 | 2780 | return log.isDebugEnabled(); |
346 | |
case 'I': |
347 | 560 | return log.isInfoEnabled(); |
348 | |
case 'W': |
349 | 116 | return log.isWarnEnabled(); |
350 | |
case 'E': |
351 | 8 | return log.isErrorEnabled(); |
352 | |
case 'F': |
353 | 0 | return log.isFatalEnabled(); |
354 | |
default: |
355 | 0 | throw new IllegalArgumentException(String.valueOf(messageType)); |
356 | |
} |
357 | |
} |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
public static final String getObjectDescription(final Object obj) { |
367 | 428 | if (obj != null) { |
368 | 428 | return obj.getClass().getName() + UrumaConstants.AT_MARK |
369 | |
+ Integer.toHexString(obj.hashCode()); |
370 | |
} else { |
371 | 0 | return "NULL"; |
372 | |
} |
373 | |
} |
374 | |
} |