KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > POILogger


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import java.util.*;
22
23 /**
24  * A logger interface that strives to make it as easy as possible for
25  * developers to write log calls, while simultaneously making those
26  * calls as cheap as possible by performing lazy evaluation of the log
27  * message.<p>
28  *
29  * @author Marc Johnson (mjohnson at apache dot org)
30  * @author Glen Stampoultzis (glens at apache.org)
31  * @author Nicola Ken Barozzi (nicolaken at apache.org)
32  */

33
34 public abstract class POILogger
35 {
36
37     public static final int DEBUG = 1;
38     public static final int INFO = 3;
39     public static final int WARN = 5;
40     public static final int ERROR = 7;
41     public static final int FATAL = 9;
42
43     /**
44      * package scope so it cannot be instantiated outside of the util
45      * package. You need a POILogger? Go to the POILogFactory for one
46      *
47      */

48     POILogger()
49     {}
50     
51     abstract public void initialize(final String JavaDoc cat);
52     
53     abstract public void log(final int level, final Object JavaDoc obj1);
54
55     /**
56      * Check if a logger is enabled to log at the specified level
57      *
58      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
59      */

60     abstract public boolean check(final int level);
61
62     /**
63      * Log a message. Lazily appends Object parameters together.
64      *
65      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
66      * @param obj1 first object to place in the message
67      * @param obj2 second object to place in the message
68      */

69
70    /**
71      * Log a message. Lazily appends Object parameters together.
72      *
73      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
74      * @param obj1 first object to place in the message
75      * @param obj2 second object to place in the message
76      */

77
78     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2)
79     {
80         if (check(level))
81         {
82             log(level, new StringBuffer JavaDoc(32).append(obj1).append(obj2));
83         }
84     }
85
86     /**
87      * Log a message. Lazily appends Object parameters together.
88      *
89      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
90      * @param obj1 first Object to place in the message
91      * @param obj2 second Object to place in the message
92      * @param obj3 third Object to place in the message
93      */

94
95     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
96                     final Object JavaDoc obj3)
97     {
98         
99
100         if (check(level))
101         {
102             log(level,
103                     new StringBuffer JavaDoc(48).append(obj1).append(obj2)
104                         .append(obj3));
105         }
106     }
107
108     /**
109      * Log a message. Lazily appends Object parameters together.
110      *
111      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
112      * @param obj1 first Object to place in the message
113      * @param obj2 second Object to place in the message
114      * @param obj3 third Object to place in the message
115      * @param obj4 fourth Object to place in the message
116      */

117
118     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
119                     final Object JavaDoc obj3, final Object JavaDoc obj4)
120     {
121         
122
123         if (check(level))
124         {
125             log(level,
126                     new StringBuffer JavaDoc(64).append(obj1).append(obj2)
127                         .append(obj3).append(obj4));
128         }
129     }
130
131     /**
132      * Log a message. Lazily appends Object parameters together.
133      *
134      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
135      * @param obj1 first Object to place in the message
136      * @param obj2 second Object to place in the message
137      * @param obj3 third Object to place in the message
138      * @param obj4 fourth Object to place in the message
139      * @param obj5 fifth Object to place in the message
140      */

141
142     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
143                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5)
144     {
145         
146
147         if (check(level))
148         {
149             log(level,
150                     new StringBuffer JavaDoc(80).append(obj1).append(obj2)
151                         .append(obj3).append(obj4).append(obj5));
152         }
153     }
154
155     /**
156      * Log a message. Lazily appends Object parameters together.
157      *
158      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
159      * @param obj1 first Object to place in the message
160      * @param obj2 second Object to place in the message
161      * @param obj3 third Object to place in the message
162      * @param obj4 fourth Object to place in the message
163      * @param obj5 fifth Object to place in the message
164      * @param obj6 sixth Object to place in the message
165      */

