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 import org.slf4j.spi.LocationAwareLogger; 26 27 /** 28 * Implementation of {@link Log org.apache.commons.logging.Log} interface which 29 * delegates all processing to a wrapped {@link Logger org.slf4j.Logger} 30 * instance. 31 * 32 * <p> 33 * JCL's FATAL level is mapped to ERROR. All other levels map one to one. 34 * 35 * @author Ceki Gülcü 36 */ 37 public class SLF4JLocationAwareLog implements Log, Serializable { 38 39 private static final long serialVersionUID = -2379157579039314822L; 40 41 // used to store this logger's name to recreate it after serialization 42 protected String name; 43 44 // in both Log4jLogger and Jdk14Logger classes in the original JCL, the 45 // logger instance is transient 46 private transient LocationAwareLogger logger; 47 48 private static final String FQCN = SLF4JLocationAwareLog.class.getName(); 49 50 SLF4JLocationAwareLog(LocationAwareLogger logger) { 51 this.logger = logger; 52 this.name = logger.getName(); 53 } 54 55 /** 56 * Delegates to the <code>isTraceEnabled<code> method of the wrapped 57 * <code>org.slf4j.Logger</code> instance. 58 */ 59 public boolean isTraceEnabled() { 60 return logger.isTraceEnabled(); 61 } 62 63 /** 64 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 65 */ 66 public boolean isDebugEnabled() { 67 return logger.isDebugEnabled(); 68 } 69 70 /** 71 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 72 */ 73 public boolean isInfoEnabled() { 74 return logger.isInfoEnabled(); 75 } 76 77 /** 78 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 79 */ 80 public boolean isWarnEnabled() { 81 return logger.isWarnEnabled(); 82 } 83 84 /** 85 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 86 */ 87 public boolean isErrorEnabled() { 88 return logger.isErrorEnabled(); 89 } 90 91 /** 92 * Delegates to the <code>isErrorEnabled<code> method of the wrapped 93 * <code>org.slf4j.Logger</code> instance. 94 */ 95 public boolean isFatalEnabled() { 96 return logger.isErrorEnabled(); 97 } 98 99 /** 100 * Converts the input parameter to String and then delegates to the debug 101 * method of the wrapped <code>org.slf4j.Logger</code> instance. 102 * 103 * @param message 104 * the message to log. Converted to {@link String} 105 */ 106 public void trace(Object message) { 107 logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, null); 108 } 109 110 /** 111 * Converts the first input parameter to String and then delegates to the 112 * debug method of the wrapped <code>org.slf4j.Logger</code> instance. 113 * 114 * @param message 115 * the message to log. Converted to {@link String} 116 * @param t 117 * the exception to log 118 */ 119 public void trace(Object message, Throwable t) { 120 logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, t); 121 } 122 123 /** 124 * Converts the input parameter to String and then delegates to the wrapped 125 * <code>org.slf4j.Logger</code> instance. 126 * 127 * @param message 128 * the message to log. Converted to {@link String} 129 */ 130 public void debug(Object message) { 131 logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, null); 132 } 133 134 /** 135 * Converts the first input parameter to String and then delegates to the 136 * wrapped <code>org.slf4j.Logger</code> instance. 137 * 138 * @param message 139 * the message to log. Converted to {@link String} 140 * @param t 141 * the exception to log 142 */ 143 public void debug(Object message, Throwable t) { 144 logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, t); 145 } 146 147 /** 148 * Converts the input parameter to String and then delegates to the wrapped 149 * <code>org.slf4j.Logger</code> instance. 150 * 151 * @param message 152 * the message to log. Converted to {@link String} 153 */ 154 public void info(Object message) { 155 logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, null); 156 } 157 158 /** 159 * Converts the first input parameter to String and then delegates to the 160 * wrapped <code>org.slf4j.Logger</code> instance. 161 * 162 * @param message 163 * the message to log. Converted to {@link String} 164 * @param t 165 * the exception to log 166 */ 167 public void info(Object message, Throwable t) { 168 logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, t); 169 } 170 171 /** 172 * Converts the input parameter to String and then delegates to the wrapped 173 * <code>org.slf4j.Logger</code> instance. 174 * 175 * @param message 176 * the message to log. Converted to {@link String} 177 */ 178 public void warn(Object message) { 179 logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, null); 180 } 181 182 /** 183 * Converts the first input parameter to String and then delegates to the 184 * wrapped <code>org.slf4j.Logger</code> instance. 185 * 186 * @param message 187 * the message to log. Converted to {@link String} 188 * @param t 189 * the exception to log 190 */ 191 public void warn(Object message, Throwable t) { 192 logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, t); 193 } 194 195 /** 196 * Converts the input parameter to String and then delegates to the wrapped 197 * <code>org.slf4j.Logger</code> instance. 198 * 199 * @param message 200 * the message to log. Converted to {@link String} 201 */ 202 public void error(Object message) { 203 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null); 204 } 205 206 /** 207 * Converts the first input parameter to String and then delegates to the 208 * wrapped <code>org.slf4j.Logger</code> instance. 209 * 210 * @param message 211 * the message to log. Converted to {@link String} 212 * @param t 213 * the exception to log 214 */ 215 public void error(Object message, Throwable t) { 216 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t); 217 } 218 219 /** 220 * Converts the input parameter to String and then delegates to the error 221 * method of the wrapped <code>org.slf4j.Logger</code> instance. 222 * 223 * @param message 224 * the message to log. Converted to {@link String} 225 */ 226 public void fatal(Object message) { 227 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null); 228 } 229 230 /** 231 * Converts the first input parameter to String and then delegates to the 232 * error method of the wrapped <code>org.slf4j.Logger</code> instance. 233 * 234 * @param message 235 * the message to log. Converted to {@link String} 236 * @param t 237 * the exception to log 238 */ 239 public void fatal(Object message, Throwable t) { 240 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t); 241 } 242 243 /** 244 * Replace this instance with a homonymous (same name) logger returned by 245 * LoggerFactory. Note that this method is only called during deserialization. 246 * 247 * @return logger with same name as returned by LoggerFactory 248 * @throws ObjectStreamException 249 */ 250 protected Object readResolve() throws ObjectStreamException { 251 Logger logger = LoggerFactory.getLogger(this.name); 252 return new SLF4JLocationAwareLog((LocationAwareLogger) logger); 253 } 254 }