KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4jMini > MonologCategory


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.log4jMini;
20
21 import org.apache.log4j.Appender;
22 import org.apache.log4j.Category;
23 import org.apache.log4j.Priority;
24 import org.objectweb.util.monolog.api.Handler;
25 import org.objectweb.util.monolog.api.Level;
26 import org.objectweb.util.monolog.api.TopicalLogger;
27 import org.objectweb.util.monolog.api.BasicLevel;
28 import org.objectweb.util.monolog.wrapper.log4jMini.LevelImpl;
29
30 import java.util.Enumeration JavaDoc;
31 import java.util.Vector JavaDoc;
32 import java.util.Hashtable JavaDoc;
33
34 public class MonologCategory implements TopicalLogger {
35
36     protected Hashtable JavaDoc handlers;
37     protected boolean enable;
38     protected Category category;
39     protected OwPriority interPriority = null;
40
41     public MonologCategory(Category category) {
42         this.category = category;
43         enable = true;
44     }
45
46     // IMPLEMENTATION OF INTERFACE Logger
47

48     /**
49      * Set the current level of the logger
50      */

51     public void setIntLevel(int level) {
52         if (level == BasicLevel.INHERIT) {
53             category.setPriority(null);
54             return;
55         }
56         switch (level) {
57         case 10000:
58             category.setPriority(org.apache.log4j.Priority.DEBUG);
59             break;
60         case 20000:
61             category.setPriority(org.apache.log4j.Priority.INFO);
62             break;
63         case 30000:
64             category.setPriority(org.apache.log4j.Priority.WARN);
65             break;
66         case 40000:
67             category.setPriority(org.apache.log4j.Priority.ERROR);
68             break;
69         case 50000:
70             category.setPriority(org.apache.log4j.Priority.FATAL);
71             break;
72         default:
73             if (interPriority == null)
74                 interPriority = new OwPriority(level, String.valueOf(level));
75             else
76                 interPriority.level = level;
77             category.setPriority(interPriority);
78             break;
79         }
80     }
81
82     public void setLevel(Level l) {
83         if (l.getIntValue() == BasicLevel.INHERIT) {
84             category.setPriority(null);
85             return;
86         }
87         switch (l.getIntValue()) {
88         case 10000:
89             category.setPriority(org.apache.log4j.Priority.DEBUG);
90             break;
91         case 20000:
92             category.setPriority(org.apache.log4j.Priority.INFO);
93             break;
94         case 30000:
95             category.setPriority(org.apache.log4j.Priority.WARN);
96             break;
97         case 40000:
98             category.setPriority(org.apache.log4j.Priority.ERROR);
99             break;
100         case 50000:
101             category.setPriority(org.apache.log4j.Priority.FATAL);
102             break;
103         default:
104             if (interPriority == null) {
105                 interPriority = new OwPriority(l.getIntValue(), l.getName());
106             }
107             else {
108                 interPriority.level = l.getIntValue();
109             }
110             category.setPriority(interPriority);
111             break;
112         }
113     }
114
115     /**
116      * Return the current Level of the logger
117      */

118     public int getCurrentIntLevel() {
119         Priority p = category.getPriority();
120         return (p==null ? BasicLevel.INHERIT : p.toInt());
121     }
122
123     public Level getCurrentLevel() {
124         Priority p = category.getPriority();
125         return (p==null
126             ? BasicLevel.LEVEL_INHERIT
127             : LevelImpl.getLevel(p.toInt()) );
128     }
129
130     /**
131      * Check if the level parameter are not filtered by the logger
132      */

133     public boolean isLoggable(int level) {
134         return level >= category.getChainedPriority().toInt();
135     }
136
137     public boolean isLoggable(Level l) {
138         return isLoggable(l.getIntValue());
139     }
140
141     /**
142      * Is the handler enabled
143      */

144     public boolean isOn() {
145         return this.enable;
146     }
147
148     /**
149      * Log an object with a specific level. If the level parameter is
150      * loggable the object is handled.
151      */

152     public void log(int level, Object JavaDoc o) {
153         if (this.enable && this.isLoggable(level)) {
154             callLog(level, o.toString(), null);
155         }
156     }
157
158     public void log(Level l, Object JavaDoc o) {
159         log(l.getIntValue(), o.toString());
160     }
161
162     /**
163      * Log an object and a trowable with a specific level.
164      */

165     public void log(int level, Object JavaDoc o, Throwable JavaDoc t) {
166         if (this.enable && this.isLoggable(level))
167             callLog(level, o, t);
168     }
169
170     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t) {
171         log(l.getIntValue(), o, t);
172     }
173
174     /**
175      * Log an object and a trowable with a specific level. This method
176      * permits to specify an object instance and a method.
177      */

178     public void log(int level, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
179         if (this.enable && this.isLoggable(level))
180             log(level, o, null, location, method);
181     }
182
183     public void log(Level l, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
184         if (this.enable && this.isLoggable(l))
185             log(l.getIntValue(), o, null, location, method);
186     }
187
188     /**
189      * Log an object and a trowable with a specific level. This method
190      * permits to specify an object instance and a method.
191      */

