001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.util.logging;
020:
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023: import java.lang.reflect.Method;
024:
025: import org.apache.commons.logging.Log;
026:
027: /**
028: * <p>
029: * Logging abstraction for NetUI. This class leverages Jakarta commons-logging to create
030: * loggers for NetUI messages. Application developers can provide their own logger
031: * implementations by following the instructions for creating new Log / LogFactory instances
032: * in commons-logging.
033: * </p>
034: */
035: public class Logger implements Log {
036:
037: /**
038: * Factory method for creating NetUI Logger instances.
039: *
040: * @param loggerClient the class whose logger to create
041: * @return a {@link Logger} instance for the given class
042: */
043: public static Logger getInstance(Class loggerClient) {
044: return new Logger(org.apache.commons.logging.LogFactory
045: .getLog(loggerClient.getName()));
046: }
047:
048: private Log _logDelegate = null;
049:
050: /**
051: * Constructor that returns a Log4J logger. This method is deprecated
052: * in favor of using commons-logging to do logger creation via the
053: * {@link #getInstance(Class)} method.
054: *
055: * @deprecated
056: * @see #getInstance(Class)
057: * @param clientClass
058: */
059: public Logger(Class clientClass) {
060: _logDelegate = createDefaultLogger(clientClass);
061: }
062:
063: /**
064: * Constructor that returns a Log4J logger. This method is deprecated
065: * in favor of using commons-logging to do logger creation via the
066: * {@link #getInstance(Class)} method.
067: *
068: * @deprecated
069: * @see #getInstance(Class)
070: * @param clientClassName
071: */
072: public Logger(String clientClassName) {
073: Class clientClass = null;
074: try {
075: /* create a default log4j logger -- this shouldn't throw a CNF exception */
076: clientClass = Class.forName(clientClassName);
077: } catch (ClassNotFoundException e) {
078: throw new IllegalArgumentException(
079: "Could not load NetUI logger client class '"
080: + clientClassName + "'");
081: }
082: _logDelegate = createDefaultLogger(clientClass);
083: }
084:
085: /**
086: * Internal method used by the factory to create a Logger instance.
087: *
088: * @param logDelegate the commons-logging {@link Log} to which messages should be logged
089: */
090: private Logger(Log logDelegate) {
091: _logDelegate = logDelegate;
092: }
093:
094: public boolean isDebugEnabled() {
095: return _logDelegate.isDebugEnabled();
096: }
097:
098: public boolean isErrorEnabled() {
099: return _logDelegate.isErrorEnabled();
100: }
101:
102: public boolean isFatalEnabled() {
103: return _logDelegate.isFatalEnabled();
104: }
105:
106: public boolean isInfoEnabled() {
107: return _logDelegate.isInfoEnabled();
108: }
109:
110: public boolean isTraceEnabled() {
111: return _logDelegate.isTraceEnabled();
112: }
113:
114: public boolean isWarnEnabled() {
115: return _logDelegate.isWarnEnabled();
116: }
117:
118: public void debug(Object message) {
119: if (isDebugEnabled())
120: _logDelegate.debug(message);
121: }
122:
123: public void debug(Object message, Throwable t) {
124: if (isDebugEnabled())
125: _logDelegate.debug(format(message, t));
126: }
127:
128: public void trace(Object message) {
129: if (isTraceEnabled())
130: _logDelegate.trace(message);
131: }
132:
133: public void trace(Object message, Throwable t) {
134: if (isTraceEnabled())
135: _logDelegate.trace(format(message, t));
136: }
137:
138: public void info(Object message) {
139: if (isInfoEnabled())
140: _logDelegate.info(message);
141: }
142:
143: public void info(Object message, Throwable t) {
144: if (isInfoEnabled())
145: _logDelegate.info(format(message, t));
146: }
147:
148: public void warn(Object message) {
149: if (isWarnEnabled())
150: _logDelegate.warn(message);
151: }
152:
153: public void warn(Object message, Throwable t) {
154: if (isWarnEnabled())
155: _logDelegate.warn(format(message, t));
156: }
157:
158: public void error(Object message) {
159: if (isErrorEnabled())
160: _logDelegate.error(message);
161: }
162:
163: public void error(Object message, Throwable t) {
164: if (isErrorEnabled())
165: _logDelegate.error(format(message, t));
166: }
167:
168: public void fatal(Object message) {
169: if (isFatalEnabled())
170: _logDelegate.fatal(message);
171: }
172:
173: public void fatal(Object message, Throwable t) {
174: if (isFatalEnabled())
175: _logDelegate.fatal(format(message, t));
176: }
177:
178: private String format(Object m, Throwable t) {
179: if (t == null)
180: return m.toString();
181:
182: StringWriter sw = new StringWriter();
183: t.printStackTrace(new PrintWriter(sw));
184:
185: /* note, no reason to close a StringWriter */
186:
187: return m + "\n\n" + "Throwable: " + t.toString()
188: + "\nStack Trace:\n" + sw.toString();
189: }
190:
191: /**
192: * Internal method used to create the backwards-compat NetUI logger. This method
193: * looks up the {@link org.apache.beehive.netui.util.logging.internal.Log4JLogger}
194: * and creates a new instance returning the resulting {@link Log}.
195: *
196: * @param loggerClient the logger client
197: * @return the {@link Log} instance
198: */
199: private static final Log createDefaultLogger(Class loggerClient) {
200: assert loggerClient != null : "Received a null loggerClient Class";
201:
202: String className = "org.apache.beehive.netui.util.logging.internal.Log4JLogger";
203: try {
204: Class logDelegateClass = Logger.class.getClassLoader()
205: .loadClass(className);
206: Method method = logDelegateClass.getMethod("getInstance",
207: new Class[] { Class.class });
208: return (Log) method.invoke(null,
209: new Object[] { loggerClient });
210: } catch (Exception e) {
211: throw new IllegalStateException(
212: "Could not create log implementation '" + className
213: + "' for client of type '"
214: + loggerClient.getName() + "'", e);
215: }
216: }
217: }
|