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.1 2007-08-24 22:17:32 ewestfal Exp $
031     */
032    public class CommonsLoggerImpl implements Logger
033    {
034        private String name;
035        private transient Log log;
036    
037    
038            /**
039             * Constructor for CommonsLoggerImpl.
040             */
041            public CommonsLoggerImpl(String aName)
042            {
043                this.name = aName;
044            }
045    
046        /**
047             * Returns the log.
048             * @return Log
049             */
050            public Log getLog()
051            {
052                    /*
053            Logger interface extends Serializable, thus Log field is
054            declared 'transient' and we have to null-check
055                    */
056            if(log == null)
057            {
058                log = LogFactory.getLog(name);
059            }
060            return log;
061            }
062    
063        /**
064             * @see org.apache.ojb.broker.util.logging.Logger#isEnabledFor(int)
065             */
066            public boolean isEnabledFor(int priority)
067            {
068                    Log commonsLog = getLog();
069            switch(priority)
070            {
071                case Logger.DEBUG: return commonsLog.isDebugEnabled();
072                case Logger.INFO: return commonsLog.isInfoEnabled();
073                case Logger.WARN: return commonsLog.isWarnEnabled();
074                case Logger.ERROR: return commonsLog.isErrorEnabled();
075                case Logger.FATAL: return commonsLog.isFatalEnabled();
076            }
077            return false;
078        }
079    
080        /**
081             * @see org.apache.ojb.broker.util.logging.Logger#debug(Object)
082             */
083            public void debug(Object pObject)
084            {
085                    getLog().debug(pObject);
086            }
087    
088            /**
089             * @see org.apache.ojb.broker.util.logging.Logger#info(Object)
090             */
091            public void info(Object pObject)
092            {
093            getLog().info(pObject);
094            }
095    
096            /**
097             * @see org.apache.ojb.broker.util.logging.Logger#warn(Object)
098             */
099            public void warn(Object pObject)
100            {
101            getLog().warn(pObject);
102            }
103    
104            /**
105             * @see org.apache.ojb.broker.util.logging.Logger#error(Object)
106             */
107            public void error(Object pObject)
108            {
109            getLog().error(pObject);
110            }
111    
112            /**
113             * @see org.apache.ojb.broker.util.logging.Logger#fatal(Object)
114             */
115            public void fatal(Object pObject)
116            {
117            getLog().fatal(pObject);
118            }
119    
120            /**
121             * @see org.apache.ojb.broker.util.logging.Logger#debug(Object, Throwable)
122             */
123            public void debug(Object message, Throwable obj)
124            {
125            getLog().debug(message, obj);
126            }
127    
128            /**
129             * @see org.apache.ojb.broker.util.logging.Logger#info(Object, Throwable)
130             */
131            public void info(Object message, Throwable obj)
132            {
133            getLog().info(message, obj);
134            }
135    
136            /**
137             * @see org.apache.ojb.broker.util.logging.Logger#warn(Object, Throwable)
138             */
139            public void warn(Object message, Throwable obj)
140            {
141            getLog().warn(message, obj);
142            }
143    
144            /**
145             * @see org.apache.ojb.broker.util.logging.Logger#error(Object, Throwable)
146             */
147            public void error(Object message, Throwable obj)
148            {
149            getLog().error(message, obj);
150            }
151    
152            /**
153             * @see org.apache.ojb.broker.util.logging.Logger#fatal(Object, Throwable)
154             */
155            public void fatal(Object message, Throwable obj)
156            {
157            getLog().fatal(message, obj);
158            }
159    
160            /**
161             * @see org.apache.ojb.broker.util.logging.Logger#isDebugEnabled()
162             */
163            public boolean isDebugEnabled()
164            {
165                    return getLog().isDebugEnabled();
166            }
167    
168            /**
169             * @see org.apache.ojb.broker.util.logging.Logger#getName()
170             */
171            public String getName()
172            {
173                    return name;
174            }
175    
176            /**
177             * @see org.apache.ojb.broker.util.logging.Logger#safeDebug(String, Object)
178             */
179            public void safeDebug(String message, Object obj)
180            {
181            if (getLog().isDebugEnabled())
182            {
183                String toString = safeToString(obj);
184                getLog().debug(message + " : " + toString);
185            }
186            }
187    
188            /**
189             * @see org.apache.ojb.broker.util.logging.Logger#safeDebug(String, Object, Throwable)
190             */
191            public void safeDebug(String message, Object obj, Throwable t)
192            {
193            if (getLog().isDebugEnabled())
194            {
195                String toString = safeToString(obj);
196                getLog().debug(message + " : " + toString, t);
197            }
198            }
199    
200            /**
201             * @see org.apache.ojb.broker.util.logging.Logger#safeInfo(String, Object)
202             */
203            public void safeInfo(String message, Object obj)
204            {
205            if (getLog().isInfoEnabled())
206            {
207                String toString = safeToString(obj);
208                getLog().info(message + " : " + toString);
209            }
210            }
211    
212            /**
213             * @see org.apache.ojb.broker.util.logging.Logger#safeInfo(String, Object, Throwable)
214             */
215            public void safeInfo(String message, Object obj, Throwable t)
216            {
217            if (getLog().isInfoEnabled())
218            {
219                String toString = safeToString(obj);
220                getLog().info(message + " : " + toString, t);
221            }
222            }
223    
224            /**
225             * @see org.apache.ojb.broker.util.logging.Logger#safeWarn(String, Object)
226             */
227            public void safeWarn(String message, Object obj)
228            {
229            if (getLog().isWarnEnabled())
230            {
231                String toString = safeToString(obj);
232                getLog().warn(message + " : " + toString);
233            }
234            }
235    
236            /**
237             * @see org.apache.ojb.broker.util.logging.Logger#safeWarn(String, Object, Throwable)
238             */
239            public void safeWarn(String message, Object obj, Throwable t)
240            {
241            if (getLog().isWarnEnabled())
242            {
243                String toString = safeToString(obj);
244                getLog().warn(message + " : " + toString, t);
245            }
246            }
247    
248            /**
249             * @see org.apache.ojb.broker.util.logging.Logger#safeError(String, Object)
250             */
251            public void safeError(String message, Object obj)
252            {
253            if (getLog().isErrorEnabled())
254            {
255                String toString = safeToString(obj);
256                getLog().error(message + " : " + toString);
257            }
258            }
259    
260            /**
261             * @see org.apache.ojb.broker.util.logging.Logger#safeError(String, Object, Throwable)
262             */
263            public void safeError(String message, Object obj, Throwable t)
264            {
265            if (getLog().isErrorEnabled())
266            {
267                String toString = safeToString(obj);
268                getLog().error(message + " : " + toString, t);
269            }
270            }
271    
272            /**
273             * @see org.apache.ojb.broker.util.logging.Logger#safeFatal(String, Object)
274             */
275            public void safeFatal(String message, Object obj)
276            {
277            if (getLog().isFatalEnabled())
278            {
279                String toString = safeToString(obj);
280                getLog().fatal(message + " : " + toString);
281            }
282            }
283    
284            /**
285             * @see org.apache.ojb.broker.util.logging.Logger#safeFatal(String, Object, Throwable)
286             */
287            public void safeFatal(String message, Object obj, Throwable t)
288            {
289            if (getLog().isFatalEnabled())
290            {
291                            String toString = safeToString(obj);
292                getLog().fatal(message + " : " + toString, t);
293            }
294            }
295    
296        /**
297         * provides a safe toString
298         */ 
299            private String safeToString(Object obj)
300            {
301                    String toString = null;
302                    if (obj != null)
303                    {
304                        try
305                        {
306                            toString = obj.toString();
307                        }
308                        catch (Throwable ex)
309                        {
310                            toString = "BAD toString() impl for " + obj.getClass().getName();
311                        }
312                    }
313                    return toString;
314            }
315    
316            /**
317             * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
318             */
319            public void configure(Configuration config) throws ConfigurationException
320            {
321                // do nothing
322            }
323    }