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 java.io.StringWriter;
023: import java.io.PrintWriter;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.velocity.app.Velocity;
027: import org.apache.velocity.app.VelocityEngine;
028: import org.apache.velocity.runtime.log.LogSystem;
029:
030: /**
031: * Redirects commons-logging messages to Velocity's LogSystem.
032: *
033: * <p>To use, specify this class in your commons-logging.properties:
034: * <code>
035: * org.apache.commons.logging.Log=org.apache.velocity.tools.generic.log.LogSystemCommonsLog
036: * </code>
037: * </p>
038: *
039: * @version $Id: LogSystemCommonsLog.java 493518 2007-01-06 17:47:14Z cbrisson $
040: */
041: public class LogSystemCommonsLog implements Log {
042:
043: protected static VelocityEngine handler = null;
044:
045: /**
046: * Set a VelocityEngine to handle all the log messages.
047: */
048: public static void setVelocityEngine(VelocityEngine engine) {
049: handler = engine;
050: }
051:
052: // ******************** begin non-static stuff *******************
053:
054: private boolean printStackTrace = false;
055:
056: public LogSystemCommonsLog() {
057: this ("");
058: }
059:
060: public LogSystemCommonsLog(String name) {
061: if (name == null) {
062: throw new NullPointerException("Log name cannot be null");
063: }
064: }
065:
066: /**
067: * Lets you set whether or not this instance should print the
068: * full stack trace of exceptions and errors passed to it.
069: *
070: * <p>It should be possible to create a LogFactory implementation
071: * that takes advantage of this constructor.</p>
072: *
073: * @param pst if true, stack traces will be printed
074: */
075: public LogSystemCommonsLog(boolean pst) {
076: this (pst, null);
077: }
078:
079: /**
080: * Lets you set whether or not this instance should print the
081: * full stack trace of exceptions and errors passed to it.
082: *
083: * <p>It should be possible to create a LogFactory implementation
084: * that takes advantage of this constructor.</p>
085: *
086: * @param pst if true, stack traces will be printed
087: * @param name the name of this logger
088: */
089: public LogSystemCommonsLog(boolean pst, String name) {
090: this (name);
091: this .printStackTrace = pst;
092: }
093:
094: private void log(int level, Object message) {
095: if (handler != null) {
096: switch (level) {
097: case LogSystem.WARN_ID:
098: handler.warn(message);
099: break;
100: case LogSystem.INFO_ID:
101: handler.info(message);
102: break;
103: case LogSystem.DEBUG_ID:
104: handler.debug(message);
105: break;
106: case LogSystem.ERROR_ID:
107: handler.error(message);
108: break;
109: default:
110: handler.debug(message);
111: break;
112: }
113: } else {
114: switch (level) {
115: case LogSystem.WARN_ID:
116: Velocity.warn(message);
117: break;
118: case LogSystem.INFO_ID:
119: Velocity.info(message);
120: break;
121: case LogSystem.DEBUG_ID:
122: Velocity.debug(message);
123: break;
124: case LogSystem.ERROR_ID:
125: Velocity.error(message);
126: break;
127: default:
128: Velocity.debug(message);
129: break;
130: }
131: }
132: }
133:
134: private void log(int level, Object message, Throwable t) {
135: if (printStackTrace) {
136: StringWriter sw = new StringWriter();
137: sw.write(String.valueOf(message));
138: t.printStackTrace(new PrintWriter(sw));
139: log(level, sw);
140: } else {
141: StringBuffer buffer = new StringBuffer(String
142: .valueOf(message));
143: buffer.append(" - ");
144: buffer.append(t.getMessage());
145: log(level, buffer);
146: }
147: }
148:
149: /*************** Commons Log Interface ****************/
150:
151: /**
152: * Passes messages to Velocity's LogSystem at "DEBUG" level.
153: * (it's the lowest available. sorry.)
154: */
155: public void trace(Object message) {
156: log(LogSystem.DEBUG_ID, message);
157: }
158:
159: /**
160: * Passes messages to Velocity's LogSystem at "DEBUG" level.
161: * (it's the lowest available. sorry.)
162: */
163: public void trace(Object message, Throwable t) {
164: log(LogSystem.DEBUG_ID, message, t);
165: }
166:
167: /**
168: * Passes messages to Velocity's LogSystem at "DEBUG" level.
169: */
170: public void debug(Object message) {
171: log(LogSystem.DEBUG_ID, message);
172: }
173:
174: /**
175: * Passes messages to Velocity's LogSystem at "DEBUG" level.
176: */
177: public void debug(Object message, Throwable t) {
178: log(LogSystem.DEBUG_ID, message, t);
179: }
180:
181: /**
182: * Passes messages to Velocity's LogSystem at "INFO" level.
183: */
184: public void info(Object message) {
185: log(LogSystem.INFO_ID, message);
186: }
187:
188: /**
189: * Passes messages to Velocity's LogSystem at "INFO" level.
190: */
191: public void info(Object message, Throwable t) {
192: log(LogSystem.INFO_ID, message, t);
193: }
194:
195: /**
196: * Passes messages to Velocity's LogSystem at "WARN" level.
197: */
198: public void warn(Object message) {
199: log(LogSystem.WARN_ID, message);
200: }
201:
202: /**
203: * Passes messages to Velocity's LogSystem at "WARN" level.
204: */
205: public void warn(Object message, Throwable t) {
206: log(LogSystem.WARN_ID, message, t);
207: }
208:
209: /**
210: * Passes messages to Velocity's LogSystem at "ERROR" level.
211: */
212: public void error(Object message) {
213: log(LogSystem.ERROR_ID, message);
214: }
215:
216: /**
217: * Passes messages to Velocity's LogSystem at "ERROR" level.
218: */
219: public void error(Object message, Throwable t) {
220: log(LogSystem.ERROR_ID, message, t);
221: }
222:
223: /**
224: * Passes messages to Velocity's LogSystem at "ERROR" level.
225: * (it's the highest available. sorry.)
226: */
227: public void fatal(Object message) {
228: log(LogSystem.ERROR_ID, message);
229: }
230:
231: /**
232: * Passes messages to Velocity's LogSystem at "ERROR" level.
233: * (it's the highest available. sorry.)
234: */
235: public void fatal(Object message, Throwable t) {
236: log(LogSystem.ERROR_ID, message, t);
237: }
238:
239: /**
240: * Always returns true since Velocity's LogSystem
241: * doesn't provide this information.
242: *
243: * @return true
244: */
245: public boolean isTraceEnabled() {
246: return true;
247: }
248:
249: /**
250: * Always returns true since Velocity's LogSystem
251: * doesn't provide this information.
252: *
253: * @return true
254: */
255: public boolean isDebugEnabled() {
256: return true;
257: }
258:
259: /**
260: * Always returns true since Velocity's LogSystem
261: * doesn't provide this information.
262: *
263: * @return true
264: */
265: public boolean isInfoEnabled() {
266: return true;
267: }
268:
269: /**
270: * Always returns true since Velocity's LogSystem
271: * doesn't provide this information.
272: *
273: * @return true
274: */
275: public boolean isWarnEnabled() {
276: return true;
277: }
278:
279: /**
280: * Always returns true since Velocity's LogSystem
281: * doesn't provide this information.
282: *
283: * @return true
284: */
285: public boolean isErrorEnabled() {
286: return true;
287: }
288:
289: /**
290: * Always returns true since Velocity's LogSystem
291: * doesn't provide this information.
292: *
293: * @return true
294: */
295: public boolean isFatalEnabled() {
296: return true;
297: }
298:
299: }
|