KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > Log4JLogger


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

19 package org.apache.avalon.excalibur.logger;
20
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.log4j.Level;
23
24 /**
25  * The default Log4J wrapper class for Logger.
26  * This implementation replaces the implementation from Avalon framework.
27  * It "caches" the log levels for improved performance.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @version CVS $Revision: 1.21 $ $Date: 2004/05/04 13:08:00 $
31  */

32 public class Log4JLogger
33     implements Logger
34 {
35     
36     /**
37      * Constant for name of class to use when recording caller
38      * of log method.
39      */

40     private static final String JavaDoc FQCN = Log4JLogger.class.getName();
41
42     /** underlying implementation */
43     private final org.apache.log4j.Logger m_logger;
44
45     private final boolean m_isDebugEnabled;
46     private final boolean m_isInfoEnabled;
47     private final boolean m_isWarnEnabled;
48     private final boolean m_isErrorEnabled;
49     private final boolean m_isFatalErrorEnabled;
50
51     /**
52      * Create a logger that delegates to specified category.
53      *
54      * @param logImpl the category to delegate to
55      */

56     public Log4JLogger( final org.apache.log4j.Logger logImpl )
57     {
58         m_logger = logImpl;
59         m_isDebugEnabled = logImpl.isDebugEnabled();
60         m_isInfoEnabled = logImpl.isInfoEnabled();
61         m_isWarnEnabled = logImpl.isEnabledFor( Level.WARN );
62         m_isErrorEnabled = logImpl.isEnabledFor( Level.ERROR );
63         m_isFatalErrorEnabled = logImpl.isEnabledFor( Level.FATAL );
64     }
65
66     /**
67      * Log a debug message.
68      *
69      * @param message the message
70      */

71     public final void debug( final String JavaDoc message )
72     {
73         if ( m_isDebugEnabled )
74             m_logger.log( FQCN, Level.DEBUG, message, null );
75     }
76
77     /**
78      * Log a debug message.
79      *
80      * @param message the message
81      * @param throwable the throwable
82      */

83     public final void debug( final String JavaDoc message, final Throwable JavaDoc throwable )
84     {
85         if ( m_isDebugEnabled )
86             m_logger.log( FQCN, Level.DEBUG, message, throwable );
87     }
88
89     /**
90      * Determine if messages of priority "debug" will be logged.
91      *
92      * @return true if "debug" messages will be logged
93      */

94     public final boolean isDebugEnabled()
95     {
96         return m_isDebugEnabled;
97     }
98
99     /**
100      * Log a info message.
101      *
102      * @param message the message
103      */

104     public final void info( final String JavaDoc message )
105     {
106         if ( m_isInfoEnabled )
107             m_logger.log( FQCN, Level.INFO, message, null );
108     }
109
110     /**
111      * Log a info message.
112      *
113      * @param message the message
114      * @param throwable the throwable
115      */

116     public final void info( final String JavaDoc message, final Throwable JavaDoc throwable )
117     {
118         if ( m_isInfoEnabled )
119             m_logger.log( FQCN, Level.INFO, message, throwable );
120     }
121
122     /**
123      * Determine if messages of priority "info" will be logged.
124      *
125      * @return true if "info" messages will be logged
126      */

127     public final boolean isInfoEnabled()
128     {
129         return m_isInfoEnabled;
130     }
131
132     /**
133      * Log a warn message.
134      *
135      * @param message the message
136      */

137     public final void warn( final String JavaDoc message )
138     {
139         if ( m_isWarnEnabled )
140             m_logger.log( FQCN, Level.WARN, message, null );
141     }
142
143     /**
144      * Log a warn message.
145      *
146      * @param message the message
147      * @param throwable the throwable
148      */

149     public final void warn( final String JavaDoc message, final Throwable JavaDoc throwable )
150     {
151         if ( m_isWarnEnabled )
152             m_logger.log( FQCN, Level.WARN, message, throwable );
153     }
154
155     /**
156      * Determine if messages of priority "warn" will be logged.
157      *
158      * @return true if "warn" messages will be logged
159      */

160     public final boolean isWarnEnabled()
161     {
162         return m_isWarnEnabled;
163     }
164
165     /**
166      * Log a error message.
167      *
168      * @param message the message
169      */

170     public final void error( final String JavaDoc message )
171     {
172         if ( m_isErrorEnabled )
173             m_logger.log( FQCN, Level.ERROR, message, null );
174     }
175
176     /**
177      * Log a error message.
178      *
179      * @param message the message
180      * @param throwable the throwable
181      */

182     public final void error( final String JavaDoc message, final Throwable JavaDoc throwable )
183     {
184         if ( m_isErrorEnabled )
185             m_logger.log( FQCN, Level.ERROR, message, throwable );
186     }
187
188     /**
189      * Determine if messages of priority "error" will be logged.
190      *
191      * @return true if "error" messages will be logged
192      */

193     public final boolean isErrorEnabled()
194     {
195         return m_isErrorEnabled;
196     }
197
198     /**
199      * Log a fatalError message.
200      *
201      * @param message the message
202      */

203     public final void fatalError( final String JavaDoc message )
204     {
205         if ( m_isFatalErrorEnabled )
206             m_logger.log( FQCN, Level.FATAL, message, null );
207     }
208
209     /**
210      * Log a fatalError message.
211      *
212      * @param message the message
213      * @param throwable the throwable
214      */

215     public final void fatalError( final String JavaDoc message, final Throwable JavaDoc throwable )
216     {
217         if ( m_isFatalErrorEnabled )
218             m_logger.log( FQCN, Level.ERROR, message, throwable );
219     }
220
221     /**
222      * Determine if messages of priority "fatalError" will be logged.
223      *
224      * @return true if "fatalError" messages will be logged
225      */

226     public final boolean isFatalErrorEnabled()
227     {
228         return m_isFatalErrorEnabled;
229     }
230
231     /**
232      * Create a new child logger.
233      * The name of the child logger is [current-loggers-name].[passed-in-name]
234      * Throws <code>IllegalArgumentException</code> if name has an empty element name
235      *
236      * @param name the subname of this logger
237      * @return the new logger
238      */

239     public final Logger getChildLogger( final String JavaDoc name )
240     {
241         return new Log4JLogger( org.apache.log4j.Logger.getLogger( m_logger.getName() + "." + name ) );
242     }
243     
244 }
245
Popular Tags