001: package net.sourceforge.squirrel_sql.fw.util.log;
002:
003: /*
004: * Copyright (C) 2001 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import org.apache.log4j.BasicConfigurator;
022:
023: import java.util.Vector;
024:
025: public class Log4jLoggerFactory implements ILoggerFactory {
026: private ILoggerListener _listenerOfAllLoggers;
027: private Vector<ILoggerListener> _listeners = new Vector<ILoggerListener>();
028:
029: public Log4jLoggerFactory() {
030: this (true);
031: }
032:
033: public void addLoggerListener(ILoggerListener l) {
034: _listeners.add(l);
035: }
036:
037: public void removeLoggerListener(ILoggerListener l) {
038: _listeners.remove(l);
039: }
040:
041: public Log4jLoggerFactory(boolean doBasicConfig) {
042: _listenerOfAllLoggers = new ILoggerListener() {
043: public void info(Class<?> source, Object message) {
044: try {
045: ILoggerListener[] listeners = _listeners
046: .toArray(new ILoggerListener[_listeners
047: .size()]);
048: for (int i = 0; i < listeners.length; i++) {
049: listeners[i].info(source, message);
050: }
051: } catch (Throwable t) {
052: // No exceptions during logging
053: }
054: }
055:
056: public void info(Class<?> source, Object message,
057: Throwable th) {
058: try {
059: ILoggerListener[] listeners = _listeners
060: .toArray(new ILoggerListener[_listeners
061: .size()]);
062: for (int i = 0; i < listeners.length; i++) {
063: listeners[i].info(source, message, th);
064: }
065: } catch (Throwable t) {
066: // No exceptions during logging
067: }
068: }
069:
070: public void warn(Class<?> source, Object message) {
071: try {
072: ILoggerListener[] listeners = _listeners
073: .toArray(new ILoggerListener[_listeners
074: .size()]);
075: for (int i = 0; i < listeners.length; i++) {
076: listeners[i].warn(source, message);
077: }
078: } catch (Throwable t) {
079: // No exceptions during logging
080: }
081: }
082:
083: public void warn(Class<?> source, Object message,
084: Throwable th) {
085: try {
086: ILoggerListener[] listeners = _listeners
087: .toArray(new ILoggerListener[_listeners
088: .size()]);
089: for (int i = 0; i < listeners.length; i++) {
090: listeners[i].warn(source, message, th);
091: }
092: } catch (Throwable t) {
093: // No exceptions during logging
094: }
095: }
096:
097: public void error(Class<?> source, Object message) {
098: try {
099: ILoggerListener[] listeners = _listeners
100: .toArray(new ILoggerListener[_listeners
101: .size()]);
102: for (int i = 0; i < listeners.length; i++) {
103: listeners[i].error(source, message);
104: }
105: } catch (Throwable t) {
106: // No exceptions during logging
107: }
108: }
109:
110: public void error(Class<?> source, Object message,
111: Throwable th) {
112: try {
113: ILoggerListener[] listeners = _listeners
114: .toArray(new ILoggerListener[_listeners
115: .size()]);
116: for (int i = 0; i < listeners.length; i++) {
117: listeners[i].error(source, message, th);
118: }
119: } catch (Throwable t) {
120: // No exceptions during logging
121: }
122: }
123: };
124:
125: if (doBasicConfig) {
126: BasicConfigurator.configure();
127: }
128: }
129:
130: public ILogger createLogger(Class<?> clazz) {
131: return new Log4jLogger(clazz, _listenerOfAllLoggers);
132: }
133:
134: public void shutdown() {
135: }
136: }
|