KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > ServletLoggingOutput


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.util;
17
18 import javax.servlet.http.HttpServlet JavaDoc;
19
20 /**
21  * An implementation of LoggingOutput that sends stuff to the Servlet.log
22  * stream.
23  * @author Joe Walker [joe at getahead dot ltd dot uk]
24  */

25 public class ServletLoggingOutput implements LoggingOutput
26 {
27     /* (non-Javadoc)
28      * @see org.directwebremoting.util.LoggingOutput#debug(java.lang.String)
29      */

30     public void debug(String JavaDoc message)
31     {
32         log(LEVEL_DEBUG, message, null);
33     }
34
35     /* (non-Javadoc)
36      * @see org.directwebremoting.util.LoggingOutput#info(java.lang.String)
37      */

38     public void info(String JavaDoc message)
39     {
40         log(LEVEL_INFO, message, null);
41     }
42
43     /* (non-Javadoc)
44      * @see org.directwebremoting.util.LoggingOutput#warn(java.lang.String)
45      */

46     public void warn(String JavaDoc message)
47     {
48         log(LEVEL_WARN, message, null);
49     }
50
51     /* (non-Javadoc)
52      * @see org.directwebremoting.util.LoggingOutput#warn(java.lang.String, java.lang.Throwable)
53      */

54     public void warn(String JavaDoc message, Throwable JavaDoc th)
55     {
56         log(LEVEL_WARN, message, th);
57     }
58
59     /* (non-Javadoc)
60      * @see org.directwebremoting.util.LoggingOutput#error(java.lang.String)
61      */

62     public void error(String JavaDoc message)
63     {
64         log(LEVEL_ERROR, message, null);
65     }
66
67     /* (non-Javadoc)
68      * @see org.directwebremoting.util.LoggingOutput#error(java.lang.String, java.lang.Throwable)
69      */

70     public void error(String JavaDoc message, Throwable JavaDoc th)
71     {
72         log(LEVEL_ERROR, message, th);
73     }
74
75     /* (non-Javadoc)
76      * @see org.directwebremoting.util.LoggingOutput#fatal(java.lang.String)
77      */

78     public void fatal(String JavaDoc message)
79     {
80         log(LEVEL_FATAL, message, null);
81     }
82
83     /* (non-Javadoc)
84      * @see org.directwebremoting.util.LoggingOutput#fatal(java.lang.String, java.lang.Throwable)
85      */

86     public void fatal(String JavaDoc message, Throwable JavaDoc th)
87     {
88         log(LEVEL_FATAL, message, th);
89     }
90
91     /**
92      * Internal log implementation.
93      * @param loglevel The level to log at
94      * @param message The (optional) message to log
95      * @param th The (optional) exception
96      */

97     private static void log(int loglevel, String JavaDoc message, Throwable JavaDoc th)
98     {
99         if (loglevel >= level)
100         {
101             HttpServlet JavaDoc servlet = (HttpServlet JavaDoc) servlets.get();
102             if (servlet != null)
103             {
104                 // Tomcat 4 NPEs is th is null
105
if (th == null)
106                 {
107                     servlet.log(message);
108                 }
109                 else
110                 {
111                     servlet.log(message, th);
112                 }
113             }
114             else
115             {
116                 if (message != null)
117                 {
118                     System.out.println(message);
119                 }
120
121                 if (th != null)
122                 {
123                     th.printStackTrace();
124                 }
125             }
126         }
127     }
128
129     /**
130      * Associate a servlet with this thread for logging purposes.
131      * @param servlet The servlet to use for logging in this thread
132      */

133     public static void setExecutionContext(HttpServlet JavaDoc servlet)
134     {
135         servlets.set(servlet);
136     }
137
138     /**
139      * Remove the servlet from this thread for logging purposes
140      */

141     public static void unsetExecutionContext()
142     {
143         servlets.set(null);
144     }
145
146     /**
147      * String version of setLevel.
148      * @param logLevel One of FATAL, ERROR, WARN, INFO, DEBUG
149      */

150     public static void setLevel(String JavaDoc logLevel)
151     {
152         if (logLevel.equalsIgnoreCase("FATAL"))
153         {
154             setLevel(LEVEL_FATAL);
155         }
156         else if (logLevel.equalsIgnoreCase("ERROR"))
157         {
158             setLevel(LEVEL_ERROR);
159         }
160         else if (logLevel.equalsIgnoreCase("WARN"))
161         {
162             setLevel(LEVEL_WARN);
163         }
164         else if (logLevel.equalsIgnoreCase("INFO"))
165         {
166             setLevel(LEVEL_INFO);
167         }
168         else if (logLevel.equalsIgnoreCase("DEBUG"))
169         {
170             setLevel(LEVEL_DEBUG);
171         }
172         else
173         {
174             throw new IllegalArgumentException JavaDoc("Unknown log level: " + logLevel);
175         }
176     }
177
178     /* (non-Javadoc)
179      * @see org.directwebremoting.util.LoggingOutput#isDebugEnabled()
180      */

181     public boolean isDebugEnabled()
182     {
183         return level == LEVEL_DEBUG;
184     }
185
186     /**
187      * @param level The logging level to set.
188      */

189     public static void setLevel(int level)
190     {
191         ServletLoggingOutput.level = level;
192     }
193
194     /**
195      * @return Returns the logging level.
196      */

197     public static int getLevel()
198     {
199         return level;
200     }
201
202     /**
203      * The container for all known threads
204      */

205     private static final ThreadLocal JavaDoc servlets = new ThreadLocal JavaDoc();
206
207     /**
208      * What is the current debug level?
209      */

210     private static int level = LEVEL_WARN;
211 }
212
Popular Tags