001/* 002 * Copyright 2001-2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.apache.commons.logging.impl; 018 019import java.io.ObjectStreamException; 020import java.io.Serializable; 021 022import org.apache.commons.logging.Log; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025import org.slf4j.spi.LocationAwareLogger; 026 027/** 028 * Implementation of {@link Log org.apache.commons.logging.Log} interface which 029 * delegates all processing to a wrapped {@link Logger org.slf4j.Logger} 030 * instance. 031 * 032 * <p> 033 * JCL's FATAL level is mapped to ERROR. All other levels map one to one. 034 * 035 * @author Ceki Gülcü 036 */ 037public class SLF4JLocationAwareLog implements Log, Serializable { 038 039 private static final long serialVersionUID = -2379157579039314822L; 040 041 // used to store this logger's name to recreate it after serialization 042 protected String name; 043 044 // in both Log4jLogger and Jdk14Logger classes in the original JCL, the 045 // logger instance is transient 046 private final transient LocationAwareLogger logger; 047 048 private static final String FQCN = SLF4JLocationAwareLog.class.getName(); 049 050 public SLF4JLocationAwareLog(LocationAwareLogger logger) { 051 this.logger = logger; 052 this.name = logger.getName(); 053 } 054 055 /** 056 * Delegates to the <code>isTraceEnabled<code> method of the wrapped 057 * <code>org.slf4j.Logger</code> instance. 058 */ 059 public boolean isTraceEnabled() { 060 return logger.isTraceEnabled(); 061 } 062 063 /** 064 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 065 */ 066 public boolean isDebugEnabled() { 067 return logger.isDebugEnabled(); 068 } 069 070 /** 071 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 072 */ 073 public boolean isInfoEnabled() { 074 return logger.isInfoEnabled(); 075 } 076 077 /** 078 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 079 */ 080 public boolean isWarnEnabled() { 081 return logger.isWarnEnabled(); 082 } 083 084 /** 085 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 086 */ 087 public boolean isErrorEnabled() { 088 return logger.isErrorEnabled(); 089 } 090 091 /** 092 * Delegates to the <code>isErrorEnabled<code> method of the wrapped 093 * <code>org.slf4j.Logger</code> instance. 094 */ 095 public boolean isFatalEnabled() { 096 return logger.isErrorEnabled(); 097 } 098 099 /** 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 if (isTraceEnabled()) { 108 logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, null); 109 } 110 } 111 112 /** 113 * Converts the first input parameter to String and then delegates to the 114 * debug method of the wrapped <code>org.slf4j.Logger</code> instance. 115 * 116 * @param message 117 * the message to log. Converted to {@link String} 118 * @param t 119 * the exception to log 120 */ 121 public void trace(Object message, Throwable t) { 122 if (isTraceEnabled()) { 123 logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, String.valueOf(message), null, t); 124 } 125 } 126 127 /** 128 * Converts the input parameter to String and then delegates to the wrapped 129 * <code>org.slf4j.Logger</code> instance. 130 * 131 * @param message 132 * the message to log. Converted to {@link String} 133 */ 134 public void debug(Object message) { 135 if (isDebugEnabled()) { 136 logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, null); 137 } 138 } 139 140 /** 141 * Converts the first input parameter to String and then delegates to the 142 * wrapped <code>org.slf4j.Logger</code> instance. 143 * 144 * @param message 145 * the message to log. Converted to {@link String} 146 * @param t 147 * the exception to log 148 */ 149 public void debug(Object message, Throwable t) { 150 if (isDebugEnabled()) { 151 logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, String.valueOf(message), null, t); 152 } 153 } 154 155 /** 156 * Converts the input parameter to String and then delegates to the wrapped 157 * <code>org.slf4j.Logger</code> instance. 158 * 159 * @param message 160 * the message to log. Converted to {@link String} 161 */ 162 public void info(Object message) { 163 if (isInfoEnabled()) { 164 logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, null); 165 } 166 } 167 168 /** 169 * Converts the first input parameter to String and then delegates to the 170 * wrapped <code>org.slf4j.Logger</code> instance. 171 * 172 * @param message 173 * the message to log. Converted to {@link String} 174 * @param t 175 * the exception to log 176 */ 177 public void info(Object message, Throwable t) { 178 if (isInfoEnabled()) { 179 logger.log(null, FQCN, LocationAwareLogger.INFO_INT, String.valueOf(message), null, t); 180 } 181 } 182 183 /** 184 * Converts the input parameter to String and then delegates to the wrapped 185 * <code>org.slf4j.Logger</code> instance. 186 * 187 * @param message 188 * the message to log. Converted to {@link String} 189 */ 190 public void warn(Object message) { 191 if (isWarnEnabled()) { 192 logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, null); 193 } 194 } 195 196 /** 197 * Converts the first input parameter to String and then delegates to the 198 * wrapped <code>org.slf4j.Logger</code> instance. 199 * 200 * @param message 201 * the message to log. Converted to {@link String} 202 * @param t 203 * the exception to log 204 */ 205 public void warn(Object message, Throwable t) { 206 if (isWarnEnabled()) { 207 logger.log(null, FQCN, LocationAwareLogger.WARN_INT, String.valueOf(message), null, t); 208 } 209 } 210 211 /** 212 * Converts the input parameter to String and then delegates to the wrapped 213 * <code>org.slf4j.Logger</code> instance. 214 * 215 * @param message 216 * the message to log. Converted to {@link String} 217 */ 218 public void error(Object message) { 219 if (isErrorEnabled()) { 220 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null); 221 } 222 } 223 224 /** 225 * Converts the first input parameter to String and then delegates to the 226 * wrapped <code>org.slf4j.Logger</code> instance. 227 * 228 * @param message 229 * the message to log. Converted to {@link String} 230 * @param t 231 * the exception to log 232 */ 233 public void error(Object message, Throwable t) { 234 if (isErrorEnabled()) { 235 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t); 236 } 237 } 238 239 /** 240 * Converts the input parameter to String and then delegates to the error 241 * method of the wrapped <code>org.slf4j.Logger</code> instance. 242 * 243 * @param message 244 * the message to log. Converted to {@link String} 245 */ 246 public void fatal(Object message) { 247 if (isErrorEnabled()) { 248 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, null); 249 } 250 } 251 252 /** 253 * Converts the first input parameter to String and then delegates to the 254 * error method of the wrapped <code>org.slf4j.Logger</code> instance. 255 * 256 * @param message 257 * the message to log. Converted to {@link String} 258 * @param t 259 * the exception to log 260 */ 261 public void fatal(Object message, Throwable t) { 262 if (isErrorEnabled()) { 263 logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, String.valueOf(message), null, t); 264 } 265 } 266 267 /** 268 * Replace this instance with a homonymous (same name) logger returned by 269 * LoggerFactory. Note that this method is only called during deserialization. 270 * 271 * @return logger with same name as returned by LoggerFactory 272 * @throws ObjectStreamException 273 */ 274 protected Object readResolve() throws ObjectStreamException { 275 Logger logger = LoggerFactory.getLogger(this.name); 276 return new SLF4JLocationAwareLog((LocationAwareLogger) logger); 277 } 278}