1 package org.apache.ojb.broker.util.logging; 2 3 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.ojb.broker.util.configuration.Configuration; 21 import org.apache.ojb.broker.util.configuration.ConfigurationException; 22 23 32 public class CommonsLoggerImpl implements Logger 33 { 34 private String name; 35 private transient Log log; 36 37 38 41 public CommonsLoggerImpl(String aName) 42 { 43 this.name = aName; 44 } 45 46 50 public Log getLog() 51 { 52 56 if(log == null) 57 { 58 log = LogFactory.getLog(name); 59 } 60 return log; 61 } 62 63 66 public boolean isEnabledFor(int priority) 67 { 68 Log commonsLog = getLog(); 69 switch(priority) 70 { 71 case Logger.DEBUG: return commonsLog.isDebugEnabled(); 72 case Logger.INFO: return commonsLog.isInfoEnabled(); 73 case Logger.WARN: return commonsLog.isWarnEnabled(); 74 case Logger.ERROR: return commonsLog.isErrorEnabled(); 75 case Logger.FATAL: return commonsLog.isFatalEnabled(); 76 } 77 return false; 78 } 79 80 83 public void debug(Object pObject) 84 { 85 getLog().debug(pObject); 86 } 87 88 91 public void info(Object pObject) 92 { 93 getLog().info(pObject); 94 } 95 96 99 public void warn(Object pObject) 100 { 101 getLog().warn(pObject); 102 } 103 104 107 public void error(Object pObject) 108 { 109 getLog().error(pObject); 110 } 111 112 115 public void fatal(Object pObject) 116 { 117 getLog().fatal(pObject); 118 } 119 120 123 public void debug(Object message, Throwable obj) 124 { 125 getLog().debug(message, obj); 126 } 127 128 131 public void info(Object message, Throwable obj) 132 { 133 getLog().info(message, obj); 134 } 135 136 139 public void warn(Object message, Throwable obj) 140 { 141 getLog().warn(message, obj); 142 } 143 144 147 public void error(Object message, Throwable obj) 148 { 149 getLog().error(message, obj); 150 } 151 152 155 public void fatal(Object message, Throwable obj) 156 { 157 getLog().fatal(message, obj); 158 } 159 160 163 public boolean isDebugEnabled() 164 { 165 return getLog().isDebugEnabled(); 166 } 167 168 171 public String getName() 172 { 173 return name; 174 } 175 176 179 public void safeDebug(String message, Object obj) 180 { 181 if (getLog().isDebugEnabled()) 182 { 183 String toString = safeToString(obj); 184 getLog().debug(message + " : " + toString); 185 } 186 } 187 188 191 public void safeDebug(String message, Object obj, Throwable t) 192 { 193 if (getLog().isDebugEnabled()) 194 { 195 String toString = safeToString(obj); 196 getLog().debug(message + " : " + toString, t); 197 } 198 } 199 200 203 public void safeInfo(String message, Object obj) 204 { 205 if (getLog().isInfoEnabled()) 206 { 207 String toString = safeToString(obj); 208 getLog().info(message + " : " + toString); 209 } 210 } 211 212 215 public void safeInfo(String message, Object obj, Throwable t) 216 { 217 if (getLog().isInfoEnabled()) 218 { 219 String toString = safeToString(obj); 220 getLog().info(message + " : " + toString, t); 221 } 222 } 223 224 227 public void safeWarn(String message, Object obj) 228 { 229 if (getLog().isWarnEnabled()) 230 { 231 String toString = safeToString(obj); 232 getLog().warn(message + " : " + toString); 233 } 234 } 235 236 239 public void safeWarn(String message, Object obj, Throwable t) 240 { 241 if (getLog().isWarnEnabled()) 242 { 243 String toString = safeToString(obj); 244 getLog().warn(message + " : " + toString, t); 245 } 246 } 247 248 251 public void safeError(String message, Object obj) 252 { 253 if (getLog().isErrorEnabled()) 254 { 255 String toString = safeToString(obj); 256 getLog().error(message + " : " + toString); 257 } 258 } 259 260 263 public void safeError(String message, Object obj, Throwable t) 264 { 265 if (getLog().isErrorEnabled()) 266 { 267 String toString = safeToString(obj); 268 getLog().error(message + " : " + toString, t); 269 } 270 } 271 272 275 public void safeFatal(String message, Object obj) 276 { 277 if (getLog().isFatalEnabled()) 278 { 279 String toString = safeToString(obj); 280 getLog().fatal(message + " : " + toString); 281 } 282 } 283 284 287 public void safeFatal(String message, Object obj, Throwable t) 288 { 289 if (getLog().isFatalEnabled()) 290 { 291 String toString = safeToString(obj); 292 getLog().fatal(message + " : " + toString, t); 293 } 294 } 295 296 299 private String safeToString(Object obj) 300 { 301 String toString = null; 302 if (obj != null) 303 { 304 try 305 { 306 toString = obj.toString(); 307 } 308 catch (Throwable ex) 309 { 310 toString = "BAD toString() impl for " + obj.getClass().getName(); 311 } 312 } 313 return toString; 314 } 315 316 319 public void configure(Configuration config) throws ConfigurationException 320 { 321 } 323 } 324 | Popular Tags |