001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    
019    package org.apache.commons.logging.impl;
020    
021    import java.io.Serializable;
022    import org.apache.commons.logging.Log;
023    import org.apache.log4j.Logger;
024    import org.apache.log4j.Priority;
025    import org.apache.log4j.Level;
026    
027    /**
028     * Implementation of {@link Log} that maps directly to a
029     * <strong>Logger</strong> for log4J version 1.2.
030     * <p>
031     * Initial configuration of the corresponding Logger instances should be done
032     * in the usual manner, as outlined in the Log4J documentation.
033     * <p>
034     * The reason this logger is distinct from the 1.3 logger is that in version 1.2
035     * of Log4J:
036     * <ul>
037     * <li>class Logger takes Priority parameters not Level parameters.
038     * <li>class Level extends Priority
039     * </ul>
040     * Log4J1.3 is expected to change Level so it no longer extends Priority, which is
041     * a non-binary-compatible change. The class generated by compiling this code against
042     * log4j 1.2 will therefore not run against log4j 1.3.
043     *
044     * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
045     * @author Rod Waldhoff
046     * @author Robert Burrell Donkin
047     * @version $Id: Log4JLogger.java 479747 2006-11-27 20:15:01Z dennisl $
048     */
049    
050    public class Log4JLogger implements Log, Serializable {
051    
052        // ------------------------------------------------------------- Attributes
053    
054        /** The fully qualified name of the Log4JLogger class. */
055        private static final String FQCN = Log4JLogger.class.getName();
056        
057        /** Log to this logger */
058        private transient Logger logger = null;
059    
060        /** Logger name */
061        private String name = null;
062    
063        private static Priority traceLevel;
064        
065        // ------------------------------------------------------------
066        // Static Initializer.
067        //
068        // Note that this must come after the static variable declarations
069        // otherwise initialiser expressions associated with those variables
070        // will override any settings done here.
071        //
072        // Verify that log4j is available, and that it is version 1.2.
073        // If an ExceptionInInitializerError is generated, then LogFactoryImpl
074        // will treat that as meaning that the appropriate underlying logging
075        // library is just not present - if discovery is in progress then
076        // discovery will continue.
077        // ------------------------------------------------------------
078    
079        static {
080            if (!Priority.class.isAssignableFrom(Level.class)) {
081                // nope, this is log4j 1.3, so force an ExceptionInInitializerError
082                throw new InstantiationError("Log4J 1.2 not available");
083            }
084            
085            // Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier
086            // versions do not. If TRACE is not available, then we have to map
087            // calls to Log.trace(...) onto the DEBUG level.
088            
089            try {
090                traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
091            } catch(Exception ex) {
092                // ok, trace not available
093                traceLevel = Priority.DEBUG;
094            }
095        }
096    
097        
098        // ------------------------------------------------------------ Constructor
099    
100        public Log4JLogger() {
101        }
102    
103    
104        /**
105         * Base constructor.
106         */
107        public Log4JLogger(String name) {
108            this.name = name;
109            this.logger = getLogger();
110        }
111    
112        /** 
113         * For use with a log4j factory.
114         */
115        public Log4JLogger(Logger logger ) {
116            if (logger == null) {
117                throw new IllegalArgumentException(
118                    "Warning - null logger in constructor; possible log4j misconfiguration.");
119            }
120            this.name = logger.getName();
121            this.logger=logger;
122        }
123    
124    
125        // --------------------------------------------------------- 
126        // Implementation
127        //
128        // Note that in the methods below the Priority class is used to define
129        // levels even though the Level class is supported in 1.2. This is done
130        // so that at compile time the call definitely resolves to a call to
131        // a method that takes a Priority rather than one that takes a Level.
132        // 
133        // The Category class (and hence its subclass Logger) in version 1.2 only
134        // has methods that take Priority objects. The Category class (and hence
135        // Logger class) in version 1.3 has methods that take both Priority and
136        // Level objects. This means that if we use Level here, and compile
137        // against log4j 1.3 then calls would be bound to the versions of
138        // methods taking Level objects and then would fail to run against
139        // version 1.2 of log4j.
140        // --------------------------------------------------------- 
141    
142    
143        /**
144         * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>.
145         * When using a log4j version that does not support the <code>TRACE</code>
146         * level, the message will be logged at the <code>DEBUG</code> level.
147         *
148         * @param message to log
149         * @see org.apache.commons.logging.Log#trace(Object)
150         */
151        public void trace(Object message) {
152            getLogger().log(FQCN, traceLevel, message, null );
153        }
154    
155    
156        /**
157         * Logs a message with <code>org.apache.log4j.Priority.TRACE</code>.
158         * When using a log4j version that does not support the <code>TRACE</code>
159         * level, the message will be logged at the <code>DEBUG</code> level.
160         *
161         * @param message to log
162         * @param t log this cause
163         * @see org.apache.commons.logging.Log#trace(Object, Throwable)
164         */
165        public void trace(Object message, Throwable t) {
166            getLogger().log(FQCN, traceLevel, message, t );
167        }
168    
169    
170        /**
171         * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>.
172         *
173         * @param message to log
174         * @see org.apache.commons.logging.Log#debug(Object)
175         */
176        public void debug(Object message) {
177            getLogger().log(FQCN, Priority.DEBUG, message, null );
178        }
179    
180        /**
181         * Logs a message with <code>org.apache.log4j.Priority.DEBUG</code>.
182         *
183         * @param message to log
184         * @param t log this cause
185         * @see org.apache.commons.logging.Log#debug(Object, Throwable)
186         */
187        public void debug(Object message, Throwable t) {
188            getLogger().log(FQCN, Priority.DEBUG, message, t );
189        }
190    
191    
192        /**
193         * Logs a message with <code>org.apache.log4j.Priority.INFO</code>.
194         *
195         * @param message to log
196         * @see org.apache.commons.logging.Log#info(Object)
197         */
198        public void info(Object message) {
199            getLogger().log(FQCN, Priority.INFO, message, null );
200        }
201    
202    
203        /**
204         * Logs a message with <code>org.apache.log4j.Priority.INFO</code>.
205         *
206         * @param message to log
207         * @param t log this cause
208         * @see org.apache.commons.logging.Log#info(Object, Throwable)
209         */
210        public void info(Object message, Throwable t) {
211            getLogger().log(FQCN, Priority.INFO, message, t );
212        }
213    
214    
215        /**
216         * Logs a message with <code>org.apache.log4j.Priority.WARN</code>.
217         *
218         * @param message to log
219         * @see org.apache.commons.logging.Log#warn(Object)
220         */
221        public void warn(Object message) {
222            getLogger().log(FQCN, Priority.WARN, message, null );
223        }
224    
225    
226        /**
227         * Logs a message with <code>org.apache.log4j.Priority.WARN</code>.
228         *
229         * @param message to log
230         * @param t log this cause
231         * @see org.apache.commons.logging.Log#warn(Object, Throwable)
232         */
233        public void warn(Object message, Throwable t) {
234            getLogger().log(FQCN, Priority.WARN, message, t );
235        }
236    
237    
238        /**
239         * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>.
240         *
241         * @param message to log
242         * @see org.apache.commons.logging.Log#error(Object)
243         */
244        public void error(Object message) {
245            getLogger().log(FQCN, Priority.ERROR, message, null );
246        }
247    
248    
249        /**
250         * Logs a message with <code>org.apache.log4j.Priority.ERROR</code>.
251         *
252         * @param message to log
253         * @param t log this cause
254         * @see org.apache.commons.logging.Log#error(Object, Throwable)
255         */
256        public void error(Object message, Throwable t) {
257            getLogger().log(FQCN, Priority.ERROR, message, t );
258        }
259    
260    
261        /**
262         * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>.
263         *
264         * @param message to log
265         * @see org.apache.commons.logging.Log#fatal(Object)
266         */
267        public void fatal(Object message) {
268            getLogger().log(FQCN, Priority.FATAL, message, null );
269        }
270    
271    
272        /**
273         * Logs a message with <code>org.apache.log4j.Priority.FATAL</code>.
274         *
275         * @param message to log
276         * @param t log this cause
277         * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
278         */
279        public void fatal(Object message, Throwable t) {
280            getLogger().log(FQCN, Priority.FATAL, message, t );
281        }
282    
283    
284        /**
285         * Return the native Logger instance we are using.
286         */
287        public Logger getLogger() {
288            if (logger == null) {
289                logger = Logger.getLogger(name);
290            }
291            return (this.logger);
292        }
293    
294    
295        /**
296         * Check whether the Log4j Logger used is enabled for <code>DEBUG</code> priority.
297         */
298        public boolean isDebugEnabled() {
299            return getLogger().isDebugEnabled();
300        }
301    
302    
303         /**
304         * Check whether the Log4j Logger used is enabled for <code>ERROR</code> priority.
305         */
306        public boolean isErrorEnabled() {
307            return getLogger().isEnabledFor(Priority.ERROR);
308        }
309    
310    
311        /**
312         * Check whether the Log4j Logger used is enabled for <code>FATAL</code> priority.
313         */
314        public boolean isFatalEnabled() {
315            return getLogger().isEnabledFor(Priority.FATAL);
316        }
317    
318    
319        /**
320         * Check whether the Log4j Logger used is enabled for <code>INFO</code> priority.
321         */
322        public boolean isInfoEnabled() {
323            return getLogger().isInfoEnabled();
324        }
325    
326    
327        /**
328         * Check whether the Log4j Logger used is enabled for <code>TRACE</code> priority.
329         * When using a log4j version that does not support the TRACE level, this call
330         * will report whether <code>DEBUG</code> is enabled or not.
331         */
332        public boolean isTraceEnabled() {
333            return getLogger().isEnabledFor(traceLevel);
334        }
335    
336        /**
337         * Check whether the Log4j Logger used is enabled for <code>WARN</code> priority.
338         */
339        public boolean isWarnEnabled() {
340            return getLogger().isEnabledFor(Priority.WARN);
341        }
342    }