001: package org.apache.ojb.broker.util.logging;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.ojb.broker.util.configuration.Configuration;
021: import org.apache.ojb.broker.util.configuration.ConfigurationException;
022:
023: /**
024: * This is a Logger implementation based on jakarta commons logging.
025: * It can be enabled by putting
026: * LoggerClass=org.apache.ojb.broker.util.logging.CommonsLoggerImpl
027: * in the OJB .properties file. <br>
028: *
029: * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
030: * @version $Id: CommonsLoggerImpl.java,v 1.4.2.3 2005/12/21 22:28:16 tomdz Exp $
031: */
032: public class CommonsLoggerImpl implements Logger {
033: private String name;
034: private transient Log log;
035:
036: /**
037: * Constructor for CommonsLoggerImpl.
038: */
039: public CommonsLoggerImpl(String aName) {
040: this .name = aName;
041: }
042:
043: /**
044: * Returns the log.
045: * @return Log
046: */
047: public Log getLog() {
048: /*
049: Logger interface extends Serializable, thus Log field is
050: declared 'transient' and we have to null-check
051: */
052: if (log == null) {
053: log = LogFactory.getLog(name);
054: }
055: return log;
056: }
057:
058: /**
059: * @see org.apache.ojb.broker.util.logging.Logger#isEnabledFor(int)
060: */
061: public boolean isEnabledFor(int priority) {
062: Log commonsLog = getLog();
063: switch (priority) {
064: case Logger.DEBUG:
065: return commonsLog.isDebugEnabled();
066: case Logger.INFO:
067: return commonsLog.isInfoEnabled();
068: case Logger.WARN:
069: return commonsLog.isWarnEnabled();
070: case Logger.ERROR:
071: return commonsLog.isErrorEnabled();
072: case Logger.FATAL:
073: return commonsLog.isFatalEnabled();
074: }
075: return false;
076: }
077:
078: /**
079: * @see org.apache.ojb.broker.util.logging.Logger#debug(Object)
080: */
081: public void debug(Object pObject) {
082: getLog().debug(pObject);
083: }
084:
085: /**
086: * @see org.apache.ojb.broker.util.logging.Logger#info(Object)
087: */
088: public void info(Object pObject) {
089: getLog().info(pObject);
090: }
091:
092: /**
093: * @see org.apache.ojb.broker.util.logging.Logger#warn(Object)
094: */
095: public void warn(Object pObject) {
096: getLog().warn(pObject);
097: }
098:
099: /**
100: * @see org.apache.ojb.broker.util.logging.Logger#error(Object)
101: */
102: public void error(Object pObject) {
103: getLog().error(pObject);
104: }
105:
106: /**
107: * @see org.apache.ojb.broker.util.logging.Logger#fatal(Object)
108: */
109: public void fatal(Object pObject) {
110: getLog().fatal(pObject);
111: }
112:
113: /**
114: * @see org.apache.ojb.broker.util.logging.Logger#debug(Object, Throwable)
115: */
116: public void debug(Object message, Throwable obj) {
117: getLog().debug(message, obj);
118: }
119:
120: /**
121: * @see org.apache.ojb.broker.util.logging.Logger#info(Object, Throwable)
122: */
123: public void info(Object message, Throwable obj) {
124: getLog().info(message, obj);
125: }
126:
127: /**
128: * @see org.apache.ojb.broker.util.logging.Logger#warn(Object, Throwable)
129: */
130: public void warn(Object message, Throwable obj) {
131: getLog().warn(message, obj);
132: }
133:
134: /**
135: * @see org.apache.ojb.broker.util.logging.Logger#error(Object, Throwable)
136: */
137: public void error(Object message, Throwable obj) {
138: getLog().error(message, obj);
139: }
140:
141: /**
142: * @see org.apache.ojb.broker.util.logging.Logger#fatal(Object, Throwable)
143: */
144: public void fatal(Object message, Throwable obj) {
145: getLog().fatal(message, obj);
146: }
147:
148: /**
149: * @see org.apache.ojb.broker.util.logging.Logger#isDebugEnabled()
150: */
151: public boolean isDebugEnabled() {
152: return getLog().isDebugEnabled();
153: }
154:
155: /**
156: * @see org.apache.ojb.broker.util.logging.Logger#getName()
157: */
158: public String getName() {
159: return name;
160: }
161:
162: /**
163: * @see org.apache.ojb.broker.util.logging.Logger#safeDebug(String, Object)
164: */
165: public void safeDebug(String message, Object obj) {
166: if (getLog().isDebugEnabled()) {
167: String toString = safeToString(obj);
168: getLog().debug(message + " : " + toString);
169: }
170: }
171:
172: /**
173: * @see org.apache.ojb.broker.util.logging.Logger#safeDebug(String, Object, Throwable)
174: */
175: public void safeDebug(String message, Object obj, Throwable t) {
176: if (getLog().isDebugEnabled()) {
177: String toString = safeToString(obj);
178: getLog().debug(message + " : " + toString, t);
179: }
180: }
181:
182: /**
183: * @see org.apache.ojb.broker.util.logging.Logger#safeInfo(String, Object)
184: */
185: public void safeInfo(String message, Object obj) {
186: if (getLog().isInfoEnabled()) {
187: String toString = safeToString(obj);
188: getLog().info(message + " : " + toString);
189: }
190: }
191:
192: /**
193: * @see org.apache.ojb.broker.util.logging.Logger#safeInfo(String, Object, Throwable)
194: */
195: public void safeInfo(String message, Object obj, Throwable t) {
196: if (getLog().isInfoEnabled()) {
197: String toString = safeToString(obj);
198: getLog().info(message + " : " + toString, t);
199: }
200: }
201:
202: /**
203: * @see org.apache.ojb.broker.util.logging.Logger#safeWarn(String, Object)
204: */
205: public void safeWarn(String message, Object obj) {
206: if (getLog().isWarnEnabled()) {
207: String toString = safeToString(obj);
208: getLog().warn(message + " : " + toString);
209: }
210: }
211:
212: /**
213: * @see org.apache.ojb.broker.util.logging.Logger#safeWarn(String, Object, Throwable)
214: */
215: public void safeWarn(String message, Object obj, Throwable t) {
216: if (getLog().isWarnEnabled()) {
217: String toString = safeToString(obj);
218: getLog().warn(message + " : " + toString, t);
219: }
220: }
221:
222: /**
223: * @see org.apache.ojb.broker.util.logging.Logger#safeError(String, Object)
224: */
225: public void safeError(String message, Object obj) {
226: if (getLog().isErrorEnabled()) {
227: String toString = safeToString(obj);
228: getLog().error(message + " : " + toString);
229: }
230: }
231:
232: /**
233: * @see org.apache.ojb.broker.util.logging.Logger#safeError(String, Object, Throwable)
234: */
235: public void safeError(String message, Object obj, Throwable t) {
236: if (getLog().isErrorEnabled()) {
237: String toString = safeToString(obj);
238: getLog().error(message + " : " + toString, t);
239: }
240: }
241:
242: /**
243: * @see org.apache.ojb.broker.util.logging.Logger#safeFatal(String, Object)
244: */
245: public void safeFatal(String message, Object obj) {
246: if (getLog().isFatalEnabled()) {
247: String toString = safeToString(obj);
248: getLog().fatal(message + " : " + toString);
249: }
250: }
251:
252: /**
253: * @see org.apache.ojb.broker.util.logging.Logger#safeFatal(String, Object, Throwable)
254: */
255: public void safeFatal(String message, Object obj, Throwable t) {
256: if (getLog().isFatalEnabled()) {
257: String toString = safeToString(obj);
258: getLog().fatal(message + " : " + toString, t);
259: }
260: }
261:
262: /**
263: * provides a safe toString
264: */
265: private String safeToString(Object obj) {
266: String toString = null;
267: if (obj != null) {
268: try {
269: toString = obj.toString();
270: } catch (Throwable ex) {
271: toString = "BAD toString() impl for "
272: + obj.getClass().getName();
273: }
274: }
275: return toString;
276: }
277:
278: /**
279: * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
280: */
281: public void configure(Configuration config)
282: throws ConfigurationException {
283: // do nothing
284: }
285: }
|