KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > servlet > ServletLogger


1 /*
2  * Copyright 2003 The Apache Software Foundation.
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
17 package org.apache.velocity.tools.view.servlet;
18
19
20 import javax.servlet.ServletContext JavaDoc;
21
22 import org.apache.velocity.runtime.log.LogSystem;
23 import org.apache.velocity.runtime.RuntimeConstants;
24 import org.apache.velocity.runtime.RuntimeServices;
25
26
27 /**
28  * Simple wrapper for the servlet log. This has Velocity log
29  * messages to ServletContext.log(String).
30  *
31  * @author <a HREF="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
32  * @version $Revision: 1.5 $ $Date: 2004/02/18 20:07:02 $
33  */

34 public class ServletLogger implements LogSystem
35 {
36     protected ServletContext JavaDoc servletContext = null;
37
38     public static final String JavaDoc PREFIX = " Velocity ";
39
40     /**
41      * Construct a simple logger for a servlet environment.
42      * <br>
43      * NOTE: this class expects that the ServletContext has already
44      * been placed in the runtime's application attributes
45      * under its full class name (i.e. "javax.servlet.ServletContext").
46      */

47     public ServletLogger()
48     {
49     }
50
51     /**
52      * init()
53      *
54      * @throws IllegalStateException if the ServletContext is not available
55      * in the application attributes under the appropriate key.
56      */

57     public void init( RuntimeServices rs )
58         throws Exception JavaDoc
59     {
60         Object JavaDoc obj = rs.getApplicationAttribute(ServletContext JavaDoc.class.getName());
61         if (obj == null)
62         {
63             throw new IllegalStateException JavaDoc("Could not retrieve ServletContext from application attributes!");
64         }
65         servletContext = (ServletContext JavaDoc)obj;
66     }
67
68     /**
69      * Send a log message from Velocity.
70      */

71     public void logVelocityMessage(int level, String JavaDoc message)
72     {
73         switch (level)
74         {
75             case LogSystem.WARN_ID:
76                 servletContext.log( PREFIX + RuntimeConstants.WARN_PREFIX + message );
77                 break;
78             case LogSystem.INFO_ID:
79                 servletContext.log( PREFIX + RuntimeConstants.INFO_PREFIX + message);
80                 break;
81             case LogSystem.DEBUG_ID:
82                 servletContext.log( PREFIX + RuntimeConstants.DEBUG_PREFIX + message);
83                 break;
84             case LogSystem.ERROR_ID:
85                 servletContext.log( PREFIX + RuntimeConstants.ERROR_PREFIX + message);
86                 break;
87             default:
88                 servletContext.log( PREFIX + " : " + message);
89                 break;
90         }
91     }
92
93 }
94
Popular Tags