01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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=<a Log4J Category name to capture Velocity message, default value: "velocity"></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: public static final String DEFAULT_CATEGORY = "velocity";
51:
52: private Log logger;
53:
54: /* (non-Javadoc)
55: * @see org.apache.velocity.runtime.log.LogSystem#init(org.apache.velocity.runtime.RuntimeServices)
56: */
57: public void init(RuntimeServices rs) throws Exception {
58: String categoryname = (String) rs
59: .getProperty("runtime.log.logsystem.log4j.category");
60:
61: if (categoryname == null) {
62: categoryname = DEFAULT_CATEGORY;
63: }
64: logger = LogFactory.getLog(categoryname);
65:
66: logVelocityMessage(DEBUG_ID,
67: "CommonsLoggingLog4JLogSystem using category '"
68: + 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: switch (level) {
76: case LogSystem.WARN_ID:
77: logger.warn(message);
78: break;
79: case LogSystem.INFO_ID:
80: logger.info(message);
81: break;
82: case LogSystem.DEBUG_ID:
83: logger.debug(message);
84: break;
85: case LogSystem.ERROR_ID:
86: logger.error(message);
87: break;
88: default:
89: logger.debug(message);
90: break;
91: }
92: }
93: }
|