View Javadoc

1   package org.apache.velocity.runtime.log;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.StringWriter;
23  import java.io.PrintWriter;
24  import javax.servlet.ServletContext;
25  import org.apache.velocity.runtime.RuntimeServices;
26  
27  /**
28   * Simple wrapper for the servlet log.  This passes Velocity log
29   * messages to ServletContext.log(String).  You may configure the
30   * level of output in your velocity.properties by adding the
31   * "runtime.log.logsystem.servlet.level" property with one of the
32   * following values: error, warn, info, debug, or trace.  The default
33   * is trace.
34   *
35   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
36   * @author Nathan Bubna
37   * @version $Revision: 530525 $ $Date: 2007-04-19 21:14:54 +0200 (jeu, 19 avr 2007) $
38   */
39  public class ServletLogChute implements LogChute
40  {
41      public static final String RUNTIME_LOG_LEVEL_KEY = 
42          "runtime.log.logsystem.servlet.level";
43  
44      private int enabled = TRACE_ID;
45  
46      protected ServletContext servletContext = null;
47  
48      public static final String PREFIX = " Velocity ";
49  
50      /**
51       * Construct a simple logger for a servlet environment.
52       * <br>
53       * NOTE: this class expects that the ServletContext has already
54       *       been placed in the runtime's application attributes
55       *       under its full class name (i.e. "javax.servlet.ServletContext").
56       */
57      public ServletLogChute()
58      {
59      }
60  
61      /**
62       * init()
63       *
64       * @throws IllegalStateException if the ServletContext is not available
65       *         in the application attributes under the appropriate key.
66       */
67      public void init(RuntimeServices rs) throws Exception
68      {
69          Object obj = rs.getApplicationAttribute(ServletContext.class.getName());
70          if (obj == null)
71          {
72              throw new UnsupportedOperationException("Could not retrieve ServletContext from application attributes");
73          }
74          servletContext = (ServletContext)obj;
75  
76          // look for a level config property
77          String level = (String)rs.getProperty(RUNTIME_LOG_LEVEL_KEY);
78          if (level != null)
79          {
80              // and set it accordingly
81              setEnabledLevel(toLevel(level));
82          }
83      }
84  
85      protected int toLevel(String level) {
86          if (level.equalsIgnoreCase("debug"))
87          {
88              return DEBUG_ID;
89          }
90          else if (level.equalsIgnoreCase("info"))
91          {
92              return INFO_ID;
93          }
94          else if (level.equalsIgnoreCase("warn"))
95          {
96              return WARN_ID;
97          }
98          else if (level.equalsIgnoreCase("error"))
99          {
100             return ERROR_ID;
101         }
102         else
103         {
104             return TRACE_ID;
105         }
106     }
107 
108     /**
109      * Set the minimum level at which messages will be printed.
110      */
111     public void setEnabledLevel(int level)
112     {
113         this.enabled = level;
114     }
115 
116     /**
117      * Returns the current minimum level at which messages will be printed.
118      */
119     public int getEnabledLevel()
120     {
121         return this.enabled;
122     }
123 
124     /**
125      * This will return true if the specified level
126      * is equal to or higher than the level this
127      * LogChute is enabled for.
128      */
129     public boolean isLevelEnabled(int level)
130     {
131         return (level >= this.enabled);
132     }
133 
134     /**
135      * Send a log message from Velocity.
136      */
137     public void log(int level, String message)
138     {
139         if (!isLevelEnabled(level))
140         {
141             return;
142         }
143 
144         switch (level)
145         {
146             case WARN_ID:
147                 servletContext.log(PREFIX + WARN_PREFIX + message);
148                 break;
149             case INFO_ID:
150                 servletContext.log(PREFIX + INFO_PREFIX + message);
151                 break;
152             case DEBUG_ID:
153                 servletContext.log(PREFIX + DEBUG_PREFIX + message);
154                 break;
155             case TRACE_ID:
156                 servletContext.log(PREFIX + TRACE_PREFIX + message);
157                 break;
158             case ERROR_ID:
159                 servletContext.log(PREFIX + ERROR_PREFIX + message);
160                 break;
161             default:
162                 servletContext.log(PREFIX + " : " + message);
163                 break;
164         }
165     }
166 
167     public void log(int level, String message, Throwable t)
168     {
169         if (!isLevelEnabled(level))
170         {
171             return;
172         }
173 
174         message += " - "+t.toString();
175         if (level >= ERROR_ID)
176         {
177             StringWriter sw = new StringWriter();
178             t.printStackTrace(new PrintWriter(sw));
179             message += "\n" + sw.toString();
180         }
181 
182         log(level, message);
183     }
184 
185 }