KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > logging > impl > Log4JCategoryLog


1 /*
2  * $Header: /cvsroot/proxool/proxool/src/java/org/logicalcobwebs/logging/impl/Log4JCategoryLog.java,v 1.5 2003/10/20 07:42:03 chr32 Exp $
3  * $Revision: 1.5 $
4  * $Date: 2003/10/20 07:42:03 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.logicalcobwebs.logging.impl;
63
64 import org.logicalcobwebs.logging.Log;
65 import org.apache.log4j.Category;
66 import org.apache.log4j.ConsoleAppender;
67 import org.apache.log4j.PatternLayout;
68 import org.apache.log4j.Level;
69 import org.apache.log4j.Priority;
70
71 import java.util.Enumeration JavaDoc;
72
73 /**
74  * <p>Implementation of {@link org.logicalcobwebs.logging.Log} that maps directly to a Log4J
75  * <strong>Category</strong>. Initial configuration of the corresponding
76  * Category instances should be done in the usual manner, as outlined in
77  * the Log4J documentation.</p>
78  *
79  * @author <a HREF="mailto:sanders@apache.org">Scott Sanders</a>
80  * @author Rod Waldhoff
81  * @author Robert Burrell Donkin
82  * @version $Id: Log4JCategoryLog.java,v 1.5 2003/10/20 07:42:03 chr32 Exp $
83  */

84 public final class Log4JCategoryLog implements Log {
85
86     // ------------------------------------------------------------- Attributes
87

88     /** The fully qualified name of the Log4JCategoryLog class. */
89     private static final String JavaDoc FQCN = Log4JCategoryLog.class.getName ();
90
91     private static boolean initialized = false;
92     private static final String JavaDoc LAYOUT = "%r [%t] %p %c{2} %x - %m%n";
93
94     /** Log to this category */
95     private Category category = null;
96
97
98     // ------------------------------------------------------------ Constructor
99

100     public Log4JCategoryLog () {
101         if (!initialized) {
102             initialize ();
103         }
104     }
105
106     /**
107      * Base constructor
108      */

109     public Log4JCategoryLog (String JavaDoc name) {
110         if (!initialized) {
111             initialize ();
112         }
113         this.category = Category.getInstance (name);
114     }
115
116     /** For use with a log4j factory
117      */

118     public Log4JCategoryLog (Category category) {
119         if (!initialized) {
120             initialize ();
121         }
122         this.category = category;
123     }
124
125
126     // ---------------------------------------------------------- Implmentation
127

128     private void initialize () {
129         Category root = Category.getRoot ();
130         Enumeration JavaDoc appenders = root.getAllAppenders ();
131         if (appenders == null || !appenders.hasMoreElements ()) {
132             // No config, set some defaults (consistent with
133
// commons-logging patterns).
134
ConsoleAppender app = new ConsoleAppender (new PatternLayout (LAYOUT),
135                     ConsoleAppender.SYSTEM_ERR);
136             app.setName ("commons-logging");
137
138             root.addAppender (app);
139             root.setLevel (Level.INFO);
140         }
141         initialized = true;
142     }
143
144     /**
145      * Log a message to the Log4j Category with <code>TRACE</code> priority.
146      * Currently logs to <code>DEBUG</code> level in Log4J.
147      */

148     public void trace (Object JavaDoc message) {
149         category.log (FQCN, Priority.DEBUG, message, null);
150     }
151
152     /**
153      * Log an error to the Log4j Category with <code>TRACE</code> priority.
154      * Currently logs to <code>DEBUG</code> level in Log4J.
155      */

156     public void trace (Object JavaDoc message, Throwable JavaDoc t) {
157         category.log (FQCN, Priority.DEBUG, message, t);
158     }
159
160     /**
161      * Log a message to the Log4j Category with <code>DEBUG</code> priority.
162      */

163     public void debug (Object JavaDoc message) {
164         category.log (FQCN, Priority.DEBUG, message, null);
165     }
166
167     /**
168      * Log an error to the Log4j Category with <code>DEBUG</code> priority.
169      */

170     public void debug (Object JavaDoc message, Throwable JavaDoc t) {
171         category.log (FQCN, Priority.DEBUG, message, t);
172     }
173
174     /**
175      * Log a message to the Log4j Category with <code>INFO</code> priority.
176      */

177     public void info (Object JavaDoc message) {
178         category.log (FQCN, Priority.INFO, message, null);
179     }
180
181     /**
182      * Log an error to the Log4j Category with <code>INFO</code> priority.
183      */

184     public void info (Object JavaDoc message, Throwable JavaDoc t) {
185         category.log (FQCN, Priority.INFO, message, t);
186     }
187
188     /**
189      * Log a message to the Log4j Category with <code>WARN</code> priority.
190      */

191     public void warn (Object JavaDoc message) {
192         category.log (FQCN, Priority.WARN, message, null);
193     }
194
195     /**
196      * Log an error to the Log4j Category with <code>WARN</code> priority.
197      */

198     public void warn (Object JavaDoc message, Throwable JavaDoc t) {
199         category.log (FQCN, Priority.WARN, message, t);
200     }
201
202     /**
203      * Log a message to the Log4j Category with <code>ERROR</code> priority.
204      */

205     public void error (Object JavaDoc message) {
206         category.log (FQCN, Priority.ERROR, message, null);
207     }
208
209     /**
210      * Log an error to the Log4j Category with <code>ERROR</code> priority.
211      */

212     public void error (Object JavaDoc message, Throwable JavaDoc t) {
213         category.log (FQCN, Priority.ERROR, message, t);
214     }
215
216     /**
217      * Log a message to the Log4j Category with <code>FATAL</code> priority.
218      */

219     public void fatal (Object JavaDoc message) {
220         category.log (FQCN, Priority.FATAL, message, null);
221     }
222
223     /**
224      * Log an error to the Log4j Category with <code>FATAL</code> priority.
225      */

226     public void fatal (Object JavaDoc message, Throwable JavaDoc t) {
227         category.log (FQCN, Priority.FATAL, message, t);
228     }
229
230     /**
231      * Check whether the Log4j Category used is enabled for <code>DEBUG</code> priority.
232      */

233     public boolean isDebugEnabled () {
234         return category.isDebugEnabled ();
235     }
236
237     /**
238      * Check whether the Log4j Category used is enabled for <code>ERROR</code> priority.
239      */

240     public boolean isErrorEnabled () {
241         return category.isEnabledFor (Priority.ERROR);
242     }
243
244     /**
245      * Check whether the Log4j Category used is enabled for <code>FATAL</code> priority.
246      */

247     public boolean isFatalEnabled () {
248         return category.isEnabledFor (Priority.FATAL);
249     }
250
251     /**
252      * Check whether the Log4j Category used is enabled for <code>INFO</code> priority.
253      */

254     public boolean isInfoEnabled () {
255         return category.isInfoEnabled ();
256     }
257
258     /**
259      * Check whether the Log4j Category used is enabled for <code>TRACE</code> priority.
260      * For Log4J, this returns the value of <code>isDebugEnabled()</code>
261      */

262     public boolean isTraceEnabled () {
263         return category.isDebugEnabled ();
264     }
265
266     /**
267      * Check whether the Log4j Category used is enabled for <code>WARN</code> priority.
268      */

269     public boolean isWarnEnabled () {
270         return category.isEnabledFor (Priority.WARN);
271     }
272 }
273
274
Popular Tags