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 javax.servlet.http.HttpServlet;
019:
020: /**
021: * An implementation of LoggingOutput that sends stuff to the Servlet.log
022: * stream.
023: * @author Joe Walker [joe at getahead dot ltd dot uk]
024: */
025: public class ServletLoggingOutput implements LoggingOutput {
026: /**
027: * @param base All LoggingOutput must have a constructor like this
028: */
029: public ServletLoggingOutput(Class<?> base) {
030: }
031:
032: /* (non-Javadoc)
033: * @see org.directwebremoting.util.LoggingOutput#debug(java.lang.String)
034: */
035: public void debug(String message) {
036: log(LEVEL_DEBUG, message, null);
037: }
038:
039: /* (non-Javadoc)
040: * @see org.directwebremoting.util.LoggingOutput#info(java.lang.String)
041: */
042: public void info(String message) {
043: log(LEVEL_INFO, message, null);
044: }
045:
046: /* (non-Javadoc)
047: * @see org.directwebremoting.util.LoggingOutput#warn(java.lang.String)
048: */
049: public void warn(String message) {
050: log(LEVEL_WARN, message, null);
051: }
052:
053: /* (non-Javadoc)
054: * @see org.directwebremoting.util.LoggingOutput#warn(java.lang.String, java.lang.Throwable)
055: */
056: public void warn(String message, Throwable th) {
057: log(LEVEL_WARN, message, th);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.directwebremoting.util.LoggingOutput#error(java.lang.String)
062: */
063: public void error(String message) {
064: log(LEVEL_ERROR, message, null);
065: }
066:
067: /* (non-Javadoc)
068: * @see org.directwebremoting.util.LoggingOutput#error(java.lang.String, java.lang.Throwable)
069: */
070: public void error(String message, Throwable th) {
071: log(LEVEL_ERROR, message, th);
072: }
073:
074: /* (non-Javadoc)
075: * @see org.directwebremoting.util.LoggingOutput#fatal(java.lang.String)
076: */
077: public void fatal(String message) {
078: log(LEVEL_FATAL, message, null);
079: }
080:
081: /* (non-Javadoc)
082: * @see org.directwebremoting.util.LoggingOutput#fatal(java.lang.String, java.lang.Throwable)
083: */
084: public void fatal(String message, Throwable th) {
085: log(LEVEL_FATAL, message, th);
086: }
087:
088: /**
089: * Internal log implementation.
090: * @param loglevel The level to log at
091: * @param message The (optional) message to log
092: * @param th The (optional) exception
093: */
094: private static void log(int loglevel, String message, Throwable th) {
095: if (loglevel >= level) {
096: HttpServlet servlet = servlets.get();
097: if (servlet != null) {
098: // Tomcat 4 NPEs is th is null
099: if (th == null) {
100: servlet.log(message);
101: } else {
102: servlet.log(message, th);
103: }
104: } else {
105: if (message != null) {
106: System.out.println(message);
107: }
108:
109: if (th != null) {
110: th.printStackTrace();
111: }
112: }
113: }
114: }
115:
116: /**
117: * Associate a servlet with this thread for logging purposes.
118: * @param servlet The servlet to use for logging in this thread
119: */
120: public static void setExecutionContext(HttpServlet servlet) {
121: servlets.set(servlet);
122: }
123:
124: /**
125: * Remove the servlet from this thread for logging purposes
126: */
127: public static void unsetExecutionContext() {
128: servlets.set(null);
129: }
130:
131: /**
132: * String version of setLevel.
133: * @param logLevel One of FATAL, ERROR, WARN, INFO, DEBUG
134: */
135: public static void setLevel(String logLevel) {
136: if ("FATAL".equalsIgnoreCase(logLevel)) {
137: setLevel(LEVEL_FATAL);
138: } else if ("ERROR".equalsIgnoreCase(logLevel)) {
139: setLevel(LEVEL_ERROR);
140: } else if ("WARN".equalsIgnoreCase(logLevel)) {
141: setLevel(LEVEL_WARN);
142: } else if ("INFO".equalsIgnoreCase(logLevel)) {
143: setLevel(LEVEL_INFO);
144: } else if ("DEBUG".equalsIgnoreCase(logLevel)) {
145: setLevel(LEVEL_DEBUG);
146: } else {
147: throw new IllegalArgumentException("Unknown log level: "
148: + logLevel);
149: }
150: }
151:
152: /* (non-Javadoc)
153: * @see org.directwebremoting.util.LoggingOutput#isDebugEnabled()
154: */
155: public boolean isDebugEnabled() {
156: return level == LEVEL_DEBUG;
157: }
158:
159: /**
160: * @param level The logging level to set.
161: */
162: public static void setLevel(int level) {
163: ServletLoggingOutput.level = level;
164: }
165:
166: /**
167: * @return Returns the logging level.
168: */
169: public static int getLevel() {
170: return level;
171: }
172:
173: /**
174: * The container for all known threads
175: */
176: private static final ThreadLocal<HttpServlet> servlets = new ThreadLocal<HttpServlet>();
177:
178: /**
179: * What is the current debug level?
180: */
181: private static int level = LEVEL_WARN;
182: }
|