001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software 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
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.log.log4j;
024:
025: import java.text.*;
026: import java.util.*;
027:
028: import com.mchange.v2.log.*;
029: import com.mchange.v2.util.DoubleWeakHashMap;
030:
031: import org.apache.log4j.*;
032:
033: public final class Log4jMLog extends MLog {
034: final static String CHECK_CLASS = "org.apache.log4j.Logger";
035:
036: MLogger global = null;
037:
038: public Log4jMLog() throws ClassNotFoundException {
039: Class.forName(CHECK_CLASS);
040: }
041:
042: public MLogger getMLogger(String name) {
043: Logger lg = Logger.getLogger(name);
044: return new Log4jMLogger(lg);
045: }
046:
047: public MLogger getMLogger(Class cl) {
048: Logger lg = Logger.getLogger(cl);
049: return new Log4jMLogger(lg);
050: }
051:
052: public MLogger getMLogger() {
053: Logger lg = Logger.getRootLogger();
054: return new Log4jMLogger(lg);
055: }
056:
057: private final static class Log4jMLogger implements MLogger {
058: final static String FQCN = Log4jMLogger.class.getName();
059:
060: // protected by this' lock
061: MLevel myLevel = null;
062:
063: volatile Logger logger;
064:
065: Log4jMLogger(Logger logger) {
066: this .logger = logger;
067: }
068:
069: private static MLevel guessMLevel(Level lvl) {
070: if (lvl == null)
071: return null;
072: else if (lvl == Level.ALL)
073: return MLevel.ALL;
074: else if (lvl == Level.DEBUG)
075: return MLevel.FINEST;
076: else if (lvl == Level.ERROR)
077: return MLevel.SEVERE;
078: else if (lvl == Level.FATAL)
079: return MLevel.SEVERE;
080: else if (lvl == Level.INFO)
081: return MLevel.INFO;
082: else if (lvl == Level.OFF)
083: return MLevel.OFF;
084: else if (lvl == Level.WARN)
085: return MLevel.WARNING;
086: else
087: throw new IllegalArgumentException("Unknown level: "
088: + lvl);
089: }
090:
091: private static Level level(MLevel lvl) {
092: if (lvl == null)
093: return null;
094: else if (lvl == MLevel.ALL)
095: return Level.ALL;
096: else if (lvl == MLevel.CONFIG)
097: return Level.DEBUG;
098: else if (lvl == MLevel.FINE)
099: return Level.DEBUG;
100: else if (lvl == MLevel.FINER)
101: return Level.DEBUG;
102: else if (lvl == MLevel.FINEST)
103: return Level.DEBUG;
104: else if (lvl == MLevel.INFO)
105: return Level.INFO;
106: else if (lvl == MLevel.INFO)
107: return Level.OFF;
108: else if (lvl == MLevel.SEVERE)
109: return Level.ERROR;
110: else if (lvl == MLevel.WARNING)
111: return Level.WARN;
112: else
113: throw new IllegalArgumentException("Unknown MLevel: "
114: + lvl);
115: }
116:
117: private static String createMessage(String srcClass,
118: String srcMeth, String msg) {
119: StringBuffer sb = new StringBuffer(511);
120: sb.append("[class: ");
121: sb.append(srcClass);
122: sb.append("; method: ");
123: sb.append(srcMeth);
124: if (!srcMeth.endsWith(")"))
125: sb.append("()");
126: sb.append("] ");
127: sb.append(msg);
128: return sb.toString();
129: }
130:
131: private static String createMessage(String srcMeth, String msg) {
132: StringBuffer sb = new StringBuffer(511);
133: sb.append("[method: ");
134: sb.append(srcMeth);
135: if (!srcMeth.endsWith(")"))
136: sb.append("()");
137: sb.append("] ");
138: sb.append(msg);
139: return sb.toString();
140: }
141:
142: public ResourceBundle getResourceBundle() {
143: return null;
144: }
145:
146: public String getResourceBundleName() {
147: return null;
148: }
149:
150: public void setFilter(Object java14Filter)
151: throws SecurityException {
152: warning("setFilter() not supported by MLogger "
153: + this .getClass().getName());
154: }
155:
156: public Object getFilter() {
157: return null;
158: }
159:
160: private void log(Level lvl, Object msg, Throwable t) {
161: logger.log(FQCN, lvl, msg, t);
162: }
163:
164: public void log(MLevel l, String msg) {
165: log(level(l), msg, null);
166: }
167:
168: public void log(MLevel l, String msg, Object param) {
169: log(level(l), (msg != null ? MessageFormat.format(msg,
170: new Object[] { param }) : null), null);
171: }
172:
173: public void log(MLevel l, String msg, Object[] params) {
174: log(level(l), (msg != null ? MessageFormat.format(msg,
175: params) : null), null);
176: }
177:
178: public void log(MLevel l, String msg, Throwable t) {
179: log(level(l), msg, t);
180: }
181:
182: public void logp(MLevel l, String srcClass, String srcMeth,
183: String msg) {
184: log(level(l), createMessage(srcClass, srcMeth, msg), null);
185: }
186:
187: public void logp(MLevel l, String srcClass, String srcMeth,
188: String msg, Object param) {
189: log(level(l), createMessage(srcClass, srcMeth,
190: (msg != null ? MessageFormat.format(msg,
191: new Object[] { param }) : null)), null);
192: }
193:
194: public void logp(MLevel l, String srcClass, String srcMeth,
195: String msg, Object[] params) {
196: log(level(l), createMessage(srcClass, srcMeth,
197: (msg != null ? MessageFormat.format(msg, params)
198: : null)), null);
199: }
200:
201: public void logp(MLevel l, String srcClass, String srcMeth,
202: String msg, Throwable t) {
203: log(level(l), createMessage(srcClass, srcMeth, msg), t);
204: }
205:
206: public void logrb(MLevel l, String srcClass, String srcMeth,
207: String rb, String msg) {
208: log(level(l), createMessage(srcClass, srcMeth,
209: formatMessage(rb, msg, null)), null);
210: }
211:
212: public void logrb(MLevel l, String srcClass, String srcMeth,
213: String rb, String msg, Object param) {
214: log(level(l), createMessage(srcClass, srcMeth,
215: formatMessage(rb, msg, new Object[] { param })),
216: null);
217: }
218:
219: public void logrb(MLevel l, String srcClass, String srcMeth,
220: String rb, String msg, Object[] params) {
221: log(level(l), createMessage(srcClass, srcMeth,
222: formatMessage(rb, msg, params)), null);
223: }
224:
225: public void logrb(MLevel l, String srcClass, String srcMeth,
226: String rb, String msg, Throwable t) {
227: log(level(l), createMessage(srcClass, srcMeth,
228: formatMessage(rb, msg, null)), t);
229: }
230:
231: public void entering(String srcClass, String srcMeth) {
232: log(Level.DEBUG, createMessage(srcClass, srcMeth,
233: "entering method."), null);
234: }
235:
236: public void entering(String srcClass, String srcMeth,
237: Object param) {
238: log(Level.DEBUG, createMessage(srcClass, srcMeth,
239: "entering method... param: " + param.toString()),
240: null);
241: }
242:
243: public void entering(String srcClass, String srcMeth,
244: Object params[]) {
245: log(Level.DEBUG, createMessage(srcClass, srcMeth,
246: "entering method... "
247: + LogUtils.createParamsList(params)), null);
248: }
249:
250: public void exiting(String srcClass, String srcMeth) {
251: log(Level.DEBUG, createMessage(srcClass, srcMeth,
252: "exiting method."), null);
253: }
254:
255: public void exiting(String srcClass, String srcMeth,
256: Object result) {
257: log(Level.DEBUG, createMessage(srcClass, srcMeth,
258: "exiting method... result: " + result.toString()),
259: null);
260: }
261:
262: public void throwing(String srcClass, String srcMeth,
263: Throwable t) {
264: log(Level.DEBUG, createMessage(srcClass, srcMeth,
265: "throwing exception... "), t);
266: }
267:
268: public void severe(String msg) {
269: log(Level.ERROR, msg, null);
270: }
271:
272: public void warning(String msg) {
273: log(Level.WARN, msg, null);
274: }
275:
276: public void info(String msg) {
277: log(Level.INFO, msg, null);
278: }
279:
280: public void config(String msg) {
281: log(Level.DEBUG, msg, null);
282: }
283:
284: public void fine(String msg) {
285: log(Level.DEBUG, msg, null);
286: }
287:
288: public void finer(String msg) {
289: log(Level.DEBUG, msg, null);
290: }
291:
292: public void finest(String msg) {
293: log(Level.DEBUG, msg, null);
294: }
295:
296: public synchronized void setLevel(MLevel l)
297: throws SecurityException {
298: logger.setLevel(level(l));
299: myLevel = l;
300: }
301:
302: public synchronized MLevel getLevel() {
303: //System.err.println( logger.getLevel() );
304: if (myLevel == null)
305: myLevel = guessMLevel(logger.getLevel());
306: return myLevel;
307: }
308:
309: public boolean isLoggable(MLevel l) {
310: //System.err.println( "MLevel: " + l + "; isEnabledFor(): " + logger.isEnabledFor( level(l) ) + "; getLevel(): " + getLevel() +
311: //"; MLog.getLogger().getLevel(): " + MLog.getLogger().getLevel());
312: //new Exception("WHADDAFUC").printStackTrace();
313: return logger.isEnabledFor(level(l));
314: }
315:
316: public String getName() {
317: return logger.getName();
318: }
319:
320: public void addHandler(Object h) throws SecurityException {
321: if (!(h instanceof Appender))
322: throw new IllegalArgumentException("The 'handler' " + h
323: + " is not compatible with MLogger " + this );
324: logger.addAppender((Appender) h);
325: }
326:
327: public void removeHandler(Object h) throws SecurityException {
328: if (!(h instanceof Appender))
329: throw new IllegalArgumentException("The 'handler' " + h
330: + " is not compatible with MLogger " + this );
331: logger.removeAppender((Appender) h);
332: }
333:
334: public Object[] getHandlers() {
335: List tmp = new LinkedList();
336: for (Enumeration e = logger.getAllAppenders(); e
337: .hasMoreElements();)
338: tmp.add(e.nextElement());
339: return tmp.toArray();
340: }
341:
342: public void setUseParentHandlers(boolean uph) {
343: logger.setAdditivity(uph);
344: }
345:
346: public boolean getUseParentHandlers() {
347: return logger.getAdditivity();
348: }
349: }
350:
351: private static String formatMessage(String rbname, String msg,
352: Object[] params) {
353: if (msg == null) {
354: if (params == null)
355: return "";
356: else
357: return LogUtils.createParamsList(params);
358: } else {
359: ResourceBundle rb = ResourceBundle.getBundle(rbname);
360: if (rb != null) {
361: String check = rb.getString(msg);
362: if (check != null)
363: msg = check;
364: }
365: return (params == null ? msg : MessageFormat.format(msg,
366: params));
367: }
368: }
369: }
|