1
2
3
4
5
6
7 package nl.tudelft.simulation.logger;
8
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.logging.ConsoleHandler;
12 import java.util.logging.Handler;
13 import java.util.logging.Level;
14 import java.util.logging.LogRecord;
15
16 import nl.tudelft.simulation.event.util.EventProducingMap;
17 import nl.tudelft.simulation.language.reflection.ClassUtil;
18
19 /***
20 * Provides a static class to Sun's logging framework.
21 * <p>
22 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
23 * University of Technology </a>, the Netherlands. <br>
24 * See for project information <a href="http://www.simulation.tudelft.nl">
25 * www.simulation.tudelft.nl </a> <br>
26 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
27 * General Public License (LGPL) </a>, no warranty.
28 *
29 * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
30 * @version $Revision: 1.5 $ $Date: 2005/07/04 12:22:38 $
31 * @since 1.5
32 */
33 public final class Logger
34 {
35 /*** loggers are the currently available loggers */
36 public static final EventProducingMap<String, java.util.logging.Logger> LOGGERS = new EventProducingMap<String, java.util.logging.Logger>(
37 Collections
38 .synchronizedMap(new HashMap<String, java.util.logging.Logger>()));
39
40 /*** the defaultHandler to use */
41 private static Class defaultHandler = ConsoleHandler.class;
42
43 /*** the logLevel */
44 private static Level logLevel = Level.ALL;
45
46 /***
47 * constructs a new Logger
48 */
49 private Logger()
50 {
51
52 }
53
54 /***
55 * resolves the caller
56 *
57 * @param caller the caller
58 * @return the class of the caller. caller if caller instanceof Caller
59 */
60 private static String resolveCaller(final Object caller)
61 {
62 if (caller instanceof Class)
63 {
64 return ((Class) caller).getName();
65 }
66 return caller.getClass().getName();
67 }
68
69 /***
70 * resolves the logger for a caller
71 *
72 * @param caller the object invoking the loggers
73 * @return Logger the logger
74 */
75 public static synchronized java.util.logging.Logger resolveLogger(
76 final Object caller)
77 {
78 if (caller == null)
79 {
80 return null;
81 }
82 Class callerClass = caller.getClass();
83 if (caller instanceof Class)
84 {
85 callerClass = (Class) caller;
86 }
87 String loggerName = callerClass.getPackage().getName();
88 java.util.logging.Logger logger = LOGGERS.get(loggerName);
89 if (logger != null)
90 {
91 return logger;
92 }
93 return Logger.createLogger(loggerName);
94 }
95
96 /***
97 * creates a new Logger
98 *
99 * @param loggerName the name of the logger
100 * @return Logger the name
101 */
102 private static synchronized java.util.logging.Logger createLogger(
103 final String loggerName)
104 {
105 java.util.logging.Logger logger = java.util.logging.Logger
106 .getLogger(loggerName);
107
108 Handler[] handlers = logger.getHandlers();
109 for (int i = 0; i < handlers.length; i++)
110 {
111 logger.removeHandler(handlers[i]);
112 }
113 Handler handler = null;
114 try
115 {
116 handler = (Handler) ClassUtil.resolveConstructor(defaultHandler,
117 null).newInstance();
118 } catch (Exception exception)
119 {
120 handler = new ConsoleHandler();
121 }
122 handler.setLevel(Logger.logLevel);
123 logger.addHandler(handler);
124 logger.setLevel(Logger.logLevel);
125 logger.setUseParentHandlers(false);
126 LOGGERS.put(loggerName, logger);
127 return logger;
128 }
129
130 /***
131 * configs a message
132 *
133 * @param caller the caller
134 * @param methodName the name of the method
135 * @param msg the message
136 */
137 public static void config(final Object caller, final String methodName,
138 final String msg)
139 {
140 Logger.resolveLogger(caller).logp(Level.CONFIG, resolveCaller(caller),
141 methodName, msg);
142 }
143
144 /***
145 * Logs a method entry.
146 *
147 * @param caller the object calling the logger
148 * @param arg0 the name of the class calling the method
149 * @param arg1 the name of the method entering
150 * @param arg2 parameter to the method being entered
151 */
152 public static void entering(final Object caller, final String arg0,
153 final String arg1, final Object arg2)
154 {
155 Logger.resolveLogger(caller).entering(arg0, arg1, arg2);
156 }
157
158 /***
159 * Logs a method entry.
160 *
161 * @param caller the object calling the logger
162 * @param arg0 the name of the class calling the method
163 * @param arg1 the name of the method entering
164 * @param arg2 parameters to the method being entered
165 */
166 public static void entering(final Object caller, final String arg0,
167 final String arg1, final Object[] arg2)
168 {
169 Logger.resolveLogger(caller).entering(arg0, arg1, arg2);
170 }
171
172 /***
173 * Logs a method entry.
174 *
175 * @param caller the object calling the logger
176 * @param arg0 the name of the class calling the method
177 * @param arg1 the name of the method entering
178 */
179 public static void entering(final Object caller, final String arg0,
180 final String arg1)
181 {
182 Logger.resolveLogger(caller).entering(arg0, arg1);
183 }
184
185 /***
186 * Logs a method exit.
187 *
188 * @param caller the object calling the logger
189 * @param arg0 the name of the class calling the method
190 * @param arg1 the name of the method entering
191 * @param arg2 result of the method
192 */
193 public static void exiting(final Object caller, final String arg0,
194 final String arg1, final Object arg2)
195 {
196 Logger.resolveLogger(caller).exiting(arg0, arg1, arg2);
197 }
198
199 /***
200 * Logs a method exit.
201 *
202 * @param caller the object calling the logger
203 * @param arg0 the name of the class calling the method
204 * @param arg1 the name of the method entering
205 */
206 public static void exiting(final Object caller, final String arg0,
207 final String arg1)
208 {
209 Logger.resolveLogger(caller).exiting(arg0, arg1);
210 }
211
212 /***
213 * Logs a fine message
214 *
215 * @param caller the object calling the logger
216 * @param methodName the name of the method
217 * @param msg the message
218 */
219 public static void fine(final Object caller, final String methodName,
220 final String msg)
221 {
222 if (Logger.logLevel.equals(Level.FINE))
223 {
224 Logger.resolveLogger(caller).logp(Level.FINE,
225 resolveCaller(caller), methodName, msg);
226 }
227 }
228
229 /***
230 * Logs a finer message
231 *
232 * @param caller the object calling the logger
233 * @param methodName the name of the method
234 * @param msg the message
235 */
236 public static void finer(final Object caller, final String methodName,
237 final String msg)
238 {
239 Logger.resolveLogger(caller).logp(Level.FINER, resolveCaller(caller),
240 methodName, msg);
241 }
242
243 /***
244 * Logs a finest message
245 *
246 * @param caller the object calling the logger
247 * @param methodName the name of the method
248 * @param msg the message
249 */
250 public static void finest(final Object caller, final String methodName,
251 final String msg)
252 {
253 Logger.resolveLogger(caller).logp(Level.FINEST, resolveCaller(caller),
254 methodName, msg);
255 }
256
257 /***
258 * returns the names of the currently used loggers
259 *
260 * @return String[] the names
261 */
262 public static synchronized String[] getLoggerNames()
263 {
264 return LOGGERS.keySet().toArray(new String[LOGGERS.keySet().size()]);
265 }
266
267 /***
268 * Logs a info message
269 *
270 * @param caller the object calling the logger
271 * @param methodName the name of the method
272 * @param msg the message
273 */
274 public static void info(final Object caller, final String methodName,
275 final String msg)
276 {
277 Logger.resolveLogger(caller).logp(Level.INFO, resolveCaller(caller),
278 methodName, msg);
279 }
280
281 /***
282 * Logs a message, with associated parameter
283 *
284 * @param caller the object calling the logger
285 * @param arg0 the log level
286 * @param arg1 the message
287 * @param arg2 the parameter
288 */
289 public static void log(final Object caller, final Level arg0,
290 final String arg1, final Object arg2)
291 {
292 Logger.resolveLogger(caller).log(arg0, arg1, arg2);
293 }
294
295 /***
296 * Logs a message, with associated parameters
297 *
298 * @param caller the object calling the logger
299 * @param arg0 the log level
300 * @param arg1 the message
301 * @param arg2 the parameters
302 */
303 public static void log(final Object caller, final Level arg0,
304 final String arg1, final Object[] arg2)
305 {
306 Logger.resolveLogger(caller).log(arg0, arg1, arg2);
307 }
308
309 /***
310 * Logs a message, with associated throwable
311 *
312 * @param caller the object calling the logger
313 * @param arg0 the log level
314 * @param arg1 the message
315 * @param arg2 the throwable
316 */
317 public static void log(final Object caller, final Level arg0,
318 final String arg1, final Throwable arg2)
319 {
320 Logger.resolveLogger(caller).log(arg0, arg1, arg2);
321 }
322
323 /***
324 * Logs a message
325 *
326 * @param caller the object calling the logger
327 * @param arg0 the log level
328 * @param arg1 the message
329 */
330 public static void log(final Object caller, final Level arg0,
331 final String arg1)
332 {
333 Logger.resolveLogger(caller).log(arg0, arg1);
334 }
335
336 /***
337 * Logs a log record
338 *
339 * @param caller the object calling the logger
340 * @param arg0 the log record
341 */
342 public static void log(final Object caller, final LogRecord arg0)
343 {
344 Logger.resolveLogger(caller).log(arg0);
345 }
346
347 /***
348 * Logs a message, with associated parameter and class information
349 *
350 * @param caller the object calling the logger
351 * @param arg0 the log level
352 * @param arg1 the source class
353 * @param arg2 the method information
354 * @param arg3 the message
355 * @param arg4 the parameter
356 */
357 public static void logp(final Object caller, final Level arg0,
358 final String arg1, final String arg2, final String arg3,
359 final Object arg4)
360 {
361 Logger.resolveLogger(caller).logp(arg0, arg1, arg2, arg3, arg4);
362 }
363
364 /***
365 * Logs a message, with associated parameter and class information
366 *
367 * @param caller the object calling the logger
368 * @param arg0 the log level
369 * @param arg1 the source class
370 * @param arg2 the method information
371 * @param arg3 the message
372 * @param arg4 the parameters
373 */
374 public static void logp(final Object caller, final Level arg0,
375 final String arg1, final String arg2, final String arg3,
376 final Object[] arg4)
377 {
378 Logger.resolveLogger(caller).logp(arg0, arg1, arg2, arg3, arg4);
379 }
380
381 /***
382 * Logs a message, with associated throwable and class information
383 *
384 * @param caller the object calling the logger
385 * @param arg0 the log level
386 * @param arg1 the source class
387 * @param arg2 the method information
388 * @param arg3 the message
389 * @param arg4 the parameters
390 */
391 public static void logp(final Object caller, final Level arg0,
392 final String arg1, final String arg2, final String arg3,
393 final Throwable arg4)
394 {
395 Logger.resolveLogger(caller).logp(arg0, arg1, arg2, arg3, arg4);
396 }
397
398 /***
399 * Logs a message, with associated class information
400 *
401 * @param caller the object calling the logger
402 * @param arg0 the log level
403 * @param arg1 the source class
404 * @param arg2 the method information
405 * @param arg3 the message
406 */
407 public static void logp(final Object caller, final Level arg0,
408 final String arg1, final String arg2, final String arg3)
409 {
410 Logger.resolveLogger(caller).logp(arg0, arg1, arg2, arg3);
411 }
412
413 /***
414 * Logs a message, with associated parameter and class information and
415 * resource bundle
416 *
417 * @param caller the object calling the logger
418 * @param arg0 the log level
419 * @param arg1 the source class
420 * @param arg2 the method information
421 * @param arg3 the resource bundle
422 * @param arg4 the message
423 * @param arg5 the parameter
424 */
425 public static void logrb(final Object caller, final Level arg0,
426 final String arg1, final String arg2, final String arg3,
427 final String arg4, final Object arg5)
428 {
429 Logger.resolveLogger(caller).logrb(arg0, arg1, arg2, arg3, arg4, arg5);
430 }
431
432 /***
433 * Logs a message, with associated parameters and class information and
434 * resource bundle
435 *
436 * @param caller the object calling the logger
437 * @param arg0 the log level
438 * @param arg1 the source class
439 * @param arg2 the method information
440 * @param arg3 the resource bundle
441 * @param arg4 the message
442 * @param arg5 the parameters
443 */
444 public static void logrb(final Object caller, final Level arg0,
445 final String arg1, final String arg2, final String arg3,
446 final String arg4, final Object[] arg5)
447 {
448 Logger.resolveLogger(caller).logrb(arg0, arg1, arg2, arg3, arg4, arg5);
449 }
450
451 /***
452 * Logs a message, with associated throwable and class information and
453 * resource bundle
454 *
455 * @param caller the object calling the logger
456 * @param arg0 the log level
457 * @param arg1 the source class
458 * @param arg2 the method information
459 * @param arg3 the resource bundle
460 * @param arg4 the message
461 * @param arg5 the parameters
462 */
463 public static void logrb(final Object caller, final Level arg0,
464 final String arg1, final String arg2, final String arg3,
465 final String arg4, final Throwable arg5)
466 {
467 Logger.resolveLogger(caller).logrb(arg0, arg1, arg2, arg3, arg4, arg5);
468 }
469
470 /***
471 * Logs a message, with associated class information and resource bundle
472 *
473 * @param caller the object calling the logger
474 * @param arg0 the log level
475 * @param arg1 the source class
476 * @param arg2 the method information
477 * @param arg3 the resource bundle
478 * @param arg4 the message
479 */
480 public static void logrb(final Object caller, final Level arg0,
481 final String arg1, final String arg2, final String arg3,
482 final String arg4)
483 {
484 Logger.resolveLogger(caller).logrb(arg0, arg1, arg2, arg3, arg4);
485 }
486
487 /***
488 * Logs a severe message
489 *
490 * @param caller the object calling the logger
491 * @param methodName the name of the method
492 * @param msg the message
493 */
494 public static void severe(final Object caller, final String methodName,
495 final String msg)
496 {
497 Logger.resolveLogger(caller).logp(Level.SEVERE, resolveCaller(caller),
498 methodName, msg);
499 }
500
501 /***
502 * Logs a severe message
503 *
504 * @param caller the object calling the logger
505 * @param methodName the name of the method
506 * @param throwable the throwable
507 */
508 public static void severe(final Object caller, final String methodName,
509 final Throwable throwable)
510 {
511 Logger.resolveLogger(caller).logp(Level.SEVERE, resolveCaller(caller),
512 methodName, throwable.getLocalizedMessage(), throwable);
513 }
514
515 /***
516 * logs a throwable message
517 *
518 * @param caller the object invoking the logger
519 * @param arg0 the class
520 * @param arg1 the method
521 * @param arg2 the throwable
522 */
523 public static void throwing(final Object caller, final String arg0,
524 final String arg1, final Throwable arg2)
525 {
526 Logger.resolveLogger(caller).throwing(arg0, arg1, arg2);
527 }
528
529 /***
530 * Logs a warning message
531 *
532 * @param caller the object calling the logger
533 * @param methodName the name of the method
534 * @param msg the message
535 */
536 public static void warning(final Object caller, final String methodName,
537 final String msg)
538 {
539 Logger.resolveLogger(caller).logp(Level.WARNING, resolveCaller(caller),
540 methodName, msg);
541 }
542
543 /***
544 * Logs a warning message
545 *
546 * @param caller the object calling the logger
547 * @param methodName the name of the method
548 * @param thrown the thrown
549 */
550 public static void warning(final Object caller, final String methodName,
551 final Throwable thrown)
552 {
553 Logger.resolveLogger(caller).logp(Level.WARNING, resolveCaller(caller),
554 methodName, thrown.getLocalizedMessage(), thrown);
555 }
556
557 /***
558 * @return Returns the defaultHandler.
559 */
560 public static Class getDefaultHandler()
561 {
562 return defaultHandler;
563 }
564
565 /***
566 * @param defaultHandler The defaultHandler to set.
567 */
568 public static void setDefaultHandler(final Class defaultHandler)
569 {
570 if (Handler.class.isAssignableFrom(defaultHandler))
571 {
572 Logger.defaultHandler = defaultHandler;
573 } else
574 {
575 throw new IllegalArgumentException(
576 "defaultHandler should extend Handler");
577 }
578 }
579
580 /***
581 * @return Returns the logLevel.
582 */
583 public static Level getLogLevel()
584 {
585 return Logger.logLevel;
586 }
587
588 /***
589 * @param logLevel The logLevel to set.
590 */
591 public static void setLogLevel(final Level logLevel)
592 {
593 Logger.logLevel = logLevel;
594 }
595
596 }