KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > printwriter > LoggerImpl


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.wrapper.printwriter;
19
20 import org.objectweb.util.monolog.api.BasicLevel;
21 import org.objectweb.util.monolog.api.Handler;
22 import org.objectweb.util.monolog.api.Level;
23 import org.objectweb.util.monolog.api.MonologFactoryListener;
24 import org.objectweb.util.monolog.api.TopicalLogger;
25 import org.objectweb.util.monolog.api.Logger;
26 import org.objectweb.util.monolog.api.MonologFactory;
27 import org.objectweb.util.monolog.wrapper.common.EnumrationImpl;
28 import org.objectweb.util.monolog.wrapper.common.LevelImpl;
29
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 /**
41  * This class is a simple implementation of the Logger interface provided by
42  * the monolog specification.
43  *
44  *@author sebastien.chassande@inrialpes.fr
45  */

46 public class LoggerImpl
47     implements TopicalLogger, MonologFactory {
48
49     public final static String JavaDoc PRINT_WRITER = "printwriter";
50
51     /**
52      * The current level of this logger
53      */

54     private int level = BasicLevel.DEBUG;
55     private Level lev = null;
56
57     protected String JavaDoc name = null;
58     protected Vector JavaDoc topics = null;
59     protected Hashtable JavaDoc handlers = null;
60     protected boolean additivity = true;
61     protected Map JavaDoc levels = null;
62
63     /**
64      * The printwriter which messages are written.
65      */

66     private PrintWriter JavaDoc pw = null;
67
68     private boolean isOn = true;
69
70     /**
71      * This constructor permits to specify the printWriter linked to this
72      * logger
73      *
74      */

75     public LoggerImpl() {
76         this(null);
77     }
78
79     /**
80      * This constructor permits to specify the printWriter linked to this
81      * logger
82      *
83      *@param _pw the printwriter
84      */

85     public LoggerImpl(PrintWriter JavaDoc _pw) {
86         this("org.objectweb.util.monolog.wrapper.printwriter", _pw);
87     }
88
89     /**
90      * This constructor permits to specify the printWriter linked to this
91      * logger
92      *
93      *@param _pw the printwriter
94      */

95     public LoggerImpl(String JavaDoc n, PrintWriter JavaDoc _pw) {
96         name = n;
97         pw = _pw;
98     }
99
100     public PrintWriter JavaDoc getPrintWriter() {
101         return pw;
102     }
103
104     public void addMonologFactoryListener(MonologFactoryListener mfl) {
105     }
106
107     public void removeMonologFactoryListener(MonologFactoryListener mfl) {
108     }
109
110     // IMPLEMENTATION OF THE HandlerFactory INTERFACE //
111
//-----------------------------------------------//
112
public void configure(Properties JavaDoc prop) throws Exception JavaDoc {
113     }
114
115     // IMPLEMENTATION OF THE HandlerFactory INTERFACE //
116
//-----------------------------------------------//
117
public Handler createHandler(String JavaDoc hn, String JavaDoc handlertype) {
118         return this;
119     }
120
121     public Handler removeHandler(String JavaDoc handlername) {
122         return this;
123     }
124
125     public Handler[] getHandlers() {
126         return new Handler[] {this};
127     }
128
129
130     // IMPLEMENTATION OF THE LevelFactory INTERFACE //
131
//-----------------------------------------------//
132

133     public synchronized Level defineLevel(String JavaDoc name, int value) {
134         Level l = new LevelImpl(name, value);
135         if (levels == null) {
136             levels = new HashMap JavaDoc();
137         }
138         levels.put(new Integer JavaDoc(value), l);
139         return l;
140     }
141
142     public Level defineLevel(String JavaDoc name, String JavaDoc value) {
143         Level l = new LevelImpl(name, value, this);
144         if (levels == null) {
145             levels = new HashMap JavaDoc();
146         }
147         levels.put(new Integer JavaDoc(value), l);
148         return l;
149     }
150
151     public Level getLevel(String JavaDoc name) {
152         Iterator JavaDoc it = levels.values().iterator();
153         while(it.hasNext()) {
154             Level l = (Level) it.next();
155             if (l.getName().equals(name)) {
156                 return l;
157             }
158         }
159         return null;
160     }
161
162     /**
163      * This method is not synchronized because the configuration is rarely
164      */

165     public Level getLevel(int value) {
166         return (Level) levels.get(new Integer JavaDoc(value));
167     }
168
169     /**
170      * This method is not synchronized because the configuration is rarely
171      */

172     public Level[] getLevels() {
173         return (Level[]) levels.values().toArray(new Level[levels.size()]);
174     }
175
176     public synchronized void removeLevel(String JavaDoc name) {
177         Iterator JavaDoc it = levels.values().iterator();
178         while(it.hasNext()) {
179             Level l = (Level) it.next();
180             if (l.getName().equals(name)) {
181                 it.remove();
182             }
183         }
184     }
185
186
187     // IMPLEMENTATION OF THE LoggerFactory INTERFACE //
188
//-----------------------------------------------//
189

190     public Logger getLogger(String JavaDoc key) {
191         return this;
192     }
193
194     public Logger getLogger(String JavaDoc key, String JavaDoc resourceBundleName) {
195         return this;
196     }
197
198     public String JavaDoc getResourceBundleName() {
199         return null;
200     }
201
202     public void setResourceBundleName(String JavaDoc resourceBundleName) {
203     }
204
205     public Logger[] getLoggers() {
206         return new Logger[]{this};
207     }
208
209     public String JavaDoc getTopicPrefix() {
210         return null;
211     }
212     
213
214
215     // IMPLEMENTATION OF THE TopicalLogger INTERFACE //
216
//-----------------------------------------------//
217

218     /**
219      * Sets the IntLevel attribute of the LoggerImpl object
220      *
221      *@param l The new IntLevel value
222      */

223     public void setIntLevel(int l) {
224         level = l;
225     }
226
227     /**
228      * Sets the Level attribute of the LoggerImpl object
229      *
230      *@param l The new Level value
231      */

232     public void setLevel(Level l) {
233         lev = l;
234         if (lev != null) {
235             level = lev.getIntValue();
236         }
237     }
238
239     /**
240      * Gets the CurrentIntLevel attribute of the LoggerImpl object
241      *
242      *@return The CurrentIntLevel value
243      */

244     public int getCurrentIntLevel() {
245         return level;
246     }
247
248     /**
249      * Gets the CurrentLevel attribute of the LoggerImpl object
250      *
251      *@return The CurrentLevel value
252      */

253     public Level getCurrentLevel() {
254         return (lev == null
255             ? lev = new LevelImpl(
256                 (level ==-1
257                     ?"INHERIT"
258                 :"INTER"),
259                 level)
260             : lev);
261     }
262
263     /**
264      * Gets the Loggable attribute of the LoggerImpl object
265      *
266      *@param l Description of Parameter
267      *@return The Loggable value
268      */

269     public boolean isLoggable(int l) {
270         return pw != null && l >= level;
271     }
272
273     /**
274      * Gets the Loggable attribute of the LoggerImpl object
275      *
276      *@param l Description of Parameter
277      *@return The Loggable value
278      */

279     public boolean isLoggable(Level l) {
280         return pw != null && l != null && l.getIntValue() >= level;
281     }
282
283     /**
284      * Gets the On attribute of the LoggerImpl object
285      *
286      *@return The On value
287      */

288     public boolean isOn() {
289         return isOn;
290     }
291
292     /**
293      * Gets the Topics attribute of the LoggerImpl object
294      *
295      *@return The Topics value
296      */

297     public Enumeration JavaDoc getTopics() {
298         if (topics == null) {
299             String JavaDoc[] ar = {name};
300             return new EnumrationImpl(ar);
301         }
302         else {
303             String JavaDoc[] str=new String JavaDoc[topics.size()];
304             for (int i=0; i<topics.size(); i++ ) {
305                 str[i]=(String JavaDoc)(topics.elementAt(i));
306             }
307             return new EnumrationImpl(str);
308         }
309     }
310
311     /**
312      * Log method
313      */

314     public void log(int level, Object JavaDoc o) {
315         if (pw == null || !isOn() || o == null || !isLoggable(level)) {
316             return;
317         }
318         if (level == BasicLevel.ERROR) {
319             pw.println("**" + format(o.toString(), 2));
320         }
321         else {
322             pw.println(format(o.toString(), 2));
323         }
324     }
325
326     /**
327      * Log method
328      */

329     public void log(Level l, Object JavaDoc o) {
330     }
331
332     /**
333      * Log method
334      */

335     public void log(int level, Object JavaDoc o, Throwable JavaDoc t) {
336         if (pw == null || !isOn() || o == null || !isLoggable(level)) {
337             return;
338         }
339         pw.println(o.toString() + ":" + t.toString());
340     }
341
342     /**
343      * Log method
344      */

345     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t) {
346     }
347
348     /**
349      * Log method
350      */