166
167     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
168                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5,
169                     final Object JavaDoc obj6)
170     {
171         
172
173         if (check(level))
174         {
175             log(level ,
176                     new StringBuffer JavaDoc(96).append(obj1).append(obj2)
177                         .append(obj3).append(obj4).append(obj5).append(obj6));
178         }
179     }
180
181     /**
182      * Log a message. Lazily appends Object parameters together.
183      *
184      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
185      * @param obj1 first Object to place in the message
186      * @param obj2 second Object to place in the message
187      * @param obj3 third Object to place in the message
188      * @param obj4 fourth Object to place in the message
189      * @param obj5 fifth Object to place in the message
190      * @param obj6 sixth Object to place in the message
191      * @param obj7 seventh Object to place in the message
192      */

193
194     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
195                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5,
196                     final Object JavaDoc obj6, final Object JavaDoc obj7)
197     {
198         
199
200         if (check(level))
201         {
202             log(level,
203                     new StringBuffer JavaDoc(112).append(obj1).append(obj2)
204                         .append(obj3).append(obj4).append(obj5).append(obj6)
205                         .append(obj7));
206         }
207     }
208
209     /**
210      * Log a message. Lazily appends Object parameters together.
211      *
212      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
213      * @param obj1 first Object to place in the message
214      * @param obj2 second Object to place in the message
215      * @param obj3 third Object to place in the message
216      * @param obj4 fourth Object to place in the message
217      * @param obj5 fifth Object to place in the message
218      * @param obj6 sixth Object to place in the message
219      * @param obj7 seventh Object to place in the message
220      * @param obj8 eighth Object to place in the message
221      */

222
223     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
224                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5,
225                     final Object JavaDoc obj6, final Object JavaDoc obj7, final Object JavaDoc obj8)
226     {
227         
228
229         if (check(level))
230         {
231             log(level,
232                     new StringBuffer JavaDoc(128).append(obj1).append(obj2)
233                         .append(obj3).append(obj4).append(obj5).append(obj6)
234                         .append(obj7).append(obj8));
235         }
236     }
237
238     /**
239      * Log a message
240      *
241      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
242      * @param obj1 The object to log. This is converted to a string.
243      * @param exception An exception to be logged
244      */

245
246     public void log(final int level, final Object JavaDoc obj1,
247                     final Throwable JavaDoc exception)
248     {
249         log(level , obj1, exception);
250     }
251
252     /**
253      * Log a message. Lazily appends Object parameters together.
254      *
255      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
256      * @param obj1 first Object to place in the message
257      * @param obj2 second Object to place in the message
258      * @param exception An exception to be logged
259      */

260
261     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
262                     final Throwable JavaDoc exception)
263     {
264         
265
266         if (check(level))
267         {
268             log(level, new StringBuffer JavaDoc(32).append(obj1).append(obj2),
269                     exception);
270         }
271     }
272
273     /**
274      * Log a message. Lazily appends Object parameters together.
275      *
276      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
277      * @param obj1 first Object to place in the message
278      * @param obj2 second Object to place in the message
279      * @param obj3 third object to place in the message
280      * @param exception An error message to be logged
281      */

282
283     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
284                     final Object JavaDoc obj3, final Throwable JavaDoc exception)
285     {
286         
287
288         if (check(level))
289         {
290             log(level, new StringBuffer JavaDoc(48).append(obj1).append(obj2)
291                 .append(obj3), exception);
292         }
293     }
294
295     /**
296      * Log a message. Lazily appends Object parameters together.
297      *
298      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
299      * @param obj1 first Object to place in the message
300      * @param obj2 second Object to place in the message
301      * @param obj3 third object to place in the message
302      * @param obj4 fourth object to place in the message
303      * @param exception An exception to be logged
304      */

