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; 025 026/** 027 * Implementation of {@link Log org.apache.commons.logging.Log} interface which 028 * delegates all processing to a wrapped {@link Logger org.slf4j.Logger} 029 * instance. 030 * 031 * <p> 032 * JCL's FATAL level is mapped to ERROR. All other levels map one to one. 033 * 034 * @author Ceki Gülcü 035 */ 036public class SLF4JLog implements Log, Serializable { 037 038 private static final long serialVersionUID = 680728617011167209L; 039 040 // used to store this logger's name to recreate it after serialization 041 protected String name; 042 043 // in both Log4jLogger and Jdk14Logger classes in the original JCL, the 044 // logger instance is transient 045 private final transient Logger logger; 046 047 public SLF4JLog(Logger logger) { 048 this.logger = logger; 049 this.name = logger.getName(); 050 } 051 052 /** 053 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 054 */ 055 public boolean isDebugEnabled() { 056 return logger.isDebugEnabled(); 057 } 058 059 /** 060 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 061 */ 062 public boolean isErrorEnabled() { 063 return logger.isErrorEnabled(); 064 } 065 066 /** 067 * Delegates to the <code>isErrorEnabled<code> method of the wrapped 068 * <code>org.slf4j.Logger</code> instance. 069 */ 070 public boolean isFatalEnabled() { 071 return logger.isErrorEnabled(); 072 } 073 074 /** 075 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 076 */ 077 public boolean isInfoEnabled() { 078 return logger.isInfoEnabled(); 079 } 080 081 /** 082 * Delegates to the <code>isDebugEnabled<code> method of the wrapped 083 * <code>org.slf4j.Logger</code> instance. 084 */ 085 public boolean isTraceEnabled() { 086 return logger.isTraceEnabled(); 087 } 088 089 /** 090 * Directly delegates to the wrapped <code>org.slf4j.Logger</code> instance. 091 */ 092 public boolean isWarnEnabled() { 093 return logger.isWarnEnabled(); 094 } 095 096 /** 097 * Converts the input parameter to String and then delegates to the debug 098 * method of the wrapped <code>org.slf4j.Logger</code> instance. 099 * 100 * @param message 101 * the message to log. Converted to {@link String} 102 */ 103 public void trace(Object message) { 104 logger.trace(String.valueOf(message)); 105 } 106 107 /** 108 * Converts the first input parameter to String and then delegates to the 109 * debug method of the wrapped <code>org.slf4j.Logger</code> instance. 110 * 111 * @param message 112 * the message to log. Converted to {@link String} 113 * @param t 114 * the exception to log 115 */ 116 public void trace(Object message, Throwable t) { 117 logger.trace(String.valueOf(message), t); 118 } 119 120 /** 121 * Converts the input parameter to String and then delegates to the wrapped 122 * <code>org.slf4j.Logger</code> instance. 123 * 124 * @param message 125 * the message to log. Converted to {@link String} 126 */ 127 public void debug(Object message) { 128 logger.debug(String.valueOf(message)); 129 } 130 131 /** 132 * Converts the first input parameter to String and then delegates to the 133 * wrapped <code>org.slf4j.Logger</code> instance. 134 * 135 * @param message 136 * the message to log. Converted to {@link String} 137 * @param t 138 * the exception to log 139 */ 140 public void debug(Object message, Throwable t) { 141 logger.debug(String.valueOf(message), t); 142 } 143 144 /** 145 * Converts the input parameter to String and then delegates to the wrapped 146 * <code>org.slf4j.Logger</code> instance. 147 * 148 * @param message 149 * the message to log. Converted to {@link String} 150 */ 151 public void info(Object message) { 152 logger.info(String.valueOf(message)); 153 } 154 155 /** 156 * Converts the first input parameter to String and then delegates to the 157 * wrapped <code>org.slf4j.Logger</code> instance. 158 * 159 * @param message 160 * the message to log. Converted to {@link String} 161 * @param t 162 * the exception to log 163 */ 164 public void info(Object message, Throwable t) { 165 logger.info(String.valueOf(message), t); 166 } 167 168 /** 169 * Converts the input parameter to String and then delegates to the wrapped 170 * <code>org.slf4j.Logger</code> instance. 171 * 172 * @param message 173 * the message to log. Converted to {@link String} 174 */ 175 public void warn(Object message) { 176 logger.warn(String.valueOf(message)); 177 } 178 179 /** 180 * Converts the first input parameter to String and then delegates to the 181 * wrapped <code>org.slf4j.Logger</code> instance. 182 * 183 * @param message 184 * the message to log. Converted to {@link String} 185 * @param t 186 * the exception to log 187 */ 188 public void warn(Object message, Throwable t) { 189 logger.warn(String.valueOf(message), t); 190 } 191 192 /** 193 * Converts the input parameter to String and then delegates to the wrapped 194 * <code>org.slf4j.Logger</code> instance. 195 * 196 * @param message 197 * the message to log. Converted to {@link String} 198 */ 199 public void error(Object message) { 200 logger.error(String.valueOf(message)); 201 } 202 203 /** 204 * Converts the first input parameter to String and then delegates to the 205 * wrapped <code>org.slf4j.Logger</code> instance. 206 * 207 * @param message 208 * the message to log. Converted to {@link String} 209 * @param t 210 * the exception to log 211 */ 212 public void error(Object message, Throwable t) { 213 logger.error(String.valueOf(message), t); 214 } 215 216 /** 217 * Converts the input parameter to String and then delegates to the error 218 * method of the wrapped <code>org.slf4j.Logger</code> instance. 219 * 220 * @param message 221 * the message to log. Converted to {@link String} 222 */ 223 public void fatal(Object message) { 224 logger.error(String.valueOf(message)); 225 } 226 227 /** 228 * Converts the first input parameter to String and then delegates to the 229 * error method of the wrapped <code>org.slf4j.Logger</code> instance. 230 * 231 * @param message 232 * the message to log. Converted to {@link String} 233 * @param t 234 * the exception to log 235 */ 236 public void fatal(Object message, Throwable t) { 237 logger.error(String.valueOf(message), t); 238 } 239 240 /** 241 * Replace this instance with a homonymous (same name) logger returned by 242 * LoggerFactory. Note that this method is only called during deserialization. 243 * 244 * @return logger with same name as returned by LoggerFactory 245 * @throws ObjectStreamException 246 */ 247 protected Object readResolve() throws ObjectStreamException { 248 Logger logger = LoggerFactory.getLogger(this.name); 249 return new SLF4JLog(logger); 250 } 251}