351     public void log(int level, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
352         if (pw == null || !isOn() || o == null || !isLoggable(level)) {
353             return;
354         }
355         pw.println(location.toString() + "." + method.toString() + "(...) :"
356             + o.toString());
357     }
358
359     /**
360      * Log method
361      */

362     public void log(Level l, Object JavaDoc o, Object JavaDoc location,
363                     Object JavaDoc method) {
364     }
365
366     /**
367      * Log method
368      */

369     public void log(int level, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location, Object JavaDoc method) {
370         if (pw == null || !isOn() || o == null || !isLoggable(level)) {
371             return;
372         }
373         pw.println(location.toString() + "." + method.toString() + "(...) :"
374             + o.toString() + " " + t.toString());
375     }
376
377     /**
378      * Log method
379      */

380     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
381                     Object JavaDoc method) {
382     }
383
384     /**
385      * Turn on this logger
386      */

387     public void turnOn() {
388         isOn = true;
389     }
390
391     /**
392      * Turn off this logger
393      */

394     public void turnOff() {
395         isOn = false;
396     }
397
398     /**
399      * The toString method is override to signal the logger imlementation
400      * fowards its messages to a printwriter
401      */

402     public String JavaDoc toString() {
403         return "Personnal implementation on PrintWriter object";
404     }
405
406
407     // IMPLEMENTATION OF THE TopicalLogger INTERFACE //
408
//-----------------------------------------------//
409

