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 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)
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.TRACE_ID:
93 log.trace(message);
94 break;
95 case LogChute.ERROR_ID:
96 log.error(message);
97 break;
98 case LogChute.DEBUG_ID:
99 default:
100 log.debug(message);
101 break;
102 }
103 }
104
105 /**
106 * Send a log message from Velocity with an error.
107 */
108 public void log(int level, String message, Throwable t)
109 {
110 switch (level)
111 {
112 case LogChute.WARN_ID:
113 log.warn(message, t);
114 break;
115 case LogChute.INFO_ID:
116 log.info(message, t);
117 break;
118 case LogChute.TRACE_ID:
119 log.trace(message, t);
120 break;
121 case LogChute.ERROR_ID:
122 log.error(message, t);
123 break;
124 case LogChute.DEBUG_ID:
125 default:
126 log.debug(message, t);
127 break;
128 }
129 }
130
131 /**
132 * Checks whether the specified log level is enabled.
133 */
134 public boolean isLevelEnabled(int level)
135 {
136 switch (level)
137 {
138 case LogChute.DEBUG_ID:
139 return log.isDebugEnabled();
140 case LogChute.INFO_ID:
141 return log.isInfoEnabled();
142 case LogChute.TRACE_ID:
143 return log.isTraceEnabled();
144 case LogChute.WARN_ID:
145 return log.isWarnEnabled();
146 case LogChute.ERROR_ID:
147 return log.isErrorEnabled();
148 default:
149 return true;
150 }
151 }
152 }