001: package org.apache.velocity.runtime.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: /**
023: * This is a wrapper around a log object, that can add a prefix to log messages
024: * and also turn logging on and off dynamically. It is mainly used to control the
025: * logging of VelociMacro generation messages but is actually generic enough code.
026: *
027:
028: * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
029: * @version $Id: LogDisplayWrapper.java 463298 2006-10-12 16:10:32Z henning $
030: */
031: public class LogDisplayWrapper extends Log {
032: /** The prefix to record with every log message */
033: private final String prefix;
034:
035: /** log messages only if true */
036: private final boolean outputMessages;
037:
038: /** The Log object we wrap */
039: private final Log log;
040:
041: /**
042: * Create a new LogDisplayWrapper
043: * @param log The Log object to wrap.
044: * @param prefix The prefix to record with all messages.
045: * @param outputMessages True when messages should actually get logged.
046: */
047: public LogDisplayWrapper(final Log log, final String prefix,
048: final boolean outputMessages) {
049: super (log.getLogChute());
050: this .log = log;
051: this .prefix = prefix;
052: this .outputMessages = outputMessages;
053: }
054:
055: /**
056: * make sure that we always use the right LogChute Object
057: */
058: protected LogChute getLogChute() {
059: return log.getLogChute();
060: }
061:
062: /**
063: * @see Log#log(int, Object)
064: */
065: protected void log(final int level, final Object message) {
066: log(outputMessages, level, message);
067: }
068:
069: protected void log(final boolean doLogging, final int level,
070: final Object message) {
071: if (doLogging) {
072: getLogChute().log(level, prefix + String.valueOf(message));
073: }
074: }
075:
076: /**
077: * @see Log#log(int, Object, Throwable)
078: */
079: protected void log(final int level, final Object message,
080: final Throwable t) {
081: log(outputMessages, level, message);
082: }
083:
084: protected void log(final boolean doLogging, final int level,
085: final Object message, final Throwable t) {
086: if (doLogging) {
087: getLogChute().log(level, prefix + String.valueOf(message),
088: t);
089: }
090: }
091:
092: /**
093: * Log a trace message.
094: * @param doLogging Log only if this parameter is true.
095: * @param message
096: */
097: public void trace(final boolean doLogging, final Object message) {
098: log(doLogging, LogChute.TRACE_ID, message);
099: }
100:
101: /**
102: * Log a trace message and accompanying Throwable.
103: * @param doLogging Log only if this parameter is true.
104: * @param message
105: * @param t
106: */
107: public void trace(final boolean doLogging, final Object message,
108: final Throwable t) {
109: log(doLogging, LogChute.TRACE_ID, message, t);
110: }
111:
112: /**
113: * Log a debug message.
114: * @param doLogging Log only if this parameter is true.
115: * @param message
116: */
117: public void debug(final boolean doLogging, final Object message) {
118: log(doLogging, LogChute.DEBUG_ID, message);
119: }
120:
121: /**
122: * Log a debug message and accompanying Throwable.
123: * @param doLogging Log only if this parameter is true.
124: * @param message
125: * @param t
126: */
127: public void debug(final boolean doLogging, final Object message,
128: final Throwable t) {
129: log(doLogging, LogChute.DEBUG_ID, message, t);
130: }
131:
132: /**
133: * Log an info message.
134: * @param doLogging Log only if this parameter is true.
135: * @param message
136: */
137: public void info(final boolean doLogging, final Object message) {
138: log(doLogging, LogChute.INFO_ID, message);
139: }
140:
141: /**
142: * Log an info message and accompanying Throwable.
143: * @param doLogging Log only if this parameter is true.
144: * @param message
145: * @param t
146: */
147: public void info(final boolean doLogging, final Object message,
148: final Throwable t) {
149: log(doLogging, LogChute.INFO_ID, message, t);
150: }
151:
152: /**
153: * Log a warning message.
154: * @param doLogging Log only if this parameter is true.
155: * @param message
156: */
157: public void warn(final boolean doLogging, final Object message) {
158: log(doLogging, LogChute.WARN_ID, message);
159: }
160:
161: /**
162: * Log a warning message and accompanying Throwable.
163: * @param doLogging Log only if this parameter is true.
164: * @param message
165: * @param t
166: */
167: public void warn(final boolean doLogging, final Object message,
168: final Throwable t) {
169: log(doLogging, LogChute.WARN_ID, message, t);
170: }
171:
172: /**
173: * Log an error message.
174: * @param doLogging Log only if this parameter is true.
175: * @param message
176: */
177: public void error(final boolean doLogging, final Object message) {
178: log(doLogging, LogChute.ERROR_ID, message);
179: }
180:
181: /**
182: * Log an error message and accompanying Throwable.
183: * @param doLogging Log only if this parameter is true.
184: * @param message
185: * @param t
186: */
187: public void error(final boolean doLogging, final Object message,
188: final Throwable t) {
189: log(doLogging, LogChute.ERROR_ID, message, t);
190: }
191: }
|