View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.webapp.logging.velocity;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.velocity.runtime.RuntimeServices;
22  import org.apache.velocity.runtime.log.LogSystem;
23  
24  /***
25   *  Implementation of a LogSystem using Commons Logging to route Velocity message
26   *  through a IsolatedLog4JLogger setup.
27   * <p>
28   * Configure the following in your velocity.properties:
29   * <ul>
30   *   <li>runtime.log.logsystem.class=org.apache.jetspeed.webapp.logging.velocity.CommonsLoggingLog4JLogSystem</li>
31   *   <li>runtime.log.logsystem.log4j.category=&lt;a Log4J Category name to capture Velocity message, default value: "velocity"&gt;</li>
32   * </ul>
33   * For further information about setting up and configuring velocity:
34   * <a href="http://jakarta.apache.org/velocity/docs/developer-guide.html">Velocity - Developer's Guide</a>
35   * </p>
36   * <p>
37   * If you want to use a VelocityEngine instantiated by Spring using its org.springframework.ui.velocity.VelocityEngineFactoryBean
38   * then you can also configure the above properties inline in its defintion or point it to your velocity.properties file.<br/>
39   * But, beware of the following: the VelocityEngineFactoryBean by default overrides logging any configuration and hooks up their own
40   * CommonsLoggingLogSystem. Which works fine just as this one, but uses as (hard coded) logging category the VelocityEngine class name.
41   * So, if you do want to route your Velocity logging using your own category (or our default "velocity"), then you need to override the
42   * VelocityEngineFactoryBean default logging setup by setting its "overrideLogging" property to false.
43   * </p>
44   * <p>
45   * </p>
46   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
47   * @version $Id$
48   */
49  public class CommonsLoggingLog4JLogSystem implements LogSystem
50  {
51      public static final String DEFAULT_CATEGORY = "velocity";
52      
53      private Log logger;
54      
55      /* (non-Javadoc)
56       * @see org.apache.velocity.runtime.log.LogSystem#init(org.apache.velocity.runtime.RuntimeServices)
57       */
58      public void init(RuntimeServices rs) throws Exception
59      {
60          String categoryname =  (String) rs.getProperty("runtime.log.logsystem.log4j.category");
61  
62          if ( categoryname == null )
63          {
64              categoryname = DEFAULT_CATEGORY;
65          }
66          logger = LogFactory.getLog(categoryname);
67  
68          logVelocityMessage( DEBUG_ID, "CommonsLoggingLog4JLogSystem using category '" + categoryname + "'");
69      }
70  
71      /* (non-Javadoc)
72       * @see org.apache.velocity.runtime.log.LogSystem#logVelocityMessage(int, java.lang.String)
73       */
74      public void logVelocityMessage(int level, String message)
75      {
76          switch (level) 
77          {
78              case LogSystem.WARN_ID:
79                  logger.warn( message );
80                  break;
81              case LogSystem.INFO_ID:
82                  logger.info(message);
83                  break;
84              case LogSystem.DEBUG_ID:
85                  logger.debug(message);
86                  break;
87              case LogSystem.ERROR_ID:
88                  logger.error(message);
89                  break;
90              default:
91                  logger.debug(message);
92                  break;
93          }
94      }
95  }