KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > logging > Logger


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.logging;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22
23 import org.apache.log4j.Category;
24 import org.apache.log4j.Level;
25 import org.apache.commons.logging.Log;
26
27 public class Logger
28     implements Log
29 {
30     private static final String JavaDoc STRUTS_APPENDER = "commons-logging";
31
32     static
33     {
34         // Need to get rid of the appender that Struts adds so
35
// that we don't spam the console with all messages
36
Category root = Category.getRoot();
37
38         if (root.getAppender(STRUTS_APPENDER) != null)
39             root.removeAppender(STRUTS_APPENDER);
40     }
41
42     private org.apache.log4j.Logger _logInstance;
43
44     /**
45      * Factory method for creating NetUI Logger instances.
46      *
47      * @param clazz
48      * @return a {@link Logger} instance for the given class
49      */

50     public static Logger getInstance(Class JavaDoc clazz)
51     {
52         return new Logger(clazz);
53     }
54
55     public Logger(Class JavaDoc clazz)
56     {
57         this(clazz.getName());
58     }
59
60     public Logger(String JavaDoc className)
61     {
62         _logInstance = org.apache.log4j.Logger.getLogger(className);
63     }
64
65     public boolean isDebugEnabled()
66     {
67         return _logInstance.isEnabledFor(Level.DEBUG);
68     }
69
70     public boolean isErrorEnabled()
71     {
72         return _logInstance.isEnabledFor(Level.ERROR);
73     }
74
75     public boolean isFatalEnabled()
76     {
77         return _logInstance.isEnabledFor(Level.FATAL);
78     }
79
80     public boolean isInfoEnabled()
81     {
82         return _logInstance.isEnabledFor(Level.INFO);
83     }
84
85     public boolean isTraceEnabled()
86     {
87         return _logInstance.isEnabledFor(Level.DEBUG);
88     }
89
90     public boolean isWarnEnabled()
91     {
92         return _logInstance.isEnabledFor(Level.WARN);
93     }
94
95     public void debug(Object JavaDoc message)
96     {
97         if (_logInstance.isEnabledFor(Level.DEBUG))
98             _logInstance.debug(message);
99     }
100
101     public void debug(Object JavaDoc message, Throwable JavaDoc t)
102     {
103         if (_logInstance.isEnabledFor(Level.DEBUG))
104             _logInstance.debug(format(message, t));
105     }
106
107     public void trace(Object JavaDoc message)
108     {
109         if (_logInstance.isEnabledFor(Level.DEBUG))
110             _logInstance.debug(message);
111     }
112
113     public void trace(Object JavaDoc message, Throwable JavaDoc t)
114     {
115         if (_logInstance.isEnabledFor(Level.DEBUG))
116             _logInstance.debug(format(message, t));
117     }
118
119     public void info(Object JavaDoc message)
120     {
121         if (_logInstance.isEnabledFor(Level.INFO))
122             _logInstance.info(message);
123     }
124
125     public void info(Object JavaDoc message, Throwable JavaDoc t)
126     {
127         if (_logInstance.isEnabledFor(Level.INFO))
128             _logInstance.info(format(message, t));
129     }
130
131     public void warn(Object JavaDoc message)
132     {
133         if (_logInstance.isEnabledFor(Level.WARN))
134             _logInstance.warn(message);
135     }
136
137     public void warn(Object JavaDoc message, Throwable JavaDoc t)
138     {
139         if (_logInstance.isEnabledFor(Level.WARN))
140             _logInstance.warn(format(message, t));
141     }
142
143     public void error(Object JavaDoc message)
144     {
145         if (_logInstance.isEnabledFor(Level.ERROR))
146             _logInstance.error(message);
147     }
148
149     public void error(Object JavaDoc message, Throwable JavaDoc t)
150     {
151         if (_logInstance.isEnabledFor(Level.ERROR))
152             _logInstance.error(format(message, t));
153     }
154
155     public void fatal(Object JavaDoc message)
156     {
157         if (_logInstance.isEnabledFor(Level.FATAL))
158             _logInstance.fatal(message);
159     }
160
161     public void fatal(Object JavaDoc message, Throwable JavaDoc t)
162     {
163         if (_logInstance.isEnabledFor(Level.FATAL))
164             _logInstance.fatal(format(message, t));
165     }
166
167     private String JavaDoc format(Object JavaDoc m, Throwable JavaDoc t)
168     {
169         if(t == null)
170             return m.toString();
171
172         StringWriter JavaDoc sw = new StringWriter JavaDoc();
173         t.printStackTrace(new PrintWriter JavaDoc(sw));
174
175         return m + "\n\n" + "Throwable: " + t.toString() + "\nStack Trace:\n" + sw.toString();
176     }
177 }
178
Popular Tags