305
306     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
307                     final Object JavaDoc obj3, final Object JavaDoc obj4,
308                     final Throwable JavaDoc exception)
309     {
310         
311
312         if (check(level))
313         {
314             log(level, new StringBuffer JavaDoc(64).append(obj1).append(obj2)
315                 .append(obj3).append(obj4), exception);
316         }
317     }
318
319     /**
320      * Log a message. Lazily appends Object parameters together.
321      *
322      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
323      * @param obj1 first Object to place in the message
324      * @param obj2 second Object to place in the message
325      * @param obj3 third object to place in the message
326      * @param obj4 fourth object to place in the message
327      * @param obj5 fifth object to place in the message
328      * @param exception An exception to be logged
329      */

330
331     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
332                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5,
333                     final Throwable JavaDoc exception)
334     {
335         
336
337         if (check(level))
338         {
339             log(level, new StringBuffer JavaDoc(80).append(obj1).append(obj2)
340                 .append(obj3).append(obj4).append(obj5), exception);
341         }
342     }
343
344     /**
345      * Log a message. Lazily appends Object parameters together.
346      *
347      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
348      * @param obj1 first Object to place in the message
349      * @param obj2 second Object to place in the message
350      * @param obj3 third object to place in the message
351      * @param obj4 fourth object to place in the message
352      * @param obj5 fifth object to place in the message
353      * @param obj6 sixth object to place in the message
354      * @param exception An exception to be logged
355      */

356
357     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
358                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5,
359                     final Object JavaDoc obj6, final Throwable JavaDoc exception)
360     {
361         
362
363         if (check(level))
364         {
365             log(level , new StringBuffer JavaDoc(96).append(obj1)
366                 .append(obj2).append(obj3).append(obj4).append(obj5)
367                 .append(obj6), exception);
368         }
369     }
370
371     /**
372      * Log a message. Lazily appends Object parameters together.
373      *
374      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
375      * @param obj1 first Object to place in the message
376      * @param obj2 second Object to place in the message
377      * @param obj3 third object to place in the message
378      * @param obj4 fourth object to place in the message
379      * @param obj5 fifth object to place in the message
380      * @param obj6 sixth object to place in the message
381      * @param obj7 seventh object to place in the message
382      * @param exception An exception to be logged
383      */

384
385     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
386                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5,
387                     final Object JavaDoc obj6, final Object JavaDoc obj7,
388                     final Throwable JavaDoc exception)
389     {
390         
391
392         if (check(level))
393         {
394             log(level, new StringBuffer JavaDoc(112).append(obj1).append(obj2)
395                 .append(obj3).append(obj4).append(obj5).append(obj6)
396                 .append(obj7), exception);
397         }
398     }
399
400     /**
401      * Log a message. Lazily appends Object parameters together.
402      *
403      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
404      * @param obj1 first Object to place in the message
405      * @param obj2 second Object to place in the message
406      * @param obj3 third object to place in the message
407      * @param obj4 fourth object to place in the message
408      * @param obj5 fifth object to place in the message
409      * @param obj6 sixth object to place in the message
410      * @param obj7 seventh object to place in the message
411      * @param obj8 eighth object to place in the message
412      * @param exception An exception to be logged
413      */

414
415     public void log(final int level, final Object JavaDoc obj1, final Object JavaDoc obj2,
416                     final Object JavaDoc obj3, final Object JavaDoc obj4, final Object JavaDoc obj5,
417                     final Object JavaDoc obj6, final Object JavaDoc obj7, final Object JavaDoc obj8,
418                     final Throwable JavaDoc exception)
419     {
420         
421
422         if (check(level))
423         {
424             log(level, new StringBuffer JavaDoc(128).append(obj1).append(obj2)
425                 .append(obj3).append(obj4).append(obj5).append(obj6)
426                 .append(obj7).append(obj8), exception);
427         }
428     }
429
430     /**
431      * Logs a formated message. The message itself may contain %
432      * characters as place holders. This routine will attempt to match
433      * the placeholder by looking at the type of parameter passed to
434      * obj1.<p>
435      *
436      * If the parameter is an array, it traverses the array first and
437      * matches parameters sequentially against the array items.
438      * Otherwise the parameters after <code>message</code> are matched
439      * in order.<p>
440      *
441      * If the place holder matches against a number it is printed as a
442      * whole number. This can be overridden by specifying a precision
443      * in the form %n.m where n is the padding for the whole part and
444      * m is the number of decimal places to display. n can be excluded
445      * if desired. n and m may not be more than 9.<p>
446      *
447      * If the last parameter (after flattening) is a Throwable it is
448      * logged specially.
449      *
450      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
451      * @param message The message to log.
452      * @param obj1 The first object to match against.
453      */

