1 package org.apache.velocity.tools.generic.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.LogSystem; 26 27 /** 28 * Redirects Velocity's LogSystem messages to commons-logging. 29 * 30 * @author Nathan Bubna 31 * @since VelocityTools 1.1 32 * @version $Id: CommonsLogLogSystem.java 534721 2007-05-03 05:55:15Z nbubna $ 33 */ 34 35 /** 36 * Redirects Velocity's LogSystem messages to commons-logging. 37 * 38 * <p>Please use CommonsLogLogChute in Velocity Engine 1.6 as this will be 39 * removed in VelocityTools releases subsequent to Velocity 1.6's release, 40 * if not earlier. 41 * </p> 42 * 43 * <p>To use, first set up commons-logging, then tell Velocity to use 44 * this class for logging by adding the following to your velocity.properties: 45 * 46 * <code> 47 * runtime.log.logsystem.class = org.apache.velocity.tools.generic.log.CommonsLogLogSystem 48 * </code> 49 * </p> 50 * 51 * <p>You may also set this property to specify what log/name Velocity's 52 * messages should be logged to (example below is default). 53 * <code> 54 * runtime.log.logsystem.commons.logging.name = org.apache.velocity 55 * </code> 56 * </p> 57 * 58 * @author Nathan Bubna 59 * @deprecated Use CommonsLogLogChute in Velocity 1.6 instead. 60 * @since VelocityTools 1.1 61 * @version $Id: CommonsLogLogSystem.java 534721 2007-05-03 05:55:15Z nbubna $ 62 */ 63 @Deprecated 64 public class CommonsLogLogSystem implements LogSystem 65 { 66 67 /** Property key for specifying the name for the log instance */ 68 public static final String LOGSYSTEM_COMMONS_LOG_NAME = 69 "runtime.log.logsystem.commons.logging.name"; 70 71 /** Default name for the commons-logging instance */ 72 public static final String DEFAULT_LOG_NAME = "org.apache.velocity"; 73 74 75 /** the commons-logging Log instance */ 76 protected Log log; 77 78 79 /********** LogSystem methods *************/ 80 81 public void init(RuntimeServices rs) throws Exception 82 { 83 String name = 84 (String)rs.getProperty(LOGSYSTEM_COMMONS_LOG_NAME); 85 86 if (name == null) 87 { 88 name = DEFAULT_LOG_NAME; 89 } 90 log = LogFactory.getLog(name); 91 logVelocityMessage(LogSystem.DEBUG_ID, 92 "CommonsLogLogSystem name is '" + name + "'"); 93 } 94 95 /** 96 * Send a log message from Velocity. 97 */ 98 public void logVelocityMessage(int level, String message) 99 { 100 switch (level) 101 { 102 case LogSystem.WARN_ID: 103 log.warn(message); 104 break; 105 case LogSystem.INFO_ID: 106 log.info(message); 107 break; 108 //NOTE: this is a hack to offer minor support for the 109 // new trace level in Velocity 1.5 110 case -1: 111 log.trace(message); 112 break; 113 case LogSystem.ERROR_ID: 114 log.error(message); 115 break; 116 case LogSystem.DEBUG_ID: 117 default: 118 log.debug(message); 119 break; 120 } 121 } 122 123 }