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.javaLog;
018:
019: import org.objectweb.util.monolog.api.Level;
020: import org.objectweb.util.monolog.api.Handler;
021: import org.objectweb.util.monolog.api.BasicLevel;
022: import org.objectweb.util.monolog.wrapper.common.AbstractFactory;
023:
024: import java.util.Enumeration;
025:
026: /**
027: * Is an extension of the java.util.logging.Logger class used to wrap Monolog
028: * on the logging system provided in the JDK since the 1.4 version.
029: *
030: * @author S.Chassande-Barrioz
031: */
032: public class Logger extends java.util.logging.Logger implements
033: org.objectweb.util.monolog.api.TopicalLogger {
034:
035: /**
036: * indicates if the logger is enabled
037: */
038: boolean enabled = true;
039:
040: /**
041: * This fieds is the real logger. In some cases inner is equal to this.
042: */
043: protected java.util.logging.Logger inner = null;
044:
045: /**
046: * Builds a a Logger with an inner logger
047: * @param inner the real logger
048: */
049: protected Logger(java.util.logging.Logger inner) {
050: super (inner.getName(), inner.getResourceBundleName());
051: this .inner = inner;
052: }
053:
054: /**
055: * Builds a Logger without inner logger (==> inner = this)
056: * @param name is the loggerName
057: * @param resName is the resource bundle name
058: */
059: protected Logger(String name, String resName) {
060: super (name, resName);
061: this .inner = this ;
062: }
063:
064: // OVERRIDE THE java.util.logging.Logger CLASS //
065: //---------------------------------------------//
066:
067: public void addHandler(java.util.logging.Handler handler) {
068: GenericHandler gh = null;
069: if (handler instanceof GenericHandler) {
070: gh = (GenericHandler) handler;
071: } else {
072: gh = new GenericHandler(handler.toString(), handler);
073: }
074: if (inner == this ) {
075: super .addHandler(gh);
076: } else {
077: inner.addHandler(gh);
078: }
079: }
080:
081: // IMPLEMENT THE Handler INTERFACE //
082: //---------------------------------//
083:
084: /**
085: * It assigns the name of the handler
086: */
087: public void setName(String name) {
088: }
089:
090: /**
091: * It retrieves the Handler type
092: */
093: public String getType() {
094: return "logger";
095: }
096:
097: /**
098: * It retrieves the attributes of the handler
099: */
100: public String[] getAttributeNames() {
101: return new String[0];
102: }
103:
104: /**
105: * It retrieves the value of an attribute value of the handler.
106: * @param name is an attribute name
107: */
108: public Object getAttribute(String name) {
109: return null;
110: }
111:
112: /**
113: * It assigns an attributte to the handler.
114: * @param name is the attribute name
115: * @param value is the attribute value
116: * @return the old value is the attribute was already defined
117: */
118: public Object setAttribute(String name, Object value) {
119: return null;
120: }
121:
122: // IMPLEMENTATION OF org.objectweb.util.monolog.api.TopicalLogger INTERFACE //
123: //--------------------------------------------------------------------------//
124: /**
125: * A TopicalLogger manages a list of Handler instances. This method
126: * allows adding a handler to this list. The addHandler method returns
127: * true only if the Handler did not exist
128: */
129: public void addHandler(Handler h) throws Exception {
130: if (inner == this ) {
131: super .addHandler((GenericHandler) h);
132: if (AbstractFactory.debug) {
133: AbstractFactory.debug("logger(" + getName()
134: + ").addHandler: " + h.getName() + "=>"
135: + super .getHandlers().length);
136: }
137: } else {
138: inner.addHandler((GenericHandler) h);
139: if (AbstractFactory.debug) {
140: AbstractFactory.debug("logger(" + getName()
141: + ").addHandler: " + h.getName() + "=>"
142: + inner.getHandlers().length);
143: }
144: }
145: }
146:
147: /**
148: * It returns the handler which the name is equals to the parameter
149: * @param hn is the handler name
150: * @return an Handler or a null value.
151: */
152: public Handler getHandler(String hn) {
153: Object[] hs;
154: if (inner == this ) {
155: hs = super .getHandlers();
156: } else {
157: hs = inner.getHandlers();
158: }
159: for (int i = 0; i < hs.length; i++) {
160: if (hs[i] instanceof Handler
161: && hn.equals(((Handler) hs[i]).getName())) {
162: return (Handler) hs[i];
163: }
164: }
165: return null;
166: }
167:
168: /**
169: * Get the Handlers associated with this logger.
170: * <p>
171: * @return an array of all registered Handlers
172: */
173: public synchronized Handler[] getHandler() {
174: Object[] os;
175: if (inner == this ) {
176: os = super .getHandlers();
177: } else {
178: os = inner.getHandlers();
179: }
180: Handler[] hs = new Handler[os.length];
181: for (int i = 0; i < os.length; i++) {
182: if (os[i] instanceof Handler) {
183: hs[i] = (Handler) os[i];
184: } else {
185: hs[i] = new GenericHandler("",
186: (java.util.logging.Handler) 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 {
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 {
209: java.util.logging.Handler[] 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 topic) throws Exception {
250: //TODO: support multiple topic
251: }
252:
253: /**
254: *This method allows getting a topic list of this TopicalLogger.
255: */
256: public String[] getTopic() {
257: //TODO: support multiple topic
258: return new String[] { AbstractFactory
259: .getTopicWithoutPrefix(inner.getName()) };
260: }
261:
262: /**
263: * This method allows getting a topic list of this TopicalLogger.
264: * Only kept for the backward compatibility.
265: */
266: public Enumeration getTopics() {
267: //TODO:
268: return null;
269: }
270:
271: /**
272: *This method allows removing a topic to a TopicalLogger. This actions change
273: * the hierarchical structure, but also the list of handlers. The list of handlers
274: * of a TopicalLogger is composed of its handlers and all handlers inherited
275: * from its parents. Removing a topic changes the inherited handlers list.
276: */
277: public void removeTopic(String topic) throws Exception {
278: //TODO: support multiple topic
279: }
280:
281: // IMPLEMENTATION OF org.objectweb.util.monolog.api.Logger INTERFACE //
282: //-------------------------------------------------------------------//
283:
284: public void setIntLevel(int level) {
285: inner.setLevel(LevelImpl.int2Level(level));
286: }
287:
288: public void setLevel(org.objectweb.util.monolog.api.Level l) {
289: inner.setLevel(LevelImpl.convertLevel(l));
290: }
291:
292: public int getCurrentIntLevel() {
293: java.util.logging.Level l = getLevel();
294: return (l == null ? BasicLevel.INHERIT : l.intValue());
295: }
296:
297: public Level getCurrentLevel() {
298: return LevelImpl.getLevel(getCurrentIntLevel());
299: }
300:
301: public boolean isLoggable(int l) {
302: return enabled && super .isLoggable(LevelImpl.int2Level(l));
303: }
304:
305: public boolean isLoggable(org.objectweb.util.monolog.api.Level l) {
306: return enabled && super .isLoggable(LevelImpl.convertLevel(l));
307: }
308:
309: public boolean isOn() {
310: return enabled;
311: }
312:
313: public void log(int l, Object o) {
314: inner.log(LevelImpl.int2Level(l), (o == null ? "null" : o
315: .toString()));
316: }
317:
318: public void log(Level l, Object o) {
319: inner.log(LevelImpl.convertLevel(l), (o == null ? "null" : o
320: .toString()));
321: }
322:
323: public void log(int l, Object o, Throwable t) {
324: inner.log(LevelImpl.int2Level(l), (o == null ? "null" : o
325: .toString()), t);
326: }
327:
328: public void log(Level l, Object o, Throwable t) {
329: inner.log(LevelImpl.convertLevel(l), (o == null ? "null" : o
330: .toString()), t);
331: }
332:
333: public void log(int level, Object o, Object location, Object method) {
334: inner.logp(LevelImpl.int2Level(level),
335: (location == null ? "null" : location.toString()),
336: (method == null ? "null" : method.toString()),
337: (o == null ? "null" : o.toString()));
338: }
339:
340: public void log(Level l, Object o, Object location, Object method) {
341: inner.logp(LevelImpl.convertLevel(l),
342: (location == null ? "null" : location.toString()),
343: (method == null ? "null" : method.toString()),
344: (o == null ? "null" : o.toString()));
345: }
346:
347: public void log(int level, Object o, Throwable t, Object location,
348: Object method) {
349: inner.logp(LevelImpl.int2Level(level),
350: (location == null ? "null" : location.toString()),
351: (method == null ? "null" : method.toString()),
352: (o == null ? "null" : o.toString()), t);
353: }
354:
355: public void log(Level l, Object o, Throwable t, Object location,
356: Object method) {
357: inner.logp(LevelImpl.convertLevel(l),
358: (location == null ? "null" : location.toString()),
359: (method == null ? "null" : method.toString()),
360: (o == null ? "null" : o.toString()), t);
361: }
362:
363: public void turnOn() {
364: enabled = true;
365: }
366:
367: public void turnOff() {
368: enabled = false;
369: }
370: }
|