192     public void log(int level, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
193                     Object JavaDoc method) {
194         if (this.enable && this.isLoggable(level))
195             callLog(level,
196                 location.toString() + "." + method.toString()
197                 + ": " + o.toString(),
198                 t);
199     }
200
201     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
202                     Object JavaDoc method) {
203         if (this.enable && this.isLoggable(l))
204             log(l.getIntValue(), o, t, location, method);
205     }
206
207     /**
208      * Enable the handler
209      */

210     public void turnOn() {
211         this.enable = true;
212     }
213
214     /**
215      * Disable the handler
216      */

217     public void turnOff() {
218         this.enable = false;
219     }
220
221     // IMPLEMENTATION OF INTERFACE TopicalLogger
222
public Handler[] getHandler() {
223         if (handlers==null) {
224             return new Handler[0];
225         }
226         Handler[] result = new Handler[handlers.size()];
227         int i = 0;
228         for (Enumeration JavaDoc e= handlers.elements(); e.hasMoreElements() ; i++) {
229             result[i] = (Handler)(e.nextElement());
230         }
231         return result;
232     }
233
234     public String JavaDoc getName() {
235         return category.getName();
236     }
237
238     public Handler getHandler(String JavaDoc hn) {
239         if (handlers==null) {
240             return null;
241         }
242         return (Handler) handlers.get(hn);
243     }
244
245     public void setName(String JavaDoc name) {
246     }
247
248     public void removeAllHandlers() throws Exception JavaDoc {
249         if (handlers!=null) {
250             handlers.clear();
251         }
252         category.removeAllAppenders();
253     }
254
255     public String JavaDoc getType() {
256         return "logger";
257     }
258
259     public void setAdditivity(boolean a) {
260         category.setAdditivity(a);
261     }
262
263     public String JavaDoc[] getAttributeNames() {
264         return new String JavaDoc[0];
265     }
266
267     public boolean getAdditivity() {
268         return category.getAdditivity();
269     }
270
271     public Object JavaDoc getAttribute(String JavaDoc name) {
272         return null;
273     }
274
275     public String JavaDoc[] getTopic() {
276         String JavaDoc[] res = new String JavaDoc[1];
277         res[0] = category.getName();
278         return res;
279     }
280
281     public Object JavaDoc setAttribute(String JavaDoc name, Object JavaDoc value) {
282         return null;
283     }
284
285     /**
286      * Add a handler in the Handler list of the topicalLogger
287      */

288     public void addHandler(Handler h) throws Exception JavaDoc {
289         if (h instanceof Appender) {
290             if (handlers==null) {
291                 handlers = new Hashtable JavaDoc();
292             }
293             category.addAppender((Appender) h);
294             handlers.put(h.getName(), h);
295         }
296         else
297             throw new Exception JavaDoc(
298                 "The type of the handler does not match with this wrapper");
299     }
300
301     /**
302      * Add a topic to the topicalLogger
303      */

304     public void addTopic(String JavaDoc topic) throws Exception JavaDoc {
305     }
306
307     /**
308      * Returns the list of the different names of the topicalLogger
309      */

310     public Enumeration JavaDoc getTopics() {
311         Vector JavaDoc v = new Vector JavaDoc(1);
312         v.addElement(category.getName());
313         return v.elements();
314     }
315
316     /**
317      * Remove a handler from the Handler list of the topicalLogger
318      */

319     public void removeHandler(Handler h) throws Exception JavaDoc {
320         if (h instanceof Appender) {
321             if (handlers!=null) {
322                 handlers.remove(h.getName());
323             }
324             this.category.removeAppender((Appender) h);
325         } else
326             throw new Exception JavaDoc(
327                 "The type of the handler does not match with this wrapper");
328     }
329
330     /**
331      * Remove a topic from the topicalLogger
332      */

333     public void removeTopic(String JavaDoc topic) throws Exception JavaDoc {
334         throw new Exception JavaDoc(
335             "The multiple name is not supported in this version");
336     }
337
338     private void callLog(int level, Object JavaDoc o, Throwable JavaDoc t) {
339         if (level > Priority.ERROR_INT)
340             this.category.fatal(o, t);
341         else if (level > Priority.WARN_INT)
342             this.category.error(o, t);
343         else if (level > Priority.INFO_INT)
344             this.category.warn(o, t);
345         else if (level > Priority.DEBUG_INT)
346             this.category.info(o, t);
347         else
348             this.category.debug(o, t);
349     }
350
351     // INNER CLASSES //
352
//---------------//
353
public class OwPriority extends org.apache.log4j.Priority {
354
355         protected int level = 10000;
356
357         public OwPriority(int l) {
358             super(l, "INTER");
359             level = l;
360         }
361         public OwPriority(int l, String JavaDoc n) {
362             super(l, n);
363             level = l;
364         }
365
366         public boolean isGreaterOrEqual(Priority pr) {
367             return level >= pr.toInt();
368         }
369     }
370 }
371
Popular Tags