001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.log;
016:
017: import java.lang.reflect.Method;
018:
019: import org.mortbay.util.Loader;
020:
021: /*-----------------------------------------------------------------------*/
022: /** Logging.
023: * This class provides a static logging interface. If an instance of the
024: * org.slf4j.Logger class is found on the classpath, the static log methods
025: * are directed to a slf4j logger for "org.mortbay.log". Otherwise the logs
026: * are directed to stderr.
027: *
028: * If the system property VERBOSE is set, then ignored exceptions are logged in detail.
029: *
030: */
031: public class Log {
032: private static final String[] __nestedEx = { "getTargetException",
033: "getTargetError", "getException", "getRootCause" };
034: /*-------------------------------------------------------------------*/
035: private static final Class[] __noArgs = new Class[0];
036: public final static String EXCEPTION = "EXCEPTION ";
037: public final static String IGNORED = "IGNORED";
038: public final static String IGNORED_FMT = "IGNORED: {}";
039: public final static String NOT_IMPLEMENTED = "NOT IMPLEMENTED ";
040:
041: private static String logClass = System.getProperty(
042: "org.mortbay.log.class", "org.mortbay.log.Slf4jLog");
043: private static boolean verbose = System
044: .getProperty("VERBOSE", null) != null;
045: private static Logger log;
046:
047: static {
048: Class log_class = null;
049: try {
050: log_class = Loader.loadClass(Log.class, logClass);
051: log = (Logger) log_class.newInstance();
052: } catch (Exception e) {
053: log_class = StdErrLog.class;
054: log = new StdErrLog();
055: if (verbose)
056: e.printStackTrace();
057: }
058:
059: log.info("Logging to {} via {}", log, log_class.getName());
060: }
061:
062: public static void setLog(Logger log) {
063: Log.log = log;
064: }
065:
066: public static Logger getLog() {
067: return log;
068: }
069:
070: public static void debug(Throwable th) {
071: if (log == null)
072: return;
073: log.debug(EXCEPTION, th);
074: unwind(th);
075: }
076:
077: public static void debug(String msg) {
078: if (log == null)
079: return;
080: log.debug(msg, null, null);
081: }
082:
083: public static void debug(String msg, Object arg) {
084: if (log == null)
085: return;
086: log.debug(msg, arg, null);
087: }
088:
089: public static void debug(String msg, Object arg0, Object arg1) {
090: if (log == null)
091: return;
092: log.debug(msg, arg0, arg1);
093: }
094:
095: /* ------------------------------------------------------------ */
096: /**
097: * Ignore an exception unless trace is enabled.
098: * This works around the problem that log4j does not support the trace level.
099: */
100: public static void ignore(Throwable th) {
101: if (log == null)
102: return;
103: if (verbose) {
104: log.debug(IGNORED, th);
105: unwind(th);
106: }
107: }
108:
109: public static void info(String msg) {
110: if (log == null)
111: return;
112: log.info(msg, null, null);
113: }
114:
115: public static void info(String msg, Object arg) {
116: if (log == null)
117: return;
118: log.info(msg, arg, null);
119: }
120:
121: public static void info(String msg, Object arg0, Object arg1) {
122: if (log == null)
123: return;
124: log.info(msg, arg0, arg1);
125: }
126:
127: public static boolean isDebugEnabled() {
128: if (log == null)
129: return false;
130: return log.isDebugEnabled();
131: }
132:
133: public static void warn(String msg) {
134: if (log == null)
135: return;
136: log.warn(msg, null, null);
137: }
138:
139: public static void warn(String msg, Object arg) {
140: if (log == null)
141: return;
142: log.warn(msg, arg, null);
143: }
144:
145: public static void warn(String msg, Object arg0, Object arg1) {
146: if (log == null)
147: return;
148: log.warn(msg, arg0, arg1);
149: }
150:
151: public static void warn(String msg, Throwable th) {
152: if (log == null)
153: return;
154: log.warn(msg, th);
155: unwind(th);
156: }
157:
158: public static void warn(Throwable th) {
159: if (log == null)
160: return;
161: log.warn(EXCEPTION, th);
162: unwind(th);
163: }
164:
165: /** Obtain a named Logger.
166: * Obtain a named Logger or the default Logger if null is passed.
167: */
168: public static Logger getLogger(String name) {
169: if (log == null)
170: return log;
171: if (name == null)
172: return log;
173: return log.getLogger(name);
174: }
175:
176: private static void unwind(Throwable th) {
177: if (th == null)
178: return;
179: for (int i = 0; i < __nestedEx.length; i++) {
180: try {
181: Method get_target = th.getClass().getMethod(
182: __nestedEx[i], __noArgs);
183: Throwable th2 = (Throwable) get_target.invoke(th, null);
184: if (th2 != null)
185: warn("Nested in " + th + ":", th2);
186: } catch (Exception ignore) {
187: }
188: }
189: }
190:
191: }
|