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: 685685 $ $Date: 2008-08-13 14:43:27 -0700 (Wed, 13 Aug 2008) $
38   * @since 1.6
39   */
40  public class ServletLogChute implements LogChute
41  {
42      public static final String RUNTIME_LOG_LEVEL_KEY = 
43          "runtime.log.logsystem.servlet.level";
44  
45      private int enabled = TRACE_ID;
46  
47      protected ServletContext servletContext = null;
48  
49      public static final String PREFIX = " Velocity ";
50  
51      /**
52       * Construct a simple logger for a servlet environment.
53       * <br>
54       * NOTE: this class expects that the ServletContext has already
55       *       been placed in the runtime's application attributes
56       *       under its full class name (i.e. "javax.servlet.ServletContext").
57       */
58      public ServletLogChute()
59      {
60      }
61  
62      /**
63       * init()
64       *
65       * @throws IllegalStateException if the ServletContext is not available
66       *         in the application attributes under the appropriate key.
67       */
68      public void init(RuntimeServices rs) throws Exception
69      {
70          Object obj = rs.getApplicationAttribute(ServletContext.class.getName());
71          if (obj == null)
72          {
73              throw new UnsupportedOperationException("Could not retrieve ServletContext from application attributes");
74          }
75          servletContext = (ServletContext)obj;
76  
77          // look for a level config property
78          String level = (String)rs.getProperty(RUNTIME_LOG_LEVEL_KEY);
79          if (level != null)
80          {
81              // and set it accordingly
82              setEnabledLevel(toLevel(level));
83          }
84      }
85  
86      protected int toLevel(String level) {
87          if (level.equalsIgnoreCase("debug"))
88          {
89              return DEBUG_ID;
90          }
91          else if (level.equalsIgnoreCase("info"))
92          {
93              return INFO_ID;
94          }
95          else if (level.equalsIgnoreCase("warn"))
96          {
97              return WARN_ID;
98          }
99          else if (level.equalsIgnoreCase("error"))
100         {
101             return ERROR_ID;
102         }
103         else
104         {
105             return TRACE_ID;
106         }
107     }
108 
109     /**
110      * Set the minimum level at which messages will be printed.
111      */
112     public void setEnabledLevel(int level)
113     {
114         this.enabled = level;
115     }
116 
117     /**
118      * Returns the current minimum level at which messages will be printed.
119      */
120     public int getEnabledLevel()
121     {
122         return this.enabled;
123     }
124 
125     /**
126      * This will return true if the specified level
127      * is equal to or higher than the level this
128      * LogChute is enabled for.
129      */
130     public boolean isLevelEnabled(int level)
131     {
132         return (level >= this.enabled);
133     }
134 
135     /**
136      * Send a log message from Velocity.
137      */
138     public void log(int level, String message)
139     {
140         if (!isLevelEnabled(level))
141         {
142             return;
143         }
144 
145         switch (level)
146         {
147             case WARN_ID:
148                 servletContext.log(PREFIX + WARN_PREFIX + message);
149                 break;
150             case INFO_ID:
151                 servletContext.log(PREFIX + INFO_PREFIX + message);
152                 break;
153             case DEBUG_ID:
154                 servletContext.log(PREFIX + DEBUG_PREFIX + message);
155                 break;
156             case TRACE_ID:
157                 servletContext.log(PREFIX + TRACE_PREFIX + message);
158                 break;
159             case ERROR_ID:
160                 servletContext.log(PREFIX + ERROR_PREFIX + message);
161                 break;
162             default:
163                 servletContext.log(PREFIX + " : " + message);
164                 break;
165         }
166     }
167 
168     public void log(int level, String message, Throwable t)
169     {
170         if (!isLevelEnabled(level))
171         {
172             return;
173         }
174 
175         message += " - "+t.toString();
176         if (level >= ERROR_ID)
177         {
178             StringWriter sw = new StringWriter();
179             t.printStackTrace(new PrintWriter(sw));
180             message += "\n" + sw.toString();
181         }
182 
183         log(level, message);
184     }
185 
186 }