KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > ServletLogger


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.excalibur.logger;
20
21 import org.apache.avalon.framework.logger.Logger;
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24
25 /**
26  * Logger to bootstrap avalon application iside a servlet.
27  * Intended to be used as a logger for Fortress
28  * ContextManager/ContainerManager.
29  *
30  * Adapted from ConsoleLogger.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version CVS $Revision: 1.5 $ $Date: 2004/03/10 13:54:50 $
34  */

35
36 public class ServletLogger implements Logger
37 {
38     /** Typecode for debugging messages. */
39     public static final int LEVEL_DEBUG = 0;
40
41     /** Typecode for informational messages. */
42     public static final int LEVEL_INFO = 1;
43
44     /** Typecode for warning messages. */
45     public static final int LEVEL_WARN = 2;
46
47     /** Typecode for error messages. */
48     public static final int LEVEL_ERROR = 3;
49
50     /** Typecode for fatal error messages. */
51     public static final int LEVEL_FATAL = 4;
52
53     /** Typecode for disabled log levels. */
54     public static final int LEVEL_DISABLED = 5;
55
56     private final ServletContext JavaDoc m_servletContext;
57     private final int m_logLevel;
58     private final String JavaDoc m_prompt;
59
60     /**
61      * Creates a new ServletLogger with the priority set to DEBUG.
62      */

63     public ServletLogger( final ServletConfig JavaDoc servletConfig )
64     {
65         this( servletConfig, LEVEL_DEBUG );
66     }
67
68     /** Helper method to write the constructors. */
69     private void checkState()
70     {
71         if ( m_servletContext == null )
72         {
73             throw new NullPointerException JavaDoc( "servletContext" );
74         }
75         if ( m_logLevel < LEVEL_DEBUG || m_logLevel > LEVEL_DISABLED )
76         {
77             throw new IllegalArgumentException JavaDoc( "Bad logLevel: " + m_logLevel );
78         }
79     }
80
81     /**
82      * Creates a new ServletLogger.
83      * @param servletContext ServletContext to log messages to
84      * @param prompt text to prepend to every message
85      * @param logLevel log level typecode
86      */

87     public ServletLogger( final ServletContext JavaDoc servletContext, final String JavaDoc prompt,
88             final int logLevel )
89     {
90         m_servletContext = servletContext;
91         m_logLevel = logLevel;
92         checkState();
93         m_prompt = prompt;
94     }
95
96     /**
97      * Creates a new ServletLogger.
98      * @param servletConfig the servletConfig to extract ServletContext from;
99      * also the servlet name is extracted to be prepended to every message.
100      * @param logLevel log level typecode
101      */

102     public ServletLogger( final ServletConfig JavaDoc servletConfig, final int logLevel )
103     {
104         m_servletContext = servletConfig.getServletContext();
105         m_logLevel = logLevel;
106         checkState();
107
108         final String JavaDoc servletName = servletConfig.getServletName();
109
110         if ( servletName == null || "".equals( servletName ) )
111         {
112             m_prompt = "unknown: ";
113         }
114         else
115         {
116             m_prompt = servletName + ": ";
117         }
118     }
119
120     /**
121      * Logs a debugging message.
122      *
123      * @param message a <code>String</code> value
124      */

125     public void debug( final String JavaDoc message )
126     {
127         debug( message, null );
128     }
129
130     /**
131      * Logs a debugging message and an exception.
132      *
133      * @param message a <code>String</code> value
134      * @param throwable a <code>Throwable</code> value
135      */

136     public void debug( final String JavaDoc message, final Throwable JavaDoc throwable )
137     {
138         if( m_logLevel <= LEVEL_DEBUG )
139         {
140             m_servletContext.log( m_prompt + "[DEBUG] " + message, throwable );
141         }
142     }
143
144     /**
145      * Returns <code>true</code> if debug-level logging is enabled, false otherwise.
146      *
147      * @return <code>true</code> if debug-level logging
148      */

149     public boolean isDebugEnabled()
150     {
151         return m_logLevel <= LEVEL_DEBUG;
152     }
153
154     /**
155      * Logs an informational message.
156      *
157      * @param message a <code>String</code> value
158      */

159     public void info( final String JavaDoc message )
160     {
161         info( message, null );
162     }
163
164     /**
165      * Logs an informational message and an exception.
166      *
167      * @param message a <code>String</code> value
168      * @param throwable a <code>Throwable</code> value
169      */

170     public void info( final String JavaDoc message, final Throwable JavaDoc throwable )
171     {
172         if( m_logLevel <= LEVEL_INFO )
173         {
174             m_servletContext.log( m_prompt + "[INFO] " + message, throwable );
175         }
176     }
177
178     /**
179      * Returns <code>true</code> if info-level logging is enabled, false otherwise.
180      *
181      * @return <code>true</code> if info-level logging is enabled
182      */

183     public boolean isInfoEnabled()
184     {
185         return m_logLevel <= LEVEL_INFO;
186     }
187
188     /**
189      * Logs a warning message.
190      *
191      * @param message a <code>String</code> value
192      */

193     public void warn( final String JavaDoc message )
194     {
195         warn( message, null );
196     }
197
198     /**
199      * Logs a warning message and an exception.
200      *
201      * @param message a <code>String</code> value
202      * @param throwable a <code>Throwable</code> value
203      */

204     public void warn( final String JavaDoc message, final Throwable JavaDoc throwable )
205     {
206         if( m_logLevel <= LEVEL_WARN )
207         {
208             m_servletContext.log( m_prompt + "[WARNING] " + message, throwable );
209         }
210     }
211
212     /**
213      * Returns <code>true</code> if warn-level logging is enabled, false otherwise.
214      *
215      * @return <code>true</code> if warn-level logging is enabled
216      */

217     public boolean isWarnEnabled()
218     {
219         return m_logLevel <= LEVEL_WARN;
220     }
221
222     /**
223      * Logs an error message.
224      *
225      * @param message a <code>String</code> value
226      */

227     public void error( final String JavaDoc message )
228     {
229         error( message, null );
230     }
231
232     /**
233      * Logs an error message and an exception.
234      *
235      * @param message a <code>String</code> value
236      * @param throwable a <code>Throwable</code> value
237      */

238     public void error( final String JavaDoc message, final Throwable JavaDoc throwable )
239     {
240         if( m_logLevel <= LEVEL_ERROR )
241         {
242             m_servletContext.log( m_prompt + "[ERROR] " + message, throwable );
243         }
244     }
245
246     /**
247      * Returns <code>true</code> if error-level logging is enabled, false otherwise.
248      *
249      * @return <code>true</code> if error-level logging is enabled
250      */

251     public boolean isErrorEnabled()
252     {
253         return m_logLevel <= LEVEL_ERROR;
254     }
255
256     /**
257      * Logs a fatal error message.
258      *
259      * @param message a <code>String</code> value
260      */

261     public void fatalError( final String JavaDoc message )
262     {
263         fatalError( message, null );
264     }
265
266     /**
267      * Logs a fatal error message and an exception.
268      *
269      * @param message a <code>String</code> value
270      * @param throwable a <code>Throwable</code> value
271      */

272     public void fatalError( final String JavaDoc message, final Throwable JavaDoc throwable )
273     {
274         if( m_logLevel <= LEVEL_FATAL )
275         {
276             m_servletContext.log( m_prompt + "[FATAL ERROR] " + message, throwable );
277         }
278     }
279
280     /**
281      * Returns <code>true</code> if fatal-level logging is enabled, false otherwise.
282      *
283      * @return <code>true</code> if fatal-level logging is enabled
284      */

285     public boolean isFatalErrorEnabled()
286     {
287         return m_logLevel <= LEVEL_FATAL;
288     }
289
290     /**
291      * Just returns this logger (<code>ServletLogger</code> is not hierarchical).
292      *
293      * @param name ignored
294      * @return this logger
295      */

296     public Logger getChildLogger( final String JavaDoc name )
297     {
298         return this;
299     }
300 }
301
Popular Tags