KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > log4j > Log4jLoggerPlugin


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.logging.log4j;
23
24 import org.apache.log4j.LogManager;
25 import org.apache.log4j.Level;
26 import org.apache.log4j.Category;
27 import org.apache.log4j.Priority;
28 import org.jboss.logging.Logger;
29 import org.jboss.logging.LoggerPlugin;
30 import org.jboss.logging.XLevel;
31
32 /**
33  * Delegate for org.jboss.logging.Logger logging to log4j. Body of implementation
34  * mainly copied from old Logger implementation.
35  *
36  * @see org.jboss.logging.Logger
37  * @see org.jboss.logging.LoggerPlugin
38  *
39  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
40  * @version $Revision: 2054 $
41  *
42  * <p><b>Revisions:</b>
43  *
44  * <p><b>3. mai 2002 Sacha Labourey:</b>
45  * <ul>
46  * <li> First implementation </li>
47  * </ul>
48  * </p>
49  * <p><b>4. february 2003 Dag Liodden:</b>
50  * <ul>
51  * <li>Fixed Log4J locationinfo by sending the fully qualified classname of <code>Logger</code> to Log4J</li>
52  * </ul>
53  * </p>
54  */

55
56 public class Log4jLoggerPlugin implements LoggerPlugin
57 {
58    
59    // Constants -----------------------------------------------------
60

61    /**
62     * Fully qualified classname for this class so Log4J locationinfo will be
63     * correct
64     */

65    private static final String JavaDoc FQCN = Logger.class.getName();
66    
67    // Attributes ----------------------------------------------------
68

69    /** The Log4j delegate logger. */
70    private transient org.apache.log4j.Logger log;
71
72    // Static --------------------------------------------------------
73

74    // Constructors --------------------------------------------------
75

76    public Log4jLoggerPlugin () { }
77    
78    public void init (String JavaDoc name)
79    {
80       log = LogManager.getLogger(name);
81    }
82    
83    // Public --------------------------------------------------------
84

85    public Category getCategory()
86    {
87       return log;
88    }
89
90    /**
91     * Exposes the delegate Log4j Logger.
92     */

93    public org.apache.log4j.Logger getLogger()
94    {
95       return log;
96    }
97    
98    // LoggerPlugin implementation ----------------------------------------------
99

100    public boolean isTraceEnabled()
101    {
102       if (log.isEnabledFor(XLevel.TRACE) == false)
103          return false;
104       return XLevel.TRACE.isGreaterOrEqual(log.getEffectiveLevel());
105    }
106
107    /**
108     * Issue a log msg with a level of TRACE.
109     * Invokes log.log(XLevel.TRACE, message);
110     */

111    public void trace(Object JavaDoc message)
112    {
113       log.log(FQCN, XLevel.TRACE, message, null);
114    }
115
116    /**
117     * Issue a log msg and throwable with a level of TRACE.
118     * Invokes log.log(XLevel.TRACE, message, t);
119     */

120    public void trace(Object JavaDoc message, Throwable JavaDoc t)
121    {
122       log.log(FQCN, XLevel.TRACE, message, t);
123    }
124
125    /**
126     * Check to see if the TRACE level is enabled for this logger.
127     *
128     * @return true if a {@link #trace(Object)} method invocation would pass
129     * the msg to the configured appenders, false otherwise.
130     */

131    public boolean isDebugEnabled()
132    {
133       Level l = Level.DEBUG;
134       if (log.isEnabledFor(l) == false)
135          return false;
136       return l.isGreaterOrEqual(log.getEffectiveLevel());
137    }
138
139    /**
140     * Issue a log msg with a level of DEBUG.
141     * Invokes log.log(Level.DEBUG, message);
142     */

143    public void debug(Object JavaDoc message)
144    {
145       log.log(FQCN, Level.DEBUG, message, null);
146    }
147
148    /**
149     * Issue a log msg and throwable with a level of DEBUG.
150     * Invokes log.log(Level.DEBUG, message, t);
151     */

152    public void debug(Object JavaDoc message, Throwable JavaDoc t)
153    {
154       log.log(FQCN, Level.DEBUG, message, t);
155    }
156
157    /**
158     * Check to see if the INFO level is enabled for this logger.
159     *
160     * @return true if a {@link #info(Object)} method invocation would pass
161     * the msg to the configured appenders, false otherwise.
162     */

163    public boolean isInfoEnabled()
164    {
165       Level l = Level.INFO;
166       if (log.isEnabledFor(l) == false)
167          return false;
168       return l.isGreaterOrEqual(log.getEffectiveLevel());
169    }
170
171    /**
172     * Issue a log msg with a level of INFO.
173     * Invokes log.log(Level.INFO, message);
174     */

175    public void info(Object JavaDoc message)
176    {
177       log.log(FQCN, Level.INFO, message, null);
178    }
179
180    /**
181     * Issue a log msg and throwable with a level of INFO.
182     * Invokes log.log(Level.INFO, message, t);
183     */

184    public void info(Object JavaDoc message, Throwable JavaDoc t)
185    {
186       log.log(FQCN, Level.INFO, message, t);
187    }
188
189    /**
190     * Issue a log msg with a level of WARN.
191     * Invokes log.log(Level.WARN, message);
192     */

193    public void warn(Object JavaDoc message)
194    {
195       log.log(FQCN, Level.WARN, message, null);
196    }
197
198    /**
199     * Issue a log msg and throwable with a level of WARN.
200     * Invokes log.log(Level.WARN, message, t);
201     */

202    public void warn(Object JavaDoc message, Throwable JavaDoc t)
203    {
204       log.log(FQCN, Level.WARN, message, t);
205    }
206
207    /**
208     * Issue a log msg with a level of ERROR.
209     * Invokes log.log(Level.ERROR, message);
210     */

211    public void error(Object JavaDoc message)
212    {
213       log.log(FQCN, Level.ERROR, message, null);
214    }
215
216    /**
217     * Issue a log msg and throwable with a level of ERROR.
218     * Invokes log.log(Level.ERROR, message, t);
219     */

220    public void error(Object JavaDoc message, Throwable JavaDoc t)
221    {
222       log.log(FQCN, Level.ERROR, message, t);
223    }
224
225    /**
226     * Issue a log msg with a level of FATAL.
227     * Invokes log.log(Level.FATAL, message);
228     */

229    public void fatal(Object JavaDoc message)
230    {
231       log.log(FQCN, Level.FATAL, message, null);
232    }
233
234    /**
235     * Issue a log msg and throwable with a level of FATAL.
236     * Invokes log.log(Level.FATAL, message, t);
237     */

238    public void fatal(Object JavaDoc message, Throwable JavaDoc t)
239    {
240       log.log(FQCN, Level.FATAL, message, t);
241    }
242
243    /**
244     * Issue a log msg with the given level.
245     * Invokes log.log(p, message);
246     *
247     * @deprecated Use Level versions.
248     */

249    public void log(Priority p, Object JavaDoc message)
250    {
251       log.log(FQCN, p, message, null);
252    }
253
254    /**
255     * Issue a log msg with the given priority.
256     * Invokes log.log(p, message, t);
257     *
258     * @deprecated Use Level versions.
259     */

260    public void log(Priority p, Object JavaDoc message, Throwable JavaDoc t)
261    {
262       log.log(FQCN, p, message, t);
263    }
264
265    /**
266     * Issue a log msg with the given level.
267     * Invokes log.log(l, message);
268     */

269    public void log(Level l, Object JavaDoc message)
270    {
271       log.log(FQCN, l, message, null);
272    }
273
274    /**
275     * Issue a log msg with the given level.
276     * Invokes log.log(l, message, t);
277     */

278    public void log(Level l, Object JavaDoc message, Throwable JavaDoc t)
279    {
280       log.log(FQCN, l, message, t);
281    }
282
283    // Y overrides ---------------------------------------------------
284

285    // Package protected ---------------------------------------------
286

287    // Protected -----------------------------------------------------
288

289    // Private -------------------------------------------------------
290

291    // Inner classes -------------------------------------------------
292

293 }
294
Popular Tags