410     public void addHandler(Handler h) throws Exception JavaDoc {
411         if (h != null) {
412             if (handlers == null) {
413                 handlers = new Hashtable JavaDoc();
414             }
415             handlers.put(h.getName(), h);
416         }
417     }
418
419     public void addTopic(String JavaDoc topic) throws Exception JavaDoc {
420         if (topics == null) {
421             topics = new Vector JavaDoc();
422             topics.addElement(name);
423         }
424         topics.addElement(topic);
425     }
426
427     public void removeHandler(Handler h) throws Exception JavaDoc {
428         if (h != null && handlers != null) {
429             handlers.remove(h.getName());
430         }
431     }
432
433     public void removeAllHandlers() throws Exception JavaDoc {
434         if (handlers!=null) {
435             handlers.clear();
436         }
437     }
438
439     public void removeTopic(String JavaDoc topic) throws Exception JavaDoc {
440         if (topics != null) {
441             topics.removeElement(topic);
442         }
443     }
444
445     public void setAdditivity(boolean a) {
446         additivity = a;
447     }
448
449     public boolean getAdditivity() {
450         return additivity;
451     }
452
453     public Handler[] getHandler() {
454         Handler[] result;
455         if (handlers == null)
456             result = new Handler[0];
457         else {
458             result = new Handler[handlers.size()];
459             int i =0;
460             for (Enumeration JavaDoc e=handlers.elements(); e.hasMoreElements(); i++) {
461                 result[i] = (Handler)(e.nextElement());
462             }
463         }
464         return (result);
465     }
466
467     public Handler getHandler(String JavaDoc hn) {
468         return (handlers == null ? null : (Handler) handlers.get(hn));
469     }
470
471     public String JavaDoc[] getTopic() {
472         if (topics == null) {
473             String JavaDoc[] ar = {name};
474             return ar;
475         }
476         else {
477             String JavaDoc[] str=new String JavaDoc[topics.size()];
478             for (int i=0; i<topics.size(); i++ ) {
479                 str[i]=(String JavaDoc)(topics.elementAt(i));
480             }
481             return str;
482         }
483     }
484
485     // IMPLEMENTATION OF THE Handler INTERFACE //
486
//-----------------------------------------//
487
public String JavaDoc getName() {
488         return name;
489     }
490
491     public void setName(String JavaDoc n) {
492         name = n;
493     }
494
495     public String JavaDoc getType() {
496         return "logger";
497     }
498
499     public String JavaDoc[] getAttributeNames() {
500         String JavaDoc[] ar = {PRINT_WRITER};
501         return ar;
502     }
503
504     public Object JavaDoc getAttribute(String JavaDoc name) {
505         if (PRINT_WRITER.equalsIgnoreCase(name)) {
506             return pw;
507         }
508         return null;
509     }
510
511     public Object JavaDoc setAttribute(String JavaDoc name, Object JavaDoc value) {
512         if (PRINT_WRITER.equalsIgnoreCase(name)
513             && value != null
514             && value instanceof PrintWriter JavaDoc) {
515             pw = (PrintWriter JavaDoc) value;
516         }
517         return null;
518     }
519
520     /**
521      * This method permits to format messages. More exatcly this method find
522      * the class name and the method name where the log call was done.
523      * In order to find the right class name and method name, this method is
524      * parametrable either the number of call done in this logger.
525      */

