001: //========================================================================
002: //$Id: Slf4jLog.java,v 1.1 2005/11/14 16:55:09 gregwilkins Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.log;
017:
018: import java.lang.reflect.Method;
019:
020: class Slf4jLog implements Logger {
021: private static final String LOGGER = "org.slf4j.Logger";
022: private static final String LOGGERFACTORY = "org.slf4j.LoggerFactory";
023: private static final Object[] NO_ARGS = new Object[] {};
024: private Method infoSOO;
025: private Method debugSOO;
026: private Method debugST;
027: private Method debugEnabled;
028: private Method warnSOO;
029: private Method warnST;
030: private Method errorST;
031: private Object logger;
032:
033: Slf4jLog() throws Exception {
034: this ("org.mortbay.log");
035: }
036:
037: Slf4jLog(String name) throws Exception {
038: Class slf4j = null;
039: Class slf4jf = null;
040: try {
041: slf4j = this .getClass().getClassLoader().loadClass(LOGGER);
042: slf4jf = this .getClass().getClassLoader().loadClass(
043: LOGGERFACTORY);
044: } catch (Exception e) {
045: slf4j = Thread.currentThread().getContextClassLoader() == null ? Class
046: .forName(LOGGER)
047: : Thread.currentThread().getContextClassLoader()
048: .loadClass(LOGGER);
049: slf4jf = Thread.currentThread().getContextClassLoader() == null ? Class
050: .forName(LOGGERFACTORY)
051: : Thread.currentThread().getContextClassLoader()
052: .loadClass(LOGGERFACTORY);
053: }
054:
055: infoSOO = slf4j.getMethod("info", new Class[] { String.class,
056: Object.class, Object.class });
057: debugSOO = slf4j.getMethod("debug", new Class[] { String.class,
058: Object.class, Object.class });
059: debugST = slf4j.getMethod("debug", new Class[] { String.class,
060: Throwable.class });
061: debugEnabled = slf4j
062: .getMethod("isDebugEnabled", new Class[] {});
063: warnSOO = slf4j.getMethod("warn", new Class[] { String.class,
064: Object.class, Object.class });
065: warnST = slf4j.getMethod("warn", new Class[] { String.class,
066: Throwable.class });
067: errorST = slf4j.getMethod("error", new Class[] { String.class,
068: Throwable.class });
069:
070: Method getLogger = slf4jf.getMethod("getLogger",
071: new Class[] { String.class });
072: logger = getLogger.invoke(null, new Object[] { name });
073: }
074:
075: /* ------------------------------------------------------------ */
076: /*
077: * @see org.mortbay.log.Log#doDebug(java.lang.String, java.lang.Object, java.lang.Object)
078: */
079: public void debug(String msg, Object arg0, Object arg1) {
080: try {
081: debugSOO.invoke(logger, new Object[] { msg, arg0, arg1 });
082: } catch (Exception e) {
083: e.printStackTrace();
084: }
085: }
086:
087: /* ------------------------------------------------------------ */
088: /*
089: * @see org.mortbay.log.Log#doDebug(java.lang.String, java.lang.Throwable)
090: */
091: public void debug(String msg, Throwable th) {
092: try {
093: debugST.invoke(logger, new Object[] { msg, th });
094: } catch (Exception e) {
095: e.printStackTrace();
096: }
097: }
098:
099: /* ------------------------------------------------------------ */
100: /*
101: * @see org.mortbay.log.Log#doDebugEnabled()
102: */
103: public boolean isDebugEnabled() {
104: try {
105: return ((Boolean) debugEnabled.invoke(logger, NO_ARGS))
106: .booleanValue();
107: } catch (Exception e) {
108: e.printStackTrace();
109: return true;
110: }
111: }
112:
113: /* ------------------------------------------------------------ */
114: /*
115: * @see org.mortbay.log.Log#doInfo(java.lang.String, java.lang.Object, java.lang.Object)
116: */
117: public void info(String msg, Object arg0, Object arg1) {
118: try {
119: infoSOO.invoke(logger, new Object[] { msg, arg0, arg1 });
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124:
125: /* ------------------------------------------------------------ */
126: /*
127: * @see org.mortbay.log.Log#doWarn(java.lang.String, java.lang.Object, java.lang.Object)
128: */
129: public void warn(String msg, Object arg0, Object arg1) {
130: try {
131: warnSOO.invoke(logger, new Object[] { msg, arg0, arg1 });
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135: }
136:
137: /* ------------------------------------------------------------ */
138: /*
139: * @see org.mortbay.log.Log#doWarn(java.lang.String, java.lang.Throwable)
140: */
141: public void warn(String msg, Throwable th) {
142: try {
143: if (th instanceof RuntimeException || th instanceof Error)
144: errorST.invoke(logger, new Object[] { msg, th });
145: else
146: warnST.invoke(logger, new Object[] { msg, th });
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150: }
151:
152: /* ------------------------------------------------------------ */
153: public Logger getLogger(String name) {
154: try {
155: return new Slf4jLog(name);
156: } catch (Exception e) {
157: Log.warn(e);
158: return this ;
159: }
160: }
161:
162: /* ------------------------------------------------------------ */
163: public String toString() {
164: return logger.toString();
165: }
166:
167: /* ------------------------------------------------------------ */
168: public void setDebugEnabled(boolean enabled) {
169: warn("setDebugEnabled not implemented", null, null);
170: }
171: }
|