KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > monolog > wrapper > javalog > Logger


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
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  * -------------------------------------------------------------------------
19  * $Id: Logger.java 12:18:02 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 /**
23  * Copyright (C) 2001-2003 France Telecom R&D
24  *
25  * This library is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU Lesser General Public
27  * License as published by the Free Software Foundation; either
28  * version 2 of the License, or (at your option) any later version.
29  *
30  * This library is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * Lesser General Public License for more details.
34  *
35  * You should have received a copy of the GNU Lesser General Public
36  * License along with this library; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38  */

39
40 package org.objectweb.petals.util.monolog.wrapper.javalog;
41
42 import java.util.Enumeration JavaDoc;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.util.monolog.api.Handler;
46 import org.objectweb.util.monolog.api.Level;
47 import org.objectweb.util.monolog.wrapper.common.AbstractFactory;
48 import org.objectweb.util.monolog.wrapper.javaLog.LevelImpl;
49
50 /**
51  * Is an extension of the java.util.logging.Logger class used to wrap Monolog on
52  * the logging system provided in the JDK since the 1.4 version.
53  *
54  * @author S.Chassande-Barrioz
55  * @author ddesjardins - eBMWebsourcing
56  */

57 public class Logger extends java.util.logging.Logger JavaDoc implements
58     org.objectweb.util.monolog.api.TopicalLogger {
59
60     /**
61      * This fieds is the real logger. In some cases inner is equal to this.
62      */

63     protected java.util.logging.Logger JavaDoc inner = null;
64
65     /**
66      * indicates if the logger is enabled
67      */

68     boolean enabled = true;
69
70     /**
71      * Builds a a Logger with an inner logger
72      *
73      * @param inner
74      * the real logger
75      */

76     protected Logger(java.util.logging.Logger JavaDoc inner) {
77         super(inner.getName(), inner.getResourceBundleName());
78         this.inner = inner;
79     }
80
81     /**
82      * Builds a Logger without inner logger (==> inner = this)
83      *
84      * @param name
85      * is the loggerName
86      * @param resName
87      * is the resource bundle name
88      */

89     protected Logger(String JavaDoc name, String JavaDoc resName) {
90         super(name, resName);
91         this.inner = this;
92     }
93
94     // OVERRIDE THE java.util.logging.Logger CLASS //
95
// ---------------------------------------------//
96

97     // IMPLEMENTATION OF org.objectweb.util.monolog.api.TopicalLogger INTERFACE
98
// //
99
// --------------------------------------------------------------------------//
100
/**
101      * A TopicalLogger manages a list of Handler instances. This method allows
102      * adding a handler to this list. The addHandler method returns true only if
103      * the Handler did not exist
104      */

105     public void addHandler(Handler h) throws Exception JavaDoc {
106         if (inner == this) {
107             super.addHandler((GenericHandler) h);
108             if (AbstractFactory.debug) {
109                 AbstractFactory.debug("logger(" + getName() + ").addHandler: "
110                     + h.getName() + "=>" + super.getHandlers().length);
111             }
112         } else {
113             inner.addHandler((GenericHandler) h);
114             if (AbstractFactory.debug) {
115                 AbstractFactory.debug("logger(" + getName() + ").addHandler: "
116                     + h.getName() + "=>" + inner.getHandlers().length);
117             }
118         }
119     }
120
121     // IMPLEMENT THE Handler INTERFACE //
122
// ---------------------------------//
123

124     public void addHandler(java.util.logging.Handler JavaDoc handler) {
125         GenericHandler gh = null;
126         if (handler instanceof GenericHandler) {
127             gh = (GenericHandler) handler;
128         } else {
129             gh = new GenericHandler(handler.toString(), handler);
130         }
131         if (inner == this) {
132             super.addHandler(gh);
133         } else {
134             inner.addHandler(gh);
135         }
136     }
137
138     /**
139      * This method allows adding a topic to a TopicalLogger. This actions change
140      * the hierarchical structure, but also the list of handlers. The list of
141      * handlers of a TopicalLogger is composed of its handlers and all handlers
142      * inherited from its parents. Adding a topic changes the inherited handlers
143      * list.
144      */

145     public void addTopic(String JavaDoc topic) throws Exception JavaDoc {
146     }
147
148     /**
149      * It retrieves the additivity flag for this logger instance.
150      */

151     public boolean getAdditivity() {
152         return inner.getUseParentHandlers();
153     }
154
155     /**
156      * It retrieves the value of an attribute value of the handler.
157      *
158      * @param name
159      * is an attribute name
160      */

161     public Object JavaDoc getAttribute(String JavaDoc name) {
162         return null;
163     }
164
165     /**
166      * It retrieves the attributes of the handler
167      */

168     public String JavaDoc[] getAttributeNames() {
169         return new String JavaDoc[0];
170     }
171
172     public int getCurrentIntLevel() {
173         java.util.logging.Level JavaDoc l = getLevel();
174         return (l == null ? BasicLevel.INHERIT : l.intValue());
175     }
176
177     public Level getCurrentLevel() {
178         return LevelImpl.getLevel(getCurrentIntLevel());
179     }
180
181     /**
182      * Get the Handlers associated with this logger.
183      * <p>
184      *
185      * @return an array of all registered Handlers
186      */

187     public synchronized Handler[] getHandler() {
188         Object JavaDoc[] os;
189         if (inner == this) {
190             os = super.getHandlers();
191         } else {
192             os = inner.getHandlers();
193         }
194         Handler[] hs = new Handler[os.length];
195         for (int i = 0; i < os.length; i++) {
196             if (os[i] instanceof Handler) {
197                 hs[i] = (Handler) os[i];
198             } else {
199                 hs[i] = new GenericHandler("",
200                     (java.util.logging.Handler JavaDoc) os[i]);
201             }
202         }
203         return hs;
204     }
205
206     /**
207      * It returns the handler which the name is equals to the parameter
208      *
209      * @param hn
210      * is the handler name
211      * @return an Handler or a null value.
212      */

213     public Handler getHandler(String JavaDoc hn) {
214         Object JavaDoc[] hs;
215         if (inner == this) {
216             hs = super.getHandlers();
217         } else {
218             hs = inner.getHandlers();
219         }
220         for (int i = 0; i < hs.length; i++) {
221             if (hs[i] instanceof Handler
222                 && hn.equals(((Handler) hs[i]).getName())) {
223                 return (Handler) hs[i];
224             }
225         }
226         return null;
227     }
228
229     /**
230      * This method allows getting a topic list of this TopicalLogger.
231      */

232     public String JavaDoc[] getTopic() {
233         return new String JavaDoc[] {AbstractFactory.getTopicWithoutPrefix(inner
234             .getName())};
235     }
236
237     /**
238      * This method allows getting a topic list of this TopicalLogger. Only kept
239      * for the backward compatibility.
240      */

241     public Enumeration JavaDoc getTopics() {
242         return null;
243     }
244
245     /**
246      * It retrieves the Handler type
247      */

248     public String JavaDoc getType() {
249         return "logger";
250     }
251
252     public boolean isLoggable(int l) {
253         return enabled && super.isLoggable(LevelImpl.int2Level(l));
254     }
255
256     public boolean isLoggable(org.objectweb.util.monolog.api.Level l) {
257         return enabled && super.isLoggable(LevelImpl.convertLevel(l));
258     }
259
260     public boolean isOn() {
261         return enabled;
262     }
263
264     public void log(int l, Object JavaDoc o) {
265         inner.log(LevelImpl.int2Level(l), (o == null ? "null" : o.toString()));
266     }
267
268     // IMPLEMENTATION OF org.objectweb.util.monolog.api.Logger INTERFACE //
269
// -------------------------------------------------------------------//
270

271     public void log(int level, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
272         inner.logp(LevelImpl.int2Level(level), (location == null ? "null"
273             : location.toString()), (method == null ? "null" : method
274             .toString()), (o == null ? "null" : o.toString()));
275     }
276
277     public void log(int l, Object JavaDoc o, Throwable JavaDoc t) {
278         inner.log(LevelImpl.int2Level(l), (o == null ? "null" : o.toString()),
279             t);
280     }
281
282     public void log(int level, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
283         Object JavaDoc method) {
284         inner.logp(LevelImpl.int2Level(level), (location == null ? "null"
285             : location.toString()), (method == null ? "null" : method
286             .toString()), (o == null ? "null" : o.toString()), t);
287     }
288
289     public void log(Level l, Object JavaDoc o) {
290         inner.log(LevelImpl.convertLevel(l),
291             (o == null ? "null" : o.toString()));
292     }
293
294     public void log(Level l, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
295         inner.logp(LevelImpl.convertLevel(l), (location == null ? "null"
296             : location.toString()), (method == null ? "null" : method
297             .toString()), (o == null ? "null" : o.toString()));
298     }
299
300     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t) {
301         inner.log(LevelImpl.convertLevel(l),
302             (o == null ? "null" : o.toString()), t);
303     }
304
305     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
306         Object JavaDoc method) {
307         inner.logp(LevelImpl.convertLevel(l), (location == null ? "null"
308             : location.toString()), (method == null ? "null" : method
309             .toString()), (o == null ? "null" : o.toString()), t);
310     }
311
312     /**
313      * A TopicalLogger manages a list of Handler instances. This method allows
314      * removing all handler.
315      */

