001: /**
002: * Copyright (C) 2001-2003 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.log4jMini;
018:
019: import org.apache.log4j.Appender;
020: import org.apache.log4j.Category;
021: import org.apache.log4j.Priority;
022: import org.objectweb.util.monolog.api.Handler;
023: import org.objectweb.util.monolog.api.Level;
024: import org.objectweb.util.monolog.api.TopicalLogger;
025: import org.objectweb.util.monolog.api.BasicLevel;
026: import org.objectweb.util.monolog.wrapper.log4jMini.LevelImpl;
027:
028: import java.util.Enumeration;
029: import java.util.Vector;
030: import java.util.Hashtable;
031:
032: public class MonologCategory implements TopicalLogger {
033:
034: protected Hashtable handlers;
035: protected boolean enable;
036: protected Category category;
037: protected OwPriority interPriority = null;
038:
039: public MonologCategory(Category category) {
040: this .category = category;
041: enable = true;
042: }
043:
044: // IMPLEMENTATION OF INTERFACE Logger
045:
046: /**
047: * Set the current level of the logger
048: */
049: public void setIntLevel(int level) {
050: if (level == BasicLevel.INHERIT) {
051: category.setPriority(null);
052: return;
053: }
054: switch (level) {
055: case 10000:
056: category.setPriority(org.apache.log4j.Priority.DEBUG);
057: break;
058: case 20000:
059: category.setPriority(org.apache.log4j.Priority.INFO);
060: break;
061: case 30000:
062: category.setPriority(org.apache.log4j.Priority.WARN);
063: break;
064: case 40000:
065: category.setPriority(org.apache.log4j.Priority.ERROR);
066: break;
067: case 50000:
068: category.setPriority(org.apache.log4j.Priority.FATAL);
069: break;
070: default:
071: if (interPriority == null)
072: interPriority = new OwPriority(level, String
073: .valueOf(level));
074: else
075: interPriority.level = level;
076: category.setPriority(interPriority);
077: break;
078: }
079: }
080:
081: public void setLevel(Level l) {
082: if (l.getIntValue() == BasicLevel.INHERIT) {
083: category.setPriority(null);
084: return;
085: }
086: switch (l.getIntValue()) {
087: case 10000:
088: category.setPriority(org.apache.log4j.Priority.DEBUG);
089: break;
090: case 20000:
091: category.setPriority(org.apache.log4j.Priority.INFO);
092: break;
093: case 30000:
094: category.setPriority(org.apache.log4j.Priority.WARN);
095: break;
096: case 40000:
097: category.setPriority(org.apache.log4j.Priority.ERROR);
098: break;
099: case 50000:
100: category.setPriority(org.apache.log4j.Priority.FATAL);
101: break;
102: default:
103: if (interPriority == null) {
104: interPriority = new OwPriority(l.getIntValue(), l
105: .getName());
106: } else {
107: interPriority.level = l.getIntValue();
108: }
109: category.setPriority(interPriority);
110: break;
111: }
112: }
113:
114: /**
115: * Return the current Level of the logger
116: */
117: public int getCurrentIntLevel() {
118: Priority p = category.getPriority();
119: return (p == null ? BasicLevel.INHERIT : p.toInt());
120: }
121:
122: public Level getCurrentLevel() {
123: Priority p = category.getPriority();
124: return (p == null ? BasicLevel.LEVEL_INHERIT : LevelImpl
125: .getLevel(p.toInt()));
126: }
127:
128: /**
129: * Check if the level parameter are not filtered by the logger
130: */
131: public boolean isLoggable(int level) {
132: return level >= category.getChainedPriority().toInt();
133: }
134:
135: public boolean isLoggable(Level l) {
136: return isLoggable(l.getIntValue());
137: }
138:
139: /**
140: * Is the handler enabled
141: */
142: public boolean isOn() {
143: return this .enable;
144: }
145:
146: /**
147: * Log an object with a specific level. If the level parameter is
148: * loggable the object is handled.
149: */
150: public void log(int level, Object o) {
151: if (this .enable && this .isLoggable(level)) {
152: callLog(level, o.toString(), null);
153: }
154: }
155:
156: public void log(Level l, Object o) {
157: log(l.getIntValue(), o.toString());
158: }
159:
160: /**
161: * Log an object and a trowable with a specific level.
162: */
163: public void log(int level, Object o, Throwable t) {
164: if (this .enable && this .isLoggable(level))
165: callLog(level, o, t);
166: }
167:
168: public void log(Level l, Object o, Throwable t) {
169: log(l.getIntValue(), o, t);
170: }
171:
172: /**
173: * Log an object and a trowable with a specific level. This method
174: * permits to specify an object instance and a method.
175: */
176: public void log(int level, Object o, Object location, Object method) {
177: if (this .enable && this .isLoggable(level))
178: log(level, o, null, location, method);
179: }
180:
181: public void log(Level l, Object o, Object location, Object method) {
182: if (this .enable && this .isLoggable(l))
183: log(l.getIntValue(), o, null, location, method);
184: }
185:
186: /**
187: * Log an object and a trowable with a specific level. This method
188: * permits to specify an object instance and a method.
189: */
190: public void log(int level, Object o, Throwable t, Object location,
191: Object method) {
192: if (this .enable && this .isLoggable(level))
193: callLog(level, location.toString() + "."
194: + method.toString() + ": " + o.toString(), t);
195: }
196:
197: public void log(Level l, Object o, Throwable t, Object location,
198: Object method) {
199: if (this .enable && this .isLoggable(l))
200: log(l.getIntValue(), o, t, location, method);
201: }
202:
203: /**
204: * Enable the handler
205: */
206: public void turnOn() {
207: this .enable = true;
208: }
209:
210: /**
211: * Disable the handler
212: */
213: public void turnOff() {
214: this .enable = false;
215: }
216:
217: // IMPLEMENTATION OF INTERFACE TopicalLogger
218: public Handler[] getHandler() {
219: if (handlers == null) {
220: return new Handler[0];
221: }
222: Handler[] result = new Handler[handlers.size()];
223: int i = 0;
224: for (Enumeration e = handlers.elements(); e.hasMoreElements(); i++) {
225: result[i] = (Handler) (e.nextElement());
226: }
227: return result;
228: }
229:
230: public String getName() {
231: return category.getName();
232: }
233:
234: public Handler getHandler(String hn) {
235: if (handlers == null) {
236: return null;
237: }
238: return (Handler) handlers.get(hn);
239: }
240:
241: public void setName(String name) {
242: }
243:
244: public void removeAllHandlers() throws Exception {
245: if (handlers != null) {
246: handlers.clear();
247: }
248: category.removeAllAppenders();
249: }
250:
251: public String getType() {
252: return "logger";
253: }
254:
255: public void setAdditivity(boolean a) {
256: category.setAdditivity(a);
257: }
258:
259: public String[] getAttributeNames() {
260: return new String[0];
261: }
262:
263: public boolean getAdditivity() {
264: return category.getAdditivity();
265: }
266:
267: public Object getAttribute(String name) {
268: return null;
269: }
270:
271: public String[] getTopic() {
272: String[] res = new String[1];
273: res[0] = category.getName();
274: return res;
275: }
276:
277: public Object setAttribute(String name, Object value) {
278: return null;
279: }
280:
281: /**
282: * Add a handler in the Handler list of the topicalLogger
283: */
284: public void addHandler(Handler h) throws Exception {
285: if (h instanceof Appender) {
286: if (handlers == null) {
287: handlers = new Hashtable();
288: }
289: category.addAppender((Appender) h);
290: handlers.put(h.getName(), h);
291: } else
292: throw new Exception(
293: "The type of the handler does not match with this wrapper");
294: }
295:
296: /**
297: * Add a topic to the topicalLogger
298: */
299: public void addTopic(String topic) throws Exception {
300: }
301:
302: /**
303: * Returns the list of the different names of the topicalLogger
304: */
305: public Enumeration getTopics() {
306: Vector v = new Vector(1);
307: v.addElement(category.getName());
308: return v.elements();
309: }
310:
311: /**
312: * Remove a handler from the Handler list of the topicalLogger
313: */
314: public void removeHandler(Handler h) throws Exception {
315: if (h instanceof Appender) {
316: if (handlers != null) {
317: handlers.remove(h.getName());
318: }
319: this .category.removeAppender((Appender) h);
320: } else
321: throw new Exception(
322: "The type of the handler does not match with this wrapper");
323: }
324:
325: /**
326: * Remove a topic from the topicalLogger
327: */
328: public void removeTopic(String topic) throws Exception {
329: throw new Exception(
330: "The multiple name is not supported in this version");
331: }
332:
333: private void callLog(int level, Object o, Throwable t) {
334: if (level > Priority.ERROR_INT)
335: this .category.fatal(o, t);
336: else if (level > Priority.WARN_INT)
337: this .category.error(o, t);
338: else if (level > Priority.INFO_INT)
339: this .category.warn(o, t);
340: else if (level > Priority.DEBUG_INT)
341: this .category.info(o, t);
342: else
343: this .category.debug(o, t);
344: }
345:
346: // INNER CLASSES //
347: //---------------//
348: public class OwPriority extends org.apache.log4j.Priority {
349:
350: protected int level = 10000;
351:
352: public OwPriority(int l) {
353: super (l, "INTER");
354: level = l;
355: }
356:
357: public OwPriority(int l, String n) {
358: super (l, n);
359: level = l;
360: }
361:
362: public boolean isGreaterOrEqual(Priority pr) {
363: return level >= pr.toInt();
364: }
365: }
366: }
|