454
455     public void logFormatted(final int level, final String JavaDoc message,
456                              final Object JavaDoc obj1)
457     {
458         commonLogFormatted(level, message, new Object JavaDoc[]
459         {
460             obj1
461         });
462     }
463
464     /**
465      * Logs a formated message. The message itself may contain %
466      * characters as place holders. This routine will attempt to match
467      * the placeholder by looking at the type of parameter passed to
468      * obj1.<p>
469      *
470      * If the parameter is an array, it traverses the array first and
471      * matches parameters sequentially against the array items.
472      * Otherwise the parameters after <code>message</code> are matched
473      * in order.<p>
474      *
475      * If the place holder matches against a number it is printed as a
476      * whole number. This can be overridden by specifying a precision
477      * in the form %n.m where n is the padding for the whole part and
478      * m is the number of decimal places to display. n can be excluded
479      * if desired. n and m may not be more than 9.<p>
480      *
481      * If the last parameter (after flattening) is a Throwable it is
482      * logged specially.
483      *
484      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
485      * @param message The message to log.
486      * @param obj1 The first object to match against.
487      * @param obj2 The second object to match against.
488      */

489
490     public void logFormatted(final int level, final String JavaDoc message,
491                              final Object JavaDoc obj1, final Object JavaDoc obj2)
492     {
493         commonLogFormatted(level, message, new Object JavaDoc[]
494         {
495             obj1, obj2
496         });
497     }
498
499     /**
500      * Logs a formated message. The message itself may contain %
501      * characters as place holders. This routine will attempt to match
502      * the placeholder by looking at the type of parameter passed to
503      * obj1.<p>
504      *
505      * If the parameter is an array, it traverses the array first and
506      * matches parameters sequentially against the array items.
507      * Otherwise the parameters after <code>message</code> are matched
508      * in order.<p>
509      *
510      * If the place holder matches against a number it is printed as a
511      * whole number. This can be overridden by specifying a precision
512      * in the form %n.m where n is the padding for the whole part and
513      * m is the number of decimal places to display. n can be excluded
514      * if desired. n and m may not be more than 9.<p>
515      *
516      * If the last parameter (after flattening) is a Throwable it is
517      * logged specially.
518      *
519      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
520      * @param message The message to log.
521      * @param obj1 The first object to match against.
522      * @param obj2 The second object to match against.
523      * @param obj3 The third object to match against.
524      */

525
526     public void logFormatted(final int level, final String JavaDoc message,
527                              final Object JavaDoc obj1, final Object JavaDoc obj2,
528                              final Object JavaDoc obj3)
529     {
530         commonLogFormatted(level, message, new Object JavaDoc[]
531         {
532             obj1, obj2, obj3
533         });
534     }
535
536     /**
537      * Logs a formated message. The message itself may contain %
538      * characters as place holders. This routine will attempt to match
539      * the placeholder by looking at the type of parameter passed to
540      * obj1.<p>
541      *
542      * If the parameter is an array, it traverses the array first and
543      * matches parameters sequentially against the array items.
544      * Otherwise the parameters after <code>message</code> are matched
545      * in order.<p>
546      *
547      * If the place holder matches against a number it is printed as a
548      * whole number. This can be overridden by specifying a precision
549      * in the form %n.m where n is the padding for the whole part and
550      * m is the number of decimal places to display. n can be excluded
551      * if desired. n and m may not be more than 9.<p>
552      *
553      * If the last parameter (after flattening) is a Throwable it is
554      * logged specially.
555      *
556      * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
557      * @param message The message to log.
558      * @param obj1 The first object to match against.
559      * @param obj2 The second object to match against.
560      * @param obj3 The third object to match against.
561      * @param obj4 The forth object to match against.
562      */

