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.ojb.broker.util.configuration.Configuration;
019    import org.apache.ojb.broker.util.configuration.ConfigurationException;
020    
021    /**
022     * this is a most simple Logger implementation.
023     * All output is directed to System.out.
024     *
025     * @author Thomas Mahler
026     * @version $Id: PoorMansLoggerImpl.java,v 1.1 2007-08-24 22:17:32 ewestfal Exp $
027     */
028    public class PoorMansLoggerImpl implements Logger
029    {
030        protected static final String STR_DEBUG = "DEBUG";
031        protected static final String STR_INFO = "INFO";
032        protected static final String STR_WARN = "WARN";
033        protected static final String STR_ERROR = "ERROR";
034        protected static final String STR_FATAL = "FATAL";
035    
036        protected static final String STR_DEBUG_MSG = "DEBUG: ";
037        protected static final String STR_INFO_MSG = "INFO: ";
038        protected static final String STR_WARN_MSG = "WARN: ";
039        protected static final String STR_ERROR_MSG = "ERROR: ";
040        protected static final String STR_FATAL_MSG = "FATAL: ";
041    
042        protected static final String BRAKE_OPEN = "[";
043        protected static final String BRAKE_CLOSE = "] ";
044    
045        private String name ;
046    
047        private int level = 0;
048    
049        public PoorMansLoggerImpl( String name)
050        {
051            this.name = name ;
052        }
053    
054        protected int getLevel()
055        {
056            return level;
057        }
058    
059        void setLevel(int pLevel)
060        {
061            level = pLevel;
062        }
063    
064        public String getName()
065        {
066            return name;
067        }
068    
069        /**
070         * generate a message for loglevel DEBUG
071         * @param pObject the message Object
072         */
073        public void debug(Object pObject)
074        {
075            debug(pObject, null);
076        }
077    
078        public void debug(Object message, Throwable t)
079        {
080            if (DEBUG >= getLevel())
081            {
082                log(STR_DEBUG_MSG, message, t);
083            }
084        }
085    
086        public void safeDebug(String message,Object obj)
087        {
088            safeDebug(message,obj,null);
089        }
090    
091        public void safeDebug(String message,Object obj,Throwable t)
092        {
093            if(DEBUG >= getLevel())
094            {
095                String toString = null;
096                if(obj != null)
097                {
098                    try
099                    {
100                        toString = obj.toString();
101                    }
102                    catch(Throwable throwable)
103                    {
104                        toString = "BAD toString() impl for "+obj.getClass().getName();
105                    }
106                }
107                log(STR_DEBUG_MSG,message + " : " + toString,t);
108            }
109        }
110    
111    
112        /**
113         * generate a message for loglevel INFO
114         * @param pObject the message Object
115         */
116        public void info(Object pObject)
117        {
118            info(pObject, null);
119        }
120    
121        public void info(Object message, Throwable t)
122        {
123            if (INFO >= getLevel())
124            {
125                log(STR_INFO_MSG, message, t);
126            }
127        }
128    
129        public void safeInfo(String message,Object obj)
130        {
131            safeInfo(message,obj,null);
132        }
133    
134        public void safeInfo(String message,Object obj,Throwable t)
135        {
136            if(INFO >= getLevel())
137            {
138                String toString = null;
139                if(obj != null)
140                {
141                    try
142                    {
143                        toString = obj.toString();
144                    }
145                    catch(Throwable throwable)
146                    {
147                        toString = "BAD toString() impl for "+obj.getClass().getName();
148                    }
149                }
150                log(STR_INFO_MSG, message + " : " + toString,t);
151            }
152        }
153    
154        /**
155         * generate a message for loglevel WARN
156         * @param pObject the message Object
157         */
158        public void warn(Object pObject)
159        {
160            warn(pObject, null);
161        }
162    
163        public void warn(Object message, Throwable t)
164        {
165            if (WARN >= getLevel())
166            {
167                log(STR_WARN_MSG, message, t);
168            }
169        }
170    
171        public void safeWarn(String message,Object obj)
172        {
173            safeWarn(message,obj,null);
174        }
175    
176        public void safeWarn(String message,Object obj,Throwable t)
177        {
178            if(WARN >= getLevel())
179            {
180                String toString = null;
181                if(obj != null)
182                {
183                    try
184                    {
185                        toString = obj.toString();
186                    }
187                    catch(Throwable throwable)
188                    {
189                        toString = "BAD toString() impl for "+obj.getClass().getName();
190                    }
191                }
192                log(STR_WARN_MSG,message + " : " + toString,t);
193            }
194        }
195    
196        /**
197         * generate a message for loglevel ERROR
198         * @param pObject the message Object
199         */
200        public void error(Object pObject)
201        {
202            error(pObject, null);
203        }
204    
205        public void error(Object message, Throwable t)
206        {
207            if (ERROR >= getLevel())
208            {
209                log(STR_ERROR_MSG, message, t);
210            }
211        }
212    
213        public void safeError(String message,Object obj)
214        {
215            safeError(message,obj,null);
216        }
217    
218        public void safeError(String message,Object obj,Throwable t)
219        {
220            if(ERROR >= getLevel())
221            {
222                String toString = null;
223                if(obj != null)
224                {
225                    try
226                    {
227                        toString = obj.toString();
228                    }
229                    catch(Throwable throwable)
230                    {
231                        toString = "BAD toString() impl for "+obj.getClass().getName();
232                    }
233                }
234                log(STR_ERROR_MSG,message + " : " + toString,t);
235            }
236        }
237    
238        /**
239         * generate a message for loglevel FATAL
240         * @param pObject the message Object
241         */
242        public void fatal(Object pObject)
243        {
244            fatal(pObject, null);
245        }
246    
247        public void fatal(Object message, Throwable t)
248        {
249            if (FATAL >= getLevel())
250            {
251                log(STR_FATAL_MSG, message, t);
252            }
253        }
254    
255        public void safeFatal(String message,Object obj)
256        {
257            safeFatal(message,obj,null);
258        }
259    
260        public void safeFatal(String message,Object obj,Throwable t)
261        {
262            if(FATAL >= getLevel())
263            {
264                String toString = null;
265                if(obj != null)
266                {
267                    try
268                    {
269                        toString = obj.toString();
270                    }
271                    catch(Throwable throwable)
272                    {
273                        toString = "BAD toString() impl for "+obj.getClass().getName();
274                    }
275                }
276                log(STR_FATAL_MSG,message + " : " + toString,t);
277            }
278        }
279    
280    
281        public boolean isDebugEnabled()
282        {
283            return isEnabledFor(DEBUG);
284        }
285    
286        public boolean isEnabledFor(int priority)
287        {
288            return priority >= getLevel();
289        }
290    
291        protected void log(String aLevel, Object obj, Throwable t)
292        {
293            System.out.print(BRAKE_OPEN + name + BRAKE_CLOSE + aLevel);
294            if (obj != null && obj instanceof Throwable)
295            {
296                try
297                {
298                    System.out.println(((Throwable) obj).getMessage());
299                    ((Throwable) obj).printStackTrace();
300                }
301                catch (Throwable ignored)
302                {
303                    /*logging should be failsafe*/
304                }
305            }
306            else
307            {
308                System.out.println(obj);
309            }
310    
311            if (t != null)
312            {
313                try
314                {
315                    System.out.println(t.getMessage());
316                    t.printStackTrace();
317                }
318                catch (Throwable ignored)
319                {
320                    /*logging should be failsafe*/
321                }
322            }
323        }
324    
325        /*
326         * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
327         */
328        public void configure(Configuration config) throws ConfigurationException
329        {
330            LoggingConfiguration lc = (LoggingConfiguration) config;
331            String levelName = lc.getLogLevel(name);
332            setLevel(levelName);
333        }
334    
335            public void setLevel(String levelName)
336            {
337                    if (levelName.equalsIgnoreCase(STR_DEBUG))
338                    {
339                        level = DEBUG;
340                    }
341                    else if (levelName.equalsIgnoreCase(STR_INFO))
342                    {
343                        level = INFO;
344                    }
345                    else if (levelName.equalsIgnoreCase(STR_WARN))
346                    {
347                        level = WARN;
348                    }
349                    else if (levelName.equalsIgnoreCase(STR_ERROR))
350                    {
351                        level = ERROR;
352                    }
353                    else if (levelName.equalsIgnoreCase(STR_FATAL))
354                    {
355                        level = FATAL;
356                    }
357                    else
358                    {
359                        level = WARN;
360                    }
361            }
362    }