View Javadoc

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