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 org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.velocity.runtime.RuntimeServices;
25  import org.apache.velocity.runtime.log.LogChute;
26  
27  /**
28   * Redirects Velocity's LogChute messages to commons-logging.
29   *
30   * <p>To use, first set up commons-logging, then tell Velocity to use
31   * this class for logging by adding the following to your velocity.properties:
32   *
33   * <code>
34   * runtime.log.logsystem.class = org.apache.velocity.runtime.log.CommonsLogLogChute
35   * </code>
36   * </p>
37   *
38   * <p>You may also set this property to specify what log/name Velocity's
39   * messages should be logged to (example below is default).
40   * <code>
41   * runtime.log.logsystem.commons.logging.name = org.apache.velocity
42   * </code>
43   * </p>
44   * 
45   * @author Nathan Bubna
46   * @since Velocity 1.6
47   * @version $Id: CommonsLogLogChute.java 71982 2004-02-18 20:11:07Z nbubna $
48   */
49  public class CommonsLogLogChute implements LogChute
50  {
51  
52      /** Property key for specifying the name for the log instance */
53      public static final String LOGCHUTE_COMMONS_LOG_NAME =
54          "runtime.log.logsystem.commons.logging.name";
55  
56      /** Default name for the commons-logging instance */
57      public static final String DEFAULT_LOG_NAME = "org.apache.velocity";
58  
59      
60      /** the commons-logging Log instance */
61      protected Log log;
62  
63  
64      /********** LogChute methods *************/
65  
66      public void init(RuntimeServices rs) throws Exception
67      {
68          String name = 
69              (String)rs.getProperty(LOGCHUTE_COMMONS_LOG_NAME);
70          
71          if (name == null)
72          {
73              name = DEFAULT_LOG_NAME;
74          }
75          log = LogFactory.getLog(name);
76          log(LogChute.DEBUG_ID, "CommonsLogLogChute name is '" + name + "'");
77      }
78  
79      /**
80       * Send a log message from Velocity.
81       */
82      public void log(int level, String message)
83      {
84          switch (level) 
85          {
86              case LogChute.WARN_ID:
87                  log.warn(message);
88                  break;
89              case LogChute.INFO_ID:
90                  log.info(message);
91                  break;
92              case LogChute.DEBUG_ID:
93                  log.debug(message);
94                  break;
95              case LogChute.TRACE_ID:
96                  log.trace(message);
97                  break;
98              case LogChute.ERROR_ID:
99                  log.error(message);
100                 break;
101             default:
102                 log.debug(message);
103                 break;
104         }
105     }
106 
107     /**
108      * Send a log message from Velocity with an error.
109      */
110     public void log(int level, String message, Throwable t)
111     {
112         switch (level) 
113         {
114             case LogChute.WARN_ID:
115                 log.warn(message, t);
116                 break;
117             case LogChute.INFO_ID:
118                 log.info(message, t);
119                 break;
120             case LogChute.DEBUG_ID:
121                 log.debug(message, t);
122                 break;
123             case LogChute.TRACE_ID:
124                 log.trace(message, t);
125                 break;
126             case LogChute.ERROR_ID:
127                 log.error(message, t);
128                 break;
129             default:
130                 log.debug(message, t);
131                 break;
132         }
133     }
134 
135     /**
136      * Checks whether the specified log level is enabled.
137      */
138     public boolean isLevelEnabled(int level)
139     {
140         switch (level)
141         {
142             case LogChute.DEBUG_ID:
143                 return log.isDebugEnabled();
144             case LogChute.INFO_ID:
145                 return log.isInfoEnabled();
146             case LogChute.TRACE_ID:
147                 return log.isTraceEnabled();
148             case LogChute.WARN_ID:
149                 return log.isWarnEnabled();
150             case LogChute.ERROR_ID:
151                 return log.isErrorEnabled();
152             default:
153                 return true;
154         }
155     }
156 }