526     public static String JavaDoc format(String JavaDoc msg, int removeTopStack) {
527         Throwable JavaDoc t = new Throwable JavaDoc().fillInStackTrace();
528         StringWriter JavaDoc sw = new StringWriter JavaDoc();
529         t.printStackTrace(new PrintWriter JavaDoc(sw));
530         String JavaDoc m = sw.getBuffer().toString();
531
532         int deb = -1;
533
534         int fin = 0;
535
536         // take the right line
537
deb = -1;
538         for (int i = 0; i < (removeTopStack + 1); i++) {
539             deb = m.indexOf("\n", deb + 1);
540         }
541
542         deb = m.indexOf("at ", deb);
543         fin = m.indexOf("\n", deb);
544         m = m.substring(deb + 3, fin);
545
546         // remove param
547
deb = m.indexOf("(");
548         fin = m.indexOf(":");
549         m = m.substring(0, deb + 1) + m.substring(fin + 1, m.length());
550
551         // remove package name
552
deb = m.indexOf("(");
553         int c1 = 0;
554         int c2 = 0;
555         int current = m.indexOf(".");
556         while (current != -1 && current < deb) {
557             c1 = c2;
558             c2 = current;
559             current = m.indexOf(".", current + 1);
560         }
561         m = m.substring(c1 + 1, m.length());
562
563         return m + ": " + msg;
564     }
565
566     static {
567         if (BasicLevel.FATAL <= 0
568             || BasicLevel.ERROR <= 0
569             || BasicLevel.WARN <= 0
570             || BasicLevel.INFO <= 0
571             || BasicLevel.DEBUG <= 0) {
572             BasicLevel.FATAL = 50000;
573             BasicLevel.ERROR = 40000;
574             BasicLevel.WARN = 30000;
575             BasicLevel.INFO = 20000;
576             BasicLevel.DEBUG = 10000;
577         }
578     }
579 }
580
581
Popular Tags