001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): __________________.
021: */package org.continuent.sequoia.common.log;
022:
023: import org.apache.log4j.Level;
024: import org.apache.log4j.Logger;
025:
026: /**
027: * This a wrapper to the log4j logging system. We provide additional features to
028: * statically remove tracing.
029: *
030: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
031: * @version 1.0
032: */
033: public class Trace {
034: /** Log4j logger instance. */
035: private Logger log4jLogger;
036:
037: /**
038: * Creates a new <code>Trace</code> object from a given log4j
039: * <code>Logger</code>.
040: *
041: * @param log4jLogger the log4j <code>Logger</code>
042: */
043: protected Trace(Logger log4jLogger) {
044: this .log4jLogger = log4jLogger;
045: }
046:
047: /**
048: * Retrieves a logger by its name.
049: *
050: * @param name logger name
051: * @return trace a <code>Trace</code> instance
052: */
053: public static Trace getLogger(String name) {
054: return LogManager.getLogger(name);
055: }
056:
057: /**
058: * Logs a message object with the <code>DEBUG</code> <code>Level</code>.
059: *
060: * @param message the message object to log
061: */
062: public void debug(Object message) {
063: log4jLogger.debug(message);
064: }
065:
066: /**
067: * Logs a message object with the <code>DEBUG</code> <code>Level</code>
068: * including the stack trace of the {@link Throwable}<code>error</code>
069: * passed as parameter.
070: *
071: * @param message the message object to log
072: * @param error the exception to log, including its stack trace
073: */
074: public void debug(Object message, Throwable error) {
075: log4jLogger.debug(message, error);
076: }
077:
078: /**
079: * Logs a message object with the <code>ERROR</code> <code>Level</code>.
080: *
081: * @param message the message object to log
082: */
083: public void error(Object message) {
084: log4jLogger.error(message);
085: }
086:
087: /**
088: * Logs a message object with the <code>ERROR</code> <code>Level</code>
089: * including the stack trace of the {@link Throwable}<code>error</code>
090: * passed as parameter.
091: *
092: * @param message the message object to log.
093: * @param error the exception to log, including its stack trace.
094: */
095: public void error(Object message, Throwable error) {
096: log4jLogger.error(message, error);
097: }
098:
099: /**
100: * Logs a message object with the <code>FATAL</code> <code>Level</code>.
101: *
102: * @param message the message object to log.
103: */
104: public void fatal(Object message) {
105: log4jLogger.fatal(message);
106: }
107:
108: /**
109: * Logs a message object with the <code>FATAL</code> <code>Level</code>
110: * including the stack trace of the {@link Throwable}<code>error</code>
111: * passed as parameter.
112: *
113: * @param message the message object to log.
114: * @param error the exception to log, including its stack trace.
115: */
116: public void fatal(Object message, Throwable error) {
117: log4jLogger.fatal(message, error);
118: }
119:
120: /**
121: * Logs a message object with the <code>INFO</code> <code>Level</code>.
122: *
123: * @param message the message object to log.
124: */
125: public void info(Object message) {
126: log4jLogger.info(message);
127: }
128:
129: /**
130: * Logs a message object with the <code>INFO</code> <code>Level</code>
131: * including the stack trace of the {@link Throwable}<code>error</code>
132: * passed as parameter.
133: *
134: * @param message the message object to log.
135: * @param error the exception to log, including its stack trace.
136: */
137: public void info(Object message, Throwable error) {
138: log4jLogger.info(message, error);
139: }
140:
141: /**
142: * Logs a message object with the <code>WARN</code> <code>Level</code>.
143: *
144: * @param message the message object to log.
145: */
146: public void warn(Object message) {
147: log4jLogger.warn(message);
148: }
149:
150: /**
151: * Logs a message object with the <code>WARN</code> <code>Level</code>
152: * including the stack trace of the {@link Throwable}<code>error</code>
153: * passed as parameter.
154: *
155: * @param message the message object to log.
156: * @param error the exception to log, including its stack trace.
157: */
158: public void warn(Object message, Throwable error) {
159: log4jLogger.warn(message, error);
160: }
161:
162: /**
163: * Checks whether this category is enabled for the
164: * <code>DEBUG</code> <code>Level</code>.
165: * <p>
166: * This function is intended to lessen the computational cost of disabled log
167: * debug statements.
168: * <p>
169: * For some <code>cat</code> Category object, when you write,
170: *
171: * <pre>
172: * cat.debug("This is entry number: " + i );
173: * </pre>
174: *
175: * <p>
176: * You incur the cost constructing the message, concatenatiion in this case,
177: * regardless of whether the message is logged or not.
178: * <p>
179: * If you are worried about speed, then you should write
180: *
181: * <pre>
182: * if(cat.isDebugEnabled()) { cat.debug("This is entry number: " + i ); }
183: * </pre>
184: *
185: * <p>
186: * This way you will not incur the cost of parameter construction if debugging
187: * is disabled for <code>cat</code>. On the other hand, if the
188: * <code>cat</code> is debug enabled, you will incur the cost of evaluating
189: * whether the category is debug enabled twice. Once in
190: * <code>isDebugEnabled</code> and once in the <code>debug</code>. This
191: * is an insignificant overhead since evaluating a category takes about 1%% of
192: * the time it takes to actually log.
193: *
194: * @return <code>true</code> if this category is debug enabled,
195: * <code>false</code> otherwise.
196: */
197: public boolean isDebugEnabled() {
198: return log4jLogger.isDebugEnabled();
199: }
200:
201: /**
202: * Checks whether this category is enabled for the <code>INFO</code>
203: * <code>Level</code>.
204: *
205: * @return <code>true</code> if this category is enabled for
206: * <code>Level</code> <code>INFO</code>, <code>false</code>
207: * otherwise.
208: * @see #isDebugEnabled()
209: */
210: public boolean isInfoEnabled() {
211: return log4jLogger.isInfoEnabled();
212: }
213:
214: /**
215: * Checks whether this category is enabled for the <code>WARN</code>
216: * <code>Level</code>.
217: *
218: * @return <code>true</code> if this category is enabled for
219: * <code>WARN</code> <code>Level</code>, <code>false</code>
220: * otherwise.
221: * @see #isDebugEnabled()
222: */
223: public boolean isWarnEnabled() {
224: return log4jLogger.isEnabledFor(Level.WARN);
225: }
226:
227: /**
228: * Checks whether this category is enabled for the <code>ERROR</code>
229: * <code>Level</code>.
230: *
231: * @return <code>true</code> if this category is enabled for
232: * <code>ERROR</code> <code>Level</code>, <code>false</code>
233: * otherwise.
234: * @see #isDebugEnabled()
235: */
236: public boolean isErrorEnabled() {
237: return log4jLogger.isEnabledFor(Level.ERROR);
238: }
239:
240: /**
241: * Checks whether this category is enabled for the <code>FATAL</code>
242: * <code>Level</code>.
243: *
244: * @return <code>true</code> if this category is enabled for
245: * <code>FATAL</code> <code>Level</code>, <code>false</code>
246: * otherwise.
247: * @see #isDebugEnabled()
248: */
249: public boolean isFatalEnabled() {
250: return log4jLogger.isEnabledFor(Level.FATAL);
251: }
252: }
|