316     public void removeAllHandlers() throws Exception JavaDoc {
317         java.util.logging.Handler JavaDoc[] hs = inner.getHandlers();
318         if (AbstractFactory.debug) {
319             AbstractFactory.debug("logger(" + getName()
320                 + ").removeAllHandlers(): before: " + hs.length);
321         }
322         for (int i = 0; i < hs.length; i++) {
323             if (inner == this) {
324                 super.removeHandler(hs[i]);
325             } else {
326                 inner.removeHandler(hs[i]);
327
328             }
329         }
330         if (AbstractFactory.debug) {
331             AbstractFactory.debug("logger(" + getName()
332                 + ").removeAllHandlers(): before: "
333                 + inner.getHandlers().length);
334         }
335     }
336
337     /**
338      * A TopicalLogger manages a list of Handler instances. This method allows
339      * removing a handler to this list.
340      */

341     public void removeHandler(Handler h) throws Exception JavaDoc {
342         if (inner == this) {
343             super.removeHandler((GenericHandler) h);
344         } else {
345             inner.removeHandler((GenericHandler) h);
346         }
347     }
348
349     /**
350      * This method allows removing a topic to a TopicalLogger. This actions
351      * change the hierarchical structure, but also the list of handlers. The
352      * list of handlers of a TopicalLogger is composed of its handlers and all
353      * handlers inherited from its parents. Removing a topic changes the
354      * inherited handlers list.
355      */

356     public void removeTopic(String JavaDoc topic) throws Exception JavaDoc {
357     }
358
359     /**
360      * It assigns the additivity flag for this logger instance.
361      */

362     public void setAdditivity(boolean a) {
363         inner.setUseParentHandlers(a);
364     }
365
366     /**
367      * It assigns an attributte to the handler.
368      *
369      * @param name
370      * is the attribute name
371      * @param value
372      * is the attribute value
373      * @return the old value is the attribute was already defined
374      */

375     public Object JavaDoc setAttribute(String JavaDoc name, Object JavaDoc value) {
376         return null;
377     }
378
379     public void setIntLevel(int level) {
380         inner.setLevel(LevelImpl.int2Level(level));
381     }
382
383     public void setLevel(org.objectweb.util.monolog.api.Level l) {
384         inner.setLevel(LevelImpl.convertLevel(l));
385     }
386
387     /**
388      * It assigns the name of the handler
389      */

390     public void setName(String JavaDoc name) {
391     }
392
393     public void turnOff() {
394         enabled = false;
395     }
396
397     public void turnOn() {
398         enabled = true;
399     }
400 }
401
Popular Tags