1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.commons.logging.impl;
18
19 import java.io.ObjectStreamException;
20 import java.io.Serializable;
21
22 import org.apache.commons.logging.Log;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27 * Implementation of {@link Log org.apache.commons.logging.Log} interface which
28 * delegates all processing to a wrapped {@link Logger org.slf4j.Logger} instance.
29 *
30 * <p>JCL's FATAL and TRACE levels are mapped to ERROR and DEBUG respectively. All
31 * other levels map one to one.
32 *
33 * @author Ceki Gülcü
34 */
35 public class SLF4JLog implements Log, Serializable {
36
37 private static final long serialVersionUID = 680728617011167209L;
38
39 //used to store this logger's name to recreate it after serialization
40 protected String name;
41
42 // in both Log4jLogger and Jdk14Logger classes in the original JCL, the
43 // logger instance is transient
44 private transient Logger logger;
45
46 SLF4JLog(Logger logger) {
47 this.logger = logger;
48 this.name = logger.getName();
49 }
50
51 /**
52 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
53 */
54 public boolean isDebugEnabled() {
55 return logger.isDebugEnabled();
56 }
57
58 /**
59 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
60 */
61 public boolean isErrorEnabled() {
62 return logger.isErrorEnabled();
63 }
64
65 /**
66 * Delegates to the <code>isErrorEnabled<code> method of the wrapped
67 * <code>org.slf4j.Logger</code> instance.
68 */
69 public boolean isFatalEnabled() {
70 return logger.isErrorEnabled();
71 }
72
73 /**
74 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
75 */
76 public boolean isInfoEnabled() {
77 return logger.isInfoEnabled();
78 }
79
80 /**
81 * Delegates to the <code>isDebugEnabled<code> method of the wrapped
82 * <code>org.slf4j.Logger</code> instance.
83 */
84 public boolean isTraceEnabled() {
85 return logger.isTraceEnabled();
86 }
87
88 /**
89 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance.
90 */
91 public boolean isWarnEnabled() {
92 return logger.isWarnEnabled();
93 }
94
95 /**
96 * Converts the input parameter to String and then delegates to
97 * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
98 *
99 * @param message the message to log. Converted to {@link String}
100 */
101 public void trace(Object message) {
102 logger.trace(String.valueOf(message));
103 }
104
105 /**
106 * Converts the first input parameter to String and then delegates to
107 * the debug method of the wrapped <code>org.slf4j.Logger</code> instance.
108 *
109 * @param message the message to log. Converted to {@link String}
110 * @param t the exception to log
111 */
112 public void trace(Object message, Throwable t) {
113 logger.trace(String.valueOf(message), t);
114 }
115
116 /**
117 * Converts the input parameter to String and then delegates to the wrapped
118 * <code>org.slf4j.Logger</code> instance.
119 *
120 * @param message the message to log. Converted to {@link String}
121 */
122 public void debug(Object message) {
123 logger.debug(String.valueOf(message));
124 }
125
126 /**
127 * Converts the first input parameter to String and then delegates to
128 * the wrapped <code>org.slf4j.Logger</code> instance.
129 *
130 * @param message the message to log. Converted to {@link String}
131 * @param t the exception to log
132 */
133 public void debug(Object message, Throwable t) {
134 logger.debug(String.valueOf(message), t);
135 }
136
137 /**
138 * Converts the input parameter to String and then delegates to the wrapped
139 * <code>org.slf4j.Logger</code> instance.
140 *
141 * @param message the message to log. Converted to {@link String}
142 */
143 public void info(Object message) {
144 logger.info(String.valueOf(message));
145 }
146
147 /**
148 * Converts the first input parameter to String and then delegates to
149 * the wrapped <code>org.slf4j.Logger</code> instance.
150 *
151 * @param message the message to log. Converted to {@link String}
152 * @param t the exception to log
153 */
154 public void info(Object message, Throwable t) {
155 logger.info(String.valueOf(message), t);
156 }
157
158 /**
159 * Converts the input parameter to String and then delegates to the wrapped
160 * <code>org.slf4j.Logger</code> instance.
161 *
162 * @param message the message to log. Converted to {@link String}
163 */
164 public void warn(Object message) {
165 logger.warn(String.valueOf(message));
166 }
167
168 /**
169 * Converts the first input parameter to String and then delegates to
170 * the wrapped <code>org.slf4j.Logger</code> instance.
171 *
172 * @param message the message to log. Converted to {@link String}
173 * @param t the exception to log
174 */
175 public void warn(Object message, Throwable t) {
176 logger.warn(String.valueOf(message), t);
177 }
178
179 /**
180 * Converts the input parameter to String and then delegates to the wrapped
181 * <code>org.slf4j.Logger</code> instance.
182 *
183 * @param message the message to log. Converted to {@link String}
184 */
185 public void error(Object message) {
186 logger.error(String.valueOf(message));
187 }
188
189 /**
190 * Converts the first input parameter to String and then delegates to
191 * the wrapped <code>org.slf4j.Logger</code> instance.
192 *
193 * @param message the message to log. Converted to {@link String}
194 * @param t the exception to log
195 */
196 public void error(Object message, Throwable t) {
197 logger.error(String.valueOf(message), t);
198 }
199
200
201
202 /**
203 * Converts the input parameter to String and then delegates to
204 * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
205 *
206 * @param message the message to log. Converted to {@link String}
207 */
208 public void fatal(Object message) {
209 logger.error(String.valueOf(message));
210 }
211
212 /**
213 * Converts the first input parameter to String and then delegates to
214 * the error method of the wrapped <code>org.slf4j.Logger</code> instance.
215 *
216 * @param message the message to log. Converted to {@link String}
217 * @param t the exception to log
218 */
219 public void fatal(Object message, Throwable t) {
220 logger.error(String.valueOf(message), t);
221 }
222
223 /**
224 * Replace this instance with a homonymous (same name) logger returned by
225 * LoggerFactory. Note that this method is only called during deserialization.
226 *
227 * @return logger with same name as returned by LoggerFactory
228 * @throws ObjectStreamException
229 */
230 protected Object readResolve() throws ObjectStreamException {
231 Logger logger = LoggerFactory.getLogger(this.name);
232 return new SLF4JLog(logger);
233 }
234 }