KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > javaLog > Logger


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
19 package org.objectweb.util.monolog.wrapper.javaLog;
20
21 import org.objectweb.util.monolog.api.Level;
22 import org.objectweb.util.monolog.api.Handler;
23 import org.objectweb.util.monolog.api.BasicLevel;
24 import org.objectweb.util.monolog.wrapper.common.AbstractFactory;
25
26 import java.util.Enumeration JavaDoc;
27
28 /**
29  * Is an extension of the java.util.logging.Logger class used to wrap Monolog
30  * on the logging system provided in the JDK since the 1.4 version.
31  *
32  * @author S.Chassande-Barrioz
33  */

34 public class Logger
35     extends java.util.logging.Logger JavaDoc
36     implements org.objectweb.util.monolog.api.TopicalLogger {
37
38     /**
39      * indicates if the logger is enabled
40      */

41     boolean enabled = true;
42
43     /**
44      * This fieds is the real logger. In some cases inner is equal to this.
45      */

46     protected java.util.logging.Logger JavaDoc inner = null;
47
48     /**
49      * Builds a a Logger with an inner logger
50      * @param inner the real logger
51      */

52     protected Logger(java.util.logging.Logger JavaDoc inner) {
53         super(inner.getName(), inner.getResourceBundleName());
54         this.inner = inner;
55     }
56
57     /**
58      * Builds a Logger without inner logger (==> inner = this)
59      * @param name is the loggerName
60      * @param resName is the resource bundle name
61      */

62     protected Logger(String JavaDoc name, String JavaDoc resName) {
63         super(name, resName);
64         this.inner = this;
65     }
66
67     // OVERRIDE THE java.util.logging.Logger CLASS //
68
//---------------------------------------------//
69

70     public void addHandler(java.util.logging.Handler JavaDoc handler) {
71         GenericHandler gh = null;
72         if (handler instanceof GenericHandler) {
73             gh = (GenericHandler) handler;
74         } else {
75             gh = new GenericHandler(handler.toString(), handler);
76         }
77         if (inner == this) {
78             super.addHandler(gh);
79         } else {
80             inner.addHandler(gh);
81         }
82     }
83
84     // IMPLEMENT THE Handler INTERFACE //
85
//---------------------------------//
86

87     /**
88      * It assigns the name of the handler
89      */

90     public void setName(String JavaDoc name) {
91     }
92
93     /**
94      * It retrieves the Handler type
95      */

96     public String JavaDoc getType() {
97         return "logger";
98     }
99
100     /**
101      * It retrieves the attributes of the handler
102      */

103     public String JavaDoc[] getAttributeNames() {
104         return new String JavaDoc[0];
105     }
106
107     /**
108      * It retrieves the value of an attribute value of the handler.
109      * @param name is an attribute name
110      */

111     public Object JavaDoc getAttribute(String JavaDoc name) {
112         return null;
113     }
114
115     /**
116      * It assigns an attributte to the handler.
117      * @param name is the attribute name
118      * @param value is the attribute value
119      * @return the old value is the attribute was already defined
120      */

121     public Object JavaDoc setAttribute(String JavaDoc name, Object JavaDoc value) {
122         return null;
123     }
124
125
126     // IMPLEMENTATION OF org.objectweb.util.monolog.api.TopicalLogger INTERFACE //
127
//--------------------------------------------------------------------------//
128
/**
129      * A TopicalLogger manages a list of Handler instances. This method
130      * allows adding a handler to this list. The addHandler method returns
131      * true only if the Handler did not exist
132      */

133     public void addHandler(Handler h) throws Exception JavaDoc {
134         if (inner == this) {
135             super.addHandler((GenericHandler) h);
136             if (AbstractFactory.debug) {
137                 AbstractFactory.debug("logger(" + getName() + ").addHandler: "
138                         + h.getName() + "=>" + super.getHandlers().length);
139             }
140         } else {
141             inner.addHandler((GenericHandler) h);
142             if (AbstractFactory.debug) {
143                 AbstractFactory.debug("logger(" + getName() + ").addHandler: "
144                         + h.getName() + "=>" + inner.getHandlers().length);
145             }
146         }
147     }
148
149     /**
150      * It returns the handler which the name is equals to the parameter
151      * @param hn is the handler name
152      * @return an Handler or a null value.
153      */

154     public Handler getHandler(String JavaDoc hn) {
155         Object JavaDoc[] hs;
156         if (inner == this) {
157             hs = super.getHandlers();
158         } else {
159             hs = inner.getHandlers();
160         }
161         for (int i = 0; i < hs.length; i++) {
162             if (hs[i] instanceof Handler && hn.equals(((Handler) hs[i]).getName())) {
163                 return (Handler) hs[i];
164             }
165         }
166         return null;
167     }
168
169     /**
170      * Get the Handlers associated with this logger.
171      * <p>
172      * @return an array of all registered Handlers
173      */

174     public synchronized Handler[] getHandler() {
175         Object JavaDoc[] os;
176         if (inner == this) {
177             os = super.getHandlers();
178         } else {
179             os = inner.getHandlers();
180         }
181         Handler[] hs = new Handler[os.length];
182         for(int i = 0; i < os.length; i++) {
183             if (os[i] instanceof Handler) {
184                 hs[i] = (Handler) os[i];
185             } else {
186                 hs[i] = new GenericHandler("", (java.util.logging.Handler JavaDoc) os[i]);
187             }
188         }
189         return hs;
190     }
191
192     /**
193      * A TopicalLogger manages a list of Handler instances. This method
194      * allows removing a handler to this list.
195      */

196     public void removeHandler(Handler h) throws Exception JavaDoc {
197         if (inner == this) {
198             super.removeHandler((GenericHandler) h);
199         } else {
200             inner.removeHandler((GenericHandler) h);
201         }
202     }
203
204     /**
205      * A TopicalLogger manages a list of Handler instances. This method
206      * allows removing all handler.
207      */

208     public void removeAllHandlers() throws Exception JavaDoc {
209         java.util.logging.Handler JavaDoc[] hs = inner.getHandlers();
210         if (AbstractFactory.debug) {
211             AbstractFactory.debug("logger(" + getName()
212                     + ").removeAllHandlers(): before: " + hs.length);
213         }
214         for (int i = 0; i < hs.length; i++) {
215             if (inner == this) {
216                 super.removeHandler(hs[i]);
217             } else {
218                 inner.removeHandler(hs[i]);
219
220             }
221         }
222         if (AbstractFactory.debug) {
223             AbstractFactory.debug("logger(" + getName()
224                     + ").removeAllHandlers(): before: "
225                     + inner.getHandlers().length);
226         }
227     }
228
229     /**
230      * It assigns the additivity flag for this logger instance.
231      */

232     public void setAdditivity(boolean a) {
233         inner.setUseParentHandlers(a);
234     }
235
236     /**
237      * It retrieves the additivity flag for this logger instance.
238      */

239     public boolean getAdditivity() {
240         return inner.getUseParentHandlers();
241     }
242
243     /**
244      *This method allows adding a topic to a TopicalLogger. This actions change
245      * the hierarchical structure, but also the list of handlers. The list of handlers
246      * of a TopicalLogger is composed of its handlers and all handlers inherited
247      * from its parents. Adding a topic changes the inherited handlers list.
248      */

249     public void addTopic(String JavaDoc topic) throws Exception JavaDoc {
250 //TODO: support multiple topic
251
}
252
253     /**
254      *This method allows getting a topic list of this TopicalLogger.
255      */

256     public String JavaDoc[] getTopic() {
257 //TODO: support multiple topic
258
return new String JavaDoc[] { AbstractFactory.getTopicWithoutPrefix(inner.getName()) };
259     }
260
261     /**
262      * This method allows getting a topic list of this TopicalLogger.
263      * Only kept for the backward compatibility.
264      */

265     public Enumeration JavaDoc getTopics() {
266 //TODO:
267
return null;
268     }
269
270     /**
271      *This method allows removing a topic to a TopicalLogger. This actions change
272      * the hierarchical structure, but also the list of handlers. The list of handlers
273      * of a TopicalLogger is composed of its handlers and all handlers inherited
274      * from its parents. Removing a topic changes the inherited handlers list.
275      */

276     public void removeTopic(String JavaDoc topic) throws Exception JavaDoc {
277 //TODO: support multiple topic
278
}
279
280     // IMPLEMENTATION OF org.objectweb.util.monolog.api.Logger INTERFACE //
281
//-------------------------------------------------------------------//
282

283     public void setIntLevel(int level) {
284         inner.setLevel(LevelImpl.int2Level(level));
285     }
286
287     public void setLevel(org.objectweb.util.monolog.api.Level l) {
288         inner.setLevel(LevelImpl.convertLevel(l));
289     }
290
291     public int getCurrentIntLevel() {
292         java.util.logging.Level JavaDoc l = getLevel();
293         return (l == null ? BasicLevel.INHERIT : l.intValue());
294     }
295
296     public Level getCurrentLevel() {
297         return LevelImpl.getLevel(getCurrentIntLevel());
298     }
299
300     public boolean isLoggable(int l) {
301         return enabled && super.isLoggable(LevelImpl.int2Level(l));
302     }
303
304     public boolean isLoggable(org.objectweb.util.monolog.api.Level l) {
305         return enabled && super.isLoggable(LevelImpl.convertLevel(l));
306     }
307
308     public boolean isOn() {
309         return enabled;
310     }
311
312     public void log(int l, Object JavaDoc o) {
313         inner.log(LevelImpl.int2Level(l), (o == null ? "null" : o.toString()));
314     }
315
316     public void log(Level l, Object JavaDoc o) {
317         inner.log(LevelImpl.convertLevel(l), (o == null ? "null" : o.toString()));
318     }
319
320     public void log(int l, Object JavaDoc o, Throwable JavaDoc t) {
321         inner.log(LevelImpl.int2Level(l), (o == null ? "null" : o.toString()), t);
322     }
323
324     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t) {
325         inner.log(LevelImpl.convertLevel(l), (o == null ? "null" : o.toString()), t);
326     }
327
328     public void log(int level, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
329         inner.logp(LevelImpl.int2Level(level),
330                 (location == null ? "null" : location.toString()),
331                 (method == null ? "null" : method.toString()),
332                 (o == null ? "null" : o.toString()));
333     }
334
335     public void log(Level l, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
336         inner.logp(LevelImpl.convertLevel(l),
337                 (location == null ? "null" : location.toString()),
338                 (method == null ? "null" : method.toString()),
339                 (o == null ? "null" : o.toString()));
340     }
341
342     public void log(int level, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
343                     Object JavaDoc method) {
344         inner.logp(LevelImpl.int2Level(level),
345                 (location == null ? "null" : location.toString()),
346                 (method == null ? "null" : method.toString()),
347                 (o == null ? "null" : o.toString()),
348                 t);
349     }
350
351     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
352                     Object JavaDoc method) {
353         inner.logp(LevelImpl.convertLevel(l),
354                 (location == null ? "null" : location.toString()),
355                 (method == null ? "null" : method.toString()),
356                 (o == null ? "null" : o.toString()),
357                 t);
358     }
359
360     public void turnOn() {
361         enabled = true;
362     }
363
364     public void turnOff() {
365         enabled = false;
366     }
367 }
368
Popular Tags