001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.logging.impl;
018:
019: import java.io.Serializable;
020: import org.apache.log.Logger;
021: import org.apache.log.Hierarchy;
022: import org.apache.commons.logging.Log;
023:
024: /**
025: * <p>Implementation of <code>org.apache.commons.logging.Log</code>
026: * that wraps the <a href="http://avalon.apache.org/logkit/">avalon-logkit</a>
027: * logging system. Configuration of <code>LogKit</code> is left to the user.
028: * </p>
029: *
030: * <p><code>LogKit</code> accepts only <code>String</code> messages.
031: * Therefore, this implementation converts object messages into strings
032: * by called their <code>toString()</code> method before logging them.</p>
033: *
034: * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
035: * @author Robert Burrell Donkin
036: * @version $Id: LogKitLogger.java 370631 2006-01-19 20:50:46Z rdonkin $
037: */
038:
039: public class LogKitLogger implements Log, Serializable {
040:
041: // ------------------------------------------------------------- Attributes
042:
043: /** Logging goes to this <code>LogKit</code> logger */
044: protected transient Logger logger = null;
045:
046: /** Name of this logger */
047: protected String name = null;
048:
049: // ------------------------------------------------------------ Constructor
050:
051: /**
052: * Construct <code>LogKitLogger</code> which wraps the <code>LogKit</code>
053: * logger with given name.
054: *
055: * @param name log name
056: */
057: public LogKitLogger(String name) {
058: this .name = name;
059: this .logger = getLogger();
060: }
061:
062: // --------------------------------------------------------- Public Methods
063:
064: /**
065: * <p>Return the underlying Logger we are using.</p>
066: */
067: public Logger getLogger() {
068:
069: if (logger == null) {
070: logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
071: }
072: return (logger);
073:
074: }
075:
076: // ----------------------------------------------------- Log Implementation
077:
078: /**
079: * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
080: *
081: * @param message to log
082: * @see org.apache.commons.logging.Log#trace(Object)
083: */
084: public void trace(Object message) {
085: debug(message);
086: }
087:
088: /**
089: * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
090: *
091: * @param message to log
092: * @param t log this cause
093: * @see org.apache.commons.logging.Log#trace(Object, Throwable)
094: */
095: public void trace(Object message, Throwable t) {
096: debug(message, t);
097: }
098:
099: /**
100: * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
101: *
102: * @param message to log
103: * @see org.apache.commons.logging.Log#debug(Object)
104: */
105: public void debug(Object message) {
106: if (message != null) {
107: getLogger().debug(String.valueOf(message));
108: }
109: }
110:
111: /**
112: * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
113: *
114: * @param message to log
115: * @param t log this cause
116: * @see org.apache.commons.logging.Log#debug(Object, Throwable)
117: */
118: public void debug(Object message, Throwable t) {
119: if (message != null) {
120: getLogger().debug(String.valueOf(message), t);
121: }
122: }
123:
124: /**
125: * Logs a message with <code>org.apache.log.Priority.INFO</code>.
126: *
127: * @param message to log
128: * @see org.apache.commons.logging.Log#info(Object)
129: */
130: public void info(Object message) {
131: if (message != null) {
132: getLogger().info(String.valueOf(message));
133: }
134: }
135:
136: /**
137: * Logs a message with <code>org.apache.log.Priority.INFO</code>.
138: *
139: * @param message to log
140: * @param t log this cause
141: * @see org.apache.commons.logging.Log#info(Object, Throwable)
142: */
143: public void info(Object message, Throwable t) {
144: if (message != null) {
145: getLogger().info(String.valueOf(message), t);
146: }
147: }
148:
149: /**
150: * Logs a message with <code>org.apache.log.Priority.WARN</code>.
151: *
152: * @param message to log
153: * @see org.apache.commons.logging.Log#warn(Object)
154: */
155: public void warn(Object message) {
156: if (message != null) {
157: getLogger().warn(String.valueOf(message));
158: }
159: }
160:
161: /**
162: * Logs a message with <code>org.apache.log.Priority.WARN</code>.
163: *
164: * @param message to log
165: * @param t log this cause
166: * @see org.apache.commons.logging.Log#warn(Object, Throwable)
167: */
168: public void warn(Object message, Throwable t) {
169: if (message != null) {
170: getLogger().warn(String.valueOf(message), t);
171: }
172: }
173:
174: /**
175: * Logs a message with <code>org.apache.log.Priority.ERROR</code>.
176: *
177: * @param message to log
178: * @see org.apache.commons.logging.Log#error(Object)
179: */
180: public void error(Object message) {
181: if (message != null) {
182: getLogger().error(String.valueOf(message));
183: }
184: }
185:
186: /**
187: * Logs a message with <code>org.apache.log.Priority.ERROR</code>.
188: *
189: * @param message to log
190: * @param t log this cause
191: * @see org.apache.commons.logging.Log#error(Object, Throwable)
192: */
193: public void error(Object message, Throwable t) {
194: if (message != null) {
195: getLogger().error(String.valueOf(message), t);
196: }
197: }
198:
199: /**
200: * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>.
201: *
202: * @param message to log
203: * @see org.apache.commons.logging.Log#fatal(Object)
204: */
205: public void fatal(Object message) {
206: if (message != null) {
207: getLogger().fatalError(String.valueOf(message));
208: }
209: }
210:
211: /**
212: * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>.
213: *
214: * @param message to log
215: * @param t log this cause
216: * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
217: */
218: public void fatal(Object message, Throwable t) {
219: if (message != null) {
220: getLogger().fatalError(String.valueOf(message), t);
221: }
222: }
223:
224: /**
225: * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
226: */
227: public boolean isDebugEnabled() {
228: return getLogger().isDebugEnabled();
229: }
230:
231: /**
232: * Checks whether the <code>LogKit</code> logger will log messages of priority <code>ERROR</code>.
233: */
234: public boolean isErrorEnabled() {
235: return getLogger().isErrorEnabled();
236: }
237:
238: /**
239: * Checks whether the <code>LogKit</code> logger will log messages of priority <code>FATAL_ERROR</code>.
240: */
241: public boolean isFatalEnabled() {
242: return getLogger().isFatalErrorEnabled();
243: }
244:
245: /**
246: * Checks whether the <code>LogKit</code> logger will log messages of priority <code>INFO</code>.
247: */
248: public boolean isInfoEnabled() {
249: return getLogger().isInfoEnabled();
250: }
251:
252: /**
253: * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
254: */
255: public boolean isTraceEnabled() {
256: return getLogger().isDebugEnabled();
257: }
258:
259: /**
260: * Checks whether the <code>LogKit</code> logger will log messages of priority <code>WARN</code>.
261: */
262: public boolean isWarnEnabled() {
263: return getLogger().isWarnEnabled();
264: }
265:
266: }
|