001: package org.apache.velocity.tools.generic.log;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.velocity.runtime.RuntimeServices;
025: import org.apache.velocity.runtime.log.LogSystem;
026:
027: /**
028: * Redirects Velocity's LogSystem messages to commons-logging.
029: *
030: * <p>To use, first set up commons-logging, then tell Velocity to use
031: * this class for logging by adding the following to your velocity.properties:
032: *
033: * <code>
034: * runtime.log.logsystem.class = org.apache.velocity.tools.generic.log.CommonsLogLogSystem
035: * </code>
036: * </p>
037: *
038: * <p>You may also set this property to specify what log/name Velocity's
039: * messages should be logged to (example below is default).
040: * <code>
041: * runtime.log.logsystem.commons.logging.name = org.apache.velocity
042: * </code>
043: * </p>
044: *
045: * @author Nathan Bubna
046: * @since VelocityTools 1.1
047: * @version $Id: CommonsLogLogSystem.java 479724 2006-11-27 18:49:37Z nbubna $
048: */
049: public class CommonsLogLogSystem implements LogSystem {
050:
051: /** Property key for specifying the name for the log instance */
052: public static final String LOGSYSTEM_COMMONS_LOG_NAME = "runtime.log.logsystem.commons.logging.name";
053:
054: /** Default name for the commons-logging instance */
055: public static final String DEFAULT_LOG_NAME = "org.apache.velocity";
056:
057: /** the commons-logging Log instance */
058: protected Log log;
059:
060: /********** LogSystem methods *************/
061:
062: public void init(RuntimeServices rs) throws Exception {
063: String name = (String) rs
064: .getProperty(LOGSYSTEM_COMMONS_LOG_NAME);
065:
066: if (name == null) {
067: name = DEFAULT_LOG_NAME;
068: }
069: log = LogFactory.getLog(name);
070: logVelocityMessage(LogSystem.DEBUG_ID,
071: "CommonsLogLogSystem name is '" + name + "'");
072: }
073:
074: /**
075: * Send a log message from Velocity.
076: */
077: public void logVelocityMessage(int level, String message) {
078: switch (level) {
079: case LogSystem.WARN_ID:
080: log.warn(message);
081: break;
082: case LogSystem.INFO_ID:
083: log.info(message);
084: break;
085: //NOTE: this is a hack to offer minor support for the
086: // new trace level in Velocity 1.5
087: case -1:
088: log.trace(message);
089: break;
090: case LogSystem.ERROR_ID:
091: log.error(message);
092: break;
093: case LogSystem.DEBUG_ID:
094: default:
095: log.debug(message);
096: break;
097: }
098: }
099:
100: }
|