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 implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.logging.impl;
19
20 import org.apache.avalon.framework.logger.Logger;
21 import org.apache.commons.logging.Log;
22
23 /**
24 * Implementation of commons-logging Log interface that delegates all
25 * logging calls to the Avalon logging abstraction: the Logger interface.
26 * <p>
27 * There are two ways in which this class can be used:
28 * <ul>
29 * <li>the instance can be constructed with an Avalon logger
30 * (by calling {@link #AvalonLogger(Logger)}). In this case, it acts
31 * as a simple thin wrapping implementation over the logger. This is
32 * particularly useful when using a property setter.
33 * </li>
34 * <li>the {@link #setDefaultLogger} class property can be called which
35 * sets the ancestral Avalon logger for this class. Any <code>AvalonLogger</code>
36 * instances created through the <code>LogFactory</code> mechanisms will output
37 * to child loggers of this <code>Logger</code>.
38 * </li>
39 * </ul>
40 * <p>
41 * <strong>Note:</strong> <code>AvalonLogger</code> does not implement Serializable
42 * because the constructors available for it make this impossible to achieve in all
43 * circumstances; there is no way to "reconnect" to an underlying Logger object on
44 * deserialization if one was just passed in to the constructor of the original
45 * object. This class <i>was</i> marked Serializable in the 1.0.4 release of
46 * commons-logging, but this never actually worked (a NullPointerException would
47 * be thrown as soon as the deserialized object was used), so removing this marker
48 * is not considered to be an incompatible change.
49 *
50 * @version $Id: AvalonLogger.java 1435115 2013-01-18 12:40:19Z tn $
51 */
52 public class AvalonLogger implements Log {
53
54 /** Ancestral Avalon logger. */
55 private static volatile Logger defaultLogger = null;
56 /** Avalon logger used to perform log. */
57 private final transient Logger logger;
58
59 /**
60 * Constructs an <code>AvalonLogger</code> that outputs to the given
61 * <code>Logger</code> instance.
62 *
63 * @param logger the Avalon logger implementation to delegate to
64 */
65 public AvalonLogger(Logger logger) {
66 this.logger = logger;
67 }
68
69 /**
70 * Constructs an <code>AvalonLogger</code> that will log to a child
71 * of the <code>Logger</code> set by calling {@link #setDefaultLogger}.
72 *
73 * @param name the name of the avalon logger implementation to delegate to
74 */
75 public AvalonLogger(String name) {
76 if (defaultLogger == null) {
77 throw new NullPointerException("default logger has to be specified if this constructor is used!");
78 }
79 this.logger = defaultLogger.getChildLogger(name);
80 }
81
82 /**
83 * Gets the Avalon logger implementation used to perform logging.
84 *
85 * @return avalon logger implementation
86 */
87 public Logger getLogger() {
88 return logger;
89 }
90
91 /**
92 * Sets the ancestral Avalon logger from which the delegating loggers will descend.
93 *
94 * @param logger the default avalon logger,
95 * in case there is no logger instance supplied in constructor
96 */
97 public static void setDefaultLogger(Logger logger) {
98 defaultLogger = logger;
99 }
100
101 /**
102 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.debug</code>.
103 *
104 * @param message to log
105 * @param t log this cause
106 * @see org.apache.commons.logging.Log#debug(Object, Throwable)
107 */
108 public void debug(Object message, Throwable t) {
109 if (getLogger().isDebugEnabled()) {
110 getLogger().debug(String.valueOf(message), t);
111 }
112 }
113
114 /**
115 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.debug</code>.
116 *
117 * @param message to log.
118 * @see org.apache.commons.logging.Log#debug(Object)
119 */
120 public void debug(Object message) {
121 if (getLogger().isDebugEnabled()) {
122 getLogger().debug(String.valueOf(message));
123 }
124 }
125
126 /**
127 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.error</code>.
128 *
129 * @param message to log
130 * @param t log this cause
131 * @see org.apache.commons.logging.Log#error(Object, Throwable)
132 */
133 public void error(Object message, Throwable t) {
134 if (getLogger().isErrorEnabled()) {
135 getLogger().error(String.valueOf(message), t);
136 }
137 }
138
139 /**
140 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.error</code>.
141 *
142 * @param message to log
143 * @see org.apache.commons.logging.Log#error(Object)
144 */
145 public void error(Object message) {
146 if (getLogger().isErrorEnabled()) {
147 getLogger().error(String.valueOf(message));
148 }
149 }
150
151 /**
152 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
153 *
154 * @param message to log.
155 * @param t log this cause.
156 * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
157 */
158 public void fatal(Object message, Throwable t) {
159 if (getLogger().isFatalErrorEnabled()) {
160 getLogger().fatalError(String.valueOf(message), t);
161 }
162 }
163
164 /**
165 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.fatalError</code>.
166 *
167 * @param message to log
168 * @see org.apache.commons.logging.Log#fatal(Object)
169 */
170 public void fatal(Object message) {
171 if (getLogger().isFatalErrorEnabled()) {
172 getLogger().fatalError(String.valueOf(message));
173 }
174 }
175
176 /**
177 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.info</code>.
178 *
179 * @param message to log
180 * @param t log this cause
181 * @see org.apache.commons.logging.Log#info(Object, Throwable)
182 */
183 public void info(Object message, Throwable t) {
184 if (getLogger().isInfoEnabled()) {
185 getLogger().info(String.valueOf(message), t);
186 }
187 }
188
189 /**
190 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.info</code>.
191 *
192 * @param message to log
193 * @see org.apache.commons.logging.Log#info(Object)
194 */
195 public void info(Object message) {
196 if (getLogger().isInfoEnabled()) {
197 getLogger().info(String.valueOf(message));
198 }
199 }
200
201 /**
202 * Is logging to <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
203 * @see org.apache.commons.logging.Log#isDebugEnabled()
204 */
205 public boolean isDebugEnabled() {
206 return getLogger().isDebugEnabled();
207 }
208
209 /**
210 * Is logging to <code>org.apache.avalon.framework.logger.Logger.error</code> enabled?
211 * @see org.apache.commons.logging.Log#isErrorEnabled()
212 */
213 public boolean isErrorEnabled() {
214 return getLogger().isErrorEnabled();
215 }
216
217 /**
218 * Is logging to <code>org.apache.avalon.framework.logger.Logger.fatalError</code> enabled?
219 * @see org.apache.commons.logging.Log#isFatalEnabled()
220 */
221 public boolean isFatalEnabled() {
222 return getLogger().isFatalErrorEnabled();
223 }
224
225 /**
226 * Is logging to <code>org.apache.avalon.framework.logger.Logger.info</code> enabled?
227 * @see org.apache.commons.logging.Log#isInfoEnabled()
228 */
229 public boolean isInfoEnabled() {
230 return getLogger().isInfoEnabled();
231 }
232
233 /**
234 * Is logging to <code>org.apache.avalon.framework.logger.Logger.debug</code> enabled?
235 * @see org.apache.commons.logging.Log#isTraceEnabled()
236 */
237 public boolean isTraceEnabled() {
238 return getLogger().isDebugEnabled();
239 }
240
241 /**
242 * Is logging to <code>org.apache.avalon.framework.logger.Logger.warn</code> enabled?
243 * @see org.apache.commons.logging.Log#isWarnEnabled()
244 */
245 public boolean isWarnEnabled() {
246 return getLogger().isWarnEnabled();
247 }
248
249 /**
250 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.debug</code>.
251 *
252 * @param message to log.
253 * @param t log this cause.
254 * @see org.apache.commons.logging.Log#trace(Object, Throwable)
255 */
256 public void trace(Object message, Throwable t) {
257 if (getLogger().isDebugEnabled()) {
258 getLogger().debug(String.valueOf(message), t);
259 }
260 }
261
262 /**
263 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.debug</code>.
264 *
265 * @param message to log
266 * @see org.apache.commons.logging.Log#trace(Object)
267 */
268 public void trace(Object message) {
269 if (getLogger().isDebugEnabled()) {
270 getLogger().debug(String.valueOf(message));
271 }
272 }
273
274 /**
275 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.warn</code>.
276 *
277 * @param message to log
278 * @param t log this cause
279 * @see org.apache.commons.logging.Log#warn(Object, Throwable)
280 */
281 public void warn(Object message, Throwable t) {
282 if (getLogger().isWarnEnabled()) {
283 getLogger().warn(String.valueOf(message), t);
284 }
285 }
286
287 /**
288 * Logs a message with <code>org.apache.avalon.framework.logger.Logger.warn</code>.
289 *
290 * @param message to log
291 * @see org.apache.commons.logging.Log#warn(Object)
292 */
293 public void warn(Object message) {
294 if (getLogger().isWarnEnabled()) {
295 getLogger().warn(String.valueOf(message));
296 }
297 }
298 }