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
018package org.apache.commons.logging.impl;
019
020import java.io.Serializable;
021import org.apache.log.Logger;
022import org.apache.log.Hierarchy;
023import org.apache.commons.logging.Log;
024
025/**
026 * Implementation of <code>org.apache.commons.logging.Log</code>
027 * that wraps the <a href="http://avalon.apache.org/logkit/">avalon-logkit</a>
028 * logging system. Configuration of <code>LogKit</code> is left to the user.
029 * <p>
030 * <code>LogKit</code> accepts only <code>String</code> messages.
031 * Therefore, this implementation converts object messages into strings
032 * by called their <code>toString()</code> method before logging them.
033 *
034 * @version $Id: LogKitLogger.html 915605 2014-07-09 20:22:43Z tn $
035 */
036public class LogKitLogger implements Log, Serializable {
037
038    /** Serializable version identifier. */
039    private static final long serialVersionUID = 3768538055836059519L;
040
041    // ------------------------------------------------------------- Attributes
042
043    /** Logging goes to this <code>LogKit</code> logger */
044    protected transient volatile Logger logger = null;
045
046    /** Name of this logger */
047    protected String name = null;
048
049    // ------------------------------------------------------------ Constructor
050
051    /**
052     * Construct <code>LogKitLogger</code> which wraps the <code>LogKit</code>
053     * logger with given name.
054     *
055     * @param name log name
056     */
057    public LogKitLogger(String name) {
058        this.name = name;
059        this.logger = getLogger();
060    }
061
062    // --------------------------------------------------------- Public Methods
063
064    /**
065     * Return the underlying Logger we are using.
066     */
067    public Logger getLogger() {
068        Logger result = logger;
069        if (result == null) {
070            synchronized(this) {
071                result = logger;
072                if (result == null) {
073                    logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
074                }
075            }
076        }
077        return result;
078    }
079
080    // ----------------------------------------------------- Log Implementation
081
082    /**
083     * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
084     *
085     * @param message to log
086     * @see org.apache.commons.logging.Log#trace(Object)
087    */
088    public void trace(Object message) {
089        debug(message);
090    }
091
092    /**
093     * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
094     *
095     * @param message to log
096     * @param t log this cause
097     * @see org.apache.commons.logging.Log#trace(Object, Throwable)
098     */
099    public void trace(Object message, Throwable t) {
100        debug(message, t);
101    }
102
103    /**
104     * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
105     *
106     * @param message to log
107     * @see org.apache.commons.logging.Log#debug(Object)
108     */
109    public void debug(Object message) {
110        if (message != null) {
111            getLogger().debug(String.valueOf(message));
112        }
113    }
114
115    /**
116     * Logs a message with <code>org.apache.log.Priority.DEBUG</code>.
117     *
118     * @param message to log
119     * @param t log this cause
120     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
121     */
122    public void debug(Object message, Throwable t) {
123        if (message != null) {
124            getLogger().debug(String.valueOf(message), t);
125        }
126    }
127
128    /**
129     * Logs a message with <code>org.apache.log.Priority.INFO</code>.
130     *
131     * @param message to log
132     * @see org.apache.commons.logging.Log#info(Object)
133     */
134    public void info(Object message) {
135        if (message != null) {
136            getLogger().info(String.valueOf(message));
137        }
138    }
139
140    /**
141     * Logs a message with <code>org.apache.log.Priority.INFO</code>.
142     *
143     * @param message to log
144     * @param t log this cause
145     * @see org.apache.commons.logging.Log#info(Object, Throwable)
146     */
147    public void info(Object message, Throwable t) {
148        if (message != null) {
149            getLogger().info(String.valueOf(message), t);
150        }
151    }
152
153    /**
154     * Logs a message with <code>org.apache.log.Priority.WARN</code>.
155     *
156     * @param message to log
157     * @see org.apache.commons.logging.Log#warn(Object)
158     */
159    public void warn(Object message) {
160        if (message != null) {
161            getLogger().warn(String.valueOf(message));
162        }
163    }
164
165    /**
166     * Logs a message with <code>org.apache.log.Priority.WARN</code>.
167     *
168     * @param message to log
169     * @param t log this cause
170     * @see org.apache.commons.logging.Log#warn(Object, Throwable)
171     */
172    public void warn(Object message, Throwable t) {
173        if (message != null) {
174            getLogger().warn(String.valueOf(message), t);
175        }
176    }
177
178    /**
179     * Logs a message with <code>org.apache.log.Priority.ERROR</code>.
180     *
181     * @param message to log
182     * @see org.apache.commons.logging.Log#error(Object)
183     */
184    public void error(Object message) {
185        if (message != null) {
186            getLogger().error(String.valueOf(message));
187        }
188    }
189
190    /**
191     * Logs a message with <code>org.apache.log.Priority.ERROR</code>.
192     *
193     * @param message to log
194     * @param t log this cause
195     * @see org.apache.commons.logging.Log#error(Object, Throwable)
196     */
197    public void error(Object message, Throwable t) {
198        if (message != null) {
199            getLogger().error(String.valueOf(message), t);
200        }
201    }
202
203    /**
204     * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>.
205     *
206     * @param message to log
207     * @see org.apache.commons.logging.Log#fatal(Object)
208     */
209    public void fatal(Object message) {
210        if (message != null) {
211            getLogger().fatalError(String.valueOf(message));
212        }
213    }
214
215    /**
216     * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>.
217     *
218     * @param message to log
219     * @param t log this cause
220     * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
221     */
222    public void fatal(Object message, Throwable t) {
223        if (message != null) {
224            getLogger().fatalError(String.valueOf(message), t);
225        }
226    }
227
228    /**
229     * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
230     */
231    public boolean isDebugEnabled() {
232        return getLogger().isDebugEnabled();
233    }
234
235    /**
236     * Checks whether the <code>LogKit</code> logger will log messages of priority <code>ERROR</code>.
237     */
238    public boolean isErrorEnabled() {
239        return getLogger().isErrorEnabled();
240    }
241
242    /**
243     * Checks whether the <code>LogKit</code> logger will log messages of priority <code>FATAL_ERROR</code>.
244     */
245    public boolean isFatalEnabled() {
246        return getLogger().isFatalErrorEnabled();
247    }
248
249    /**
250     * Checks whether the <code>LogKit</code> logger will log messages of priority <code>INFO</code>.
251     */
252    public boolean isInfoEnabled() {
253        return getLogger().isInfoEnabled();
254    }
255
256    /**
257     * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>.
258     */
259    public boolean isTraceEnabled() {
260        return getLogger().isDebugEnabled();
261    }
262
263    /**
264     * Checks whether the <code>LogKit</code> logger will log messages of priority <code>WARN</code>.
265     */
266    public boolean isWarnEnabled() {
267        return getLogger().isWarnEnabled();
268    }
269}