001: /*
002: * Copyright 2005 Joe Walker
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: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.util;
017:
018: import java.lang.reflect.Constructor;
019:
020: /**
021: * A very quick and dirty logging implementation.
022: * <code>java.util.logging</code> is out because we work with JDK 1.3 and we
023: * don't want to force users to import log4j or commons-logging.
024: * Don't use this outside of DWR - it's just a quick hack to keep things simple.
025: * @author Joe Walker [joe at getahead dot ltd dot uk]
026: */
027: public final class Logger {
028: /**
029: * @param base The class to log against
030: * @return A new logger
031: */
032: public static Logger getLogger(Class<?> base) {
033: return new Logger(base);
034: }
035:
036: /**
037: * @param defaultImplementation the defaultImplementation to set
038: */
039: public static void setDefaultImplementation(
040: Class<? extends LoggingOutput> defaultImplementation) {
041: Logger.defaultImplementation = defaultImplementation;
042: }
043:
044: /**
045: * Prevent instantiation
046: * @param base The class to log against
047: */
048: @SuppressWarnings("unchecked")
049: private Logger(Class<?> base) {
050: if (!defaultTried) {
051: try {
052: constructor = defaultImplementation
053: .getConstructor(Class.class);
054: LoggingOutput internal = constructor
055: .newInstance(Logger.class);
056: internal.debug("Logging using "
057: + defaultImplementation.getSimpleName());
058: defaultAvailable = true;
059: } catch (Throwable ex) {
060: LoggingOutput internal = new ServletLoggingOutput(base);
061: internal.debug("Logging using servlet.log.");
062: defaultAvailable = false;
063: }
064:
065: defaultTried = true;
066: }
067:
068: if (defaultAvailable) {
069: try {
070: output = constructor.newInstance(base);
071: } catch (Exception ex) {
072: ex.printStackTrace();
073: output = new ServletLoggingOutput(base);
074: }
075: } else {
076: output = new ServletLoggingOutput(base);
077: }
078: }
079:
080: private static Class<? extends LoggingOutput> defaultImplementation = CommonsLoggingOutput.class;
081:
082: private static Constructor<? extends LoggingOutput> constructor;
083:
084: private static boolean defaultTried = false;
085:
086: private static boolean defaultAvailable = false;
087:
088: /**
089: * The logging implementation
090: */
091: private LoggingOutput output;
092:
093: /**
094: * Logger a debug message
095: * @param message The text to log
096: */
097: public void debug(String message) {
098: output.debug(message);
099: }
100:
101: /**
102: * Logger an info message
103: * @param message The text to log
104: */
105: public void info(String message) {
106: output.info(message);
107: }
108:
109: /**
110: * Logger a warning message
111: * @param message The text to log
112: */
113: public void warn(String message) {
114: output.warn(message);
115: }
116:
117: /**
118: * Logger a warning message
119: * @param message The text to log
120: * @param th An optional stack trace
121: */
122: public void warn(String message, Throwable th) {
123: output.warn(message, th);
124: }
125:
126: /**
127: * Logger an error message
128: * @param message The text to log
129: */
130: public void error(String message) {
131: output.error(message);
132: }
133:
134: /**
135: * Logger an error message
136: * @param message The text to log
137: * @param th An optional stack trace
138: */
139: public void error(String message, Throwable th) {
140: output.error(message, th);
141: }
142:
143: /**
144: * Logger a fatal error message
145: * @param message The text to log
146: */
147: public void fatal(String message) {
148: output.fatal(message);
149: }
150:
151: /**
152: * Logger a fatal error message
153: * @param message The text to log
154: * @param th An optional stack trace
155: */
156: public void fatal(String message, Throwable th) {
157: output.fatal(message, th);
158: }
159:
160: /**
161: * Save CPU time when we are not debugging
162: * @return true if debugging is enabled
163: */
164: public boolean isDebugEnabled() {
165: return output.isDebugEnabled();
166: }
167: }
|