563
564     public void logFormatted(final int level, final String JavaDoc message,
565                              final Object JavaDoc obj1, final Object JavaDoc obj2,
566                              final Object JavaDoc obj3, final Object JavaDoc obj4)
567     {
568         commonLogFormatted(level, message, new Object JavaDoc[]
569         {
570             obj1, obj2, obj3, obj4
571         });
572     }
573
574     private void commonLogFormatted(final int level, final String JavaDoc message,
575                                     final Object JavaDoc [] unflatParams)
576     {
577         
578
579         if (check(level))
580         {
581             Object JavaDoc[] params = flattenArrays(unflatParams);
582
583             if (params[ params.length - 1 ] instanceof Throwable JavaDoc)
584             {
585                 log(level, StringUtil.format(message, params),
586                     ( Throwable JavaDoc ) params[ params.length - 1 ]);
587             }
588             else
589             {
590                 log(level, StringUtil.format(message, params));
591             }
592         }
593     }
594
595     /**
596      * Flattens any contained objects. Only tranverses one level deep.
597      */

598
599     private Object JavaDoc [] flattenArrays(final Object JavaDoc [] objects)
600     {
601         List results = new ArrayList();
602
603         for (int i = 0; i < objects.length; i++)
604         {
605             results.addAll(objectToObjectArray(objects[ i ]));
606         }
607         return ( Object JavaDoc [] ) results.toArray(new Object JavaDoc[ results.size() ]);
608     }
609
610     private List objectToObjectArray(Object JavaDoc object)
611     {
612         List results = new ArrayList();
613
614         if (object instanceof byte [])
615         {
616             byte[] array = ( byte [] ) object;
617
618             for (int j = 0; j < array.length; j++)
619             {
620                 results.add(new Byte JavaDoc(array[ j ]));
621             }
622         }
623         if (object instanceof char [])
624         {
625             char[] array = ( char [] ) object;
626
627             for (int j = 0; j < array.length; j++)
628             {
629                 results.add(new Character JavaDoc(array[ j ]));
630             }
631         }
632         else if (object instanceof short [])
633         {
634             short[] array = ( short [] ) object;
635
636             for (int j = 0; j < array.length; j++)
637             {
638                 results.add(new Short JavaDoc(array[ j ]));
639             }
640         }
641         else if (object instanceof int [])
642         {
643             int[] array = ( int [] ) object;
644
645             for (int j = 0; j < array.length; j++)
646             {
647                 results.add(new Integer JavaDoc(array[ j ]));
648             }
649         }
650         else if (object instanceof long [])
651         {
652             long[] array = ( long [] ) object;
653
654             for (int j = 0; j < array.length; j++)
655             {
656                 results.add(new Long JavaDoc(array[ j ]));
657             }
658         }
659         else if (object instanceof float [])
660         {
661             float[] array = ( float [] ) object;
662
663             for (int j = 0; j < array.length; j++)
664             {
665                 results.add(new Float JavaDoc(array[ j ]));
666             }
667         }
668         else if (object instanceof double [])
669         {
670             double[] array = ( double [] ) object;
671
672             for (int j = 0; j < array.length; j++)
673             {
674                 results.add(new Double JavaDoc(array[ j ]));
675             }
676         }
677         else if (object instanceof Object JavaDoc [])
678         {
679             Object JavaDoc[] array = ( Object JavaDoc [] ) object;
680
681             for (int j = 0; j < array.length; j++)
682             {
683                 results.add(array[ j ]);
684             }
685         }
686         else
687         {
688             results.add(object);
689         }
690         return results;
691     }
692                                  
693 } // end package scope abstract class POILogger
694

695
Popular Tags