1 package org.grlea.log.adapters.commons; 2 3 6 18 import org.grlea.log.DebugLevel; 19 import org.grlea.log.SimpleLog; 20 import org.grlea.log.SimpleLogger; 21 22 import org.apache.commons.logging.Log; 23 24 120 public class 121 CommonsLoggingAdapter 122 implements Log 123 { 124 private static final String SUPRESS_WARNINGS_PROPERTY = "org.grlea.log.adapters.commons.supressWarnings"; 125 126 private static SimpleLog log = null; 127 128 private final SimpleLogger logger; 129 130 public 131 CommonsLoggingAdapter(String loggerName) 132 { 133 initLog(); 134 135 SimpleLogger newLogger; 136 try 137 { 138 Class loggingClass = Class.forName(loggerName); 139 newLogger = new SimpleLogger(loggingClass); 140 } 141 catch (Exception e) 142 { 143 boolean supressWarnings = false; 144 try 145 { 146 String supressWarningsString = System.getProperty(SUPRESS_WARNINGS_PROPERTY); 147 supressWarnings = 148 supressWarningsString != null && supressWarningsString.toLowerCase().equals("true"); 149 } 150 catch (Exception e1) 151 { 152 System.err.println("WARNING: Simple Log (CommonsLoggingAdapter): " + 153 "Failed to read system property '" + SUPRESS_WARNINGS_PROPERTY + "'"); 154 } 155 156 if (!supressWarnings) 157 { 158 System.err.println("WARNING: Simple Log (CommonsLoggingAdapter): " + 159 "Failed to find class for logger name '" + loggerName + "'. " + 160 "Using class '" + Log.class.getName() + "' and instanceId '" + 161 loggerName +"'."); 162 } 163 164 newLogger = new SimpleLogger(Log.class, loggerName); 165 } 166 167 this.logger = newLogger; 168 } 169 170 private static void 171 initLog() 172 { 173 if (log == null) 174 { 175 setLog(SimpleLog.defaultInstance()); 176 } 177 } 178 179 public static void 180 setLog(SimpleLog simpleLog) 181 { 182 log = simpleLog; 183 } 184 185 public boolean 186 isFatalEnabled() 187 { 188 return logger.wouldLog(DebugLevel.L1_FATAL); 189 } 190 191 public boolean 192 isErrorEnabled() 193 { 194 return logger.wouldLog(DebugLevel.L2_ERROR); 195 } 196 197 public boolean 198 isWarnEnabled() 199 { 200 return logger.wouldLog(DebugLevel.L3_WARN); 201 } 202 203 public boolean 204 isInfoEnabled() 205 { 206 return logger.wouldLog(DebugLevel.L4_INFO); 207 } 208 209 public boolean 210 isDebugEnabled() 211 { 212 return logger.wouldLog(DebugLevel.L5_DEBUG); 213 } 214 215 public boolean 216 isTraceEnabled() 217 { 218 return logger.isTracing(); 219 } 220 221 public void 222 fatal(Object message) 223 { 224 if (logger.wouldLog(DebugLevel.L1_FATAL)) 225 { 226 log(DebugLevel.L1_FATAL, message); 227 } 228 } 229 230 public void 231 fatal(Object message, Throwable t) 232 { 233 if (logger.wouldLog(DebugLevel.L1_FATAL)) 234 { 235 log(DebugLevel.L1_FATAL, message); 236 logger.dbe(DebugLevel.L1_FATAL, t); 237 } 238 } 239 240 public void 241 error(Object message) 242 { 243 if (logger.wouldLog(DebugLevel.L2_ERROR)) 244 { 245 log(DebugLevel.L2_ERROR, message); 246 } 247 } 248 249 public void 250 error(Object message, Throwable t) 251 { 252 if (logger.wouldLog(DebugLevel.L2_ERROR)) 253 { 254 log(DebugLevel.L2_ERROR, message); 255 logger.dbe(DebugLevel.L2_ERROR, t); 256 } 257 } 258 259 public void 260 warn(Object message) 261 { 262 if (logger.wouldLog(DebugLevel.L3_WARN)) 263 { 264 log(DebugLevel.L3_WARN, message); 265 } 266 } 267 268 public void 269 warn(Object message, Throwable t) 270 { 271 if (logger.wouldLog(DebugLevel.L3_WARN)) 272 { 273 log(DebugLevel.L3_WARN, message); 274 logger.dbe(DebugLevel.L3_WARN, t); 275 } 276 } 277 278 public void 279 info(Object message) 280 { 281 if (logger.wouldLog(DebugLevel.L4_INFO)) 282 { 283 log(DebugLevel.L4_INFO, message); 284 } 285 } 286 287 public void 288 info(Object message, Throwable t) 289 { 290 if (logger.wouldLog(DebugLevel.L4_INFO)) 291 { 292 log(DebugLevel.L4_INFO, message); 293 logger.dbe(DebugLevel.L4_INFO, t); 294 } 295 } 296 297 public void 298 debug(Object message) 299 { 300 if (logger.wouldLog(DebugLevel.L5_DEBUG)) 301 { 302 log(DebugLevel.L5_DEBUG, message); 303 } 304 } 305 306 public void 307 debug(Object message, Throwable t) 308 { 309 if (logger.wouldLog(DebugLevel.L5_DEBUG)) 310 { 311 log(DebugLevel.L5_DEBUG, message); 312 logger.dbe(DebugLevel.L5_DEBUG, t); 313 } 314 } 315 316 public void 317 trace(Object message) 318 { 319 if (logger.wouldLog(DebugLevel.L6_VERBOSE)) 320 { 321 log(DebugLevel.L6_VERBOSE, message); 322 } 323 } 324 325 public void 326 trace(Object message, Throwable t) 327 { 328 if (logger.wouldLog(DebugLevel.L6_VERBOSE)) 329 { 330 log(DebugLevel.L6_VERBOSE, message); 331 logger.dbe(DebugLevel.L6_VERBOSE, t); 332 } 333 } 334 335 private void 336 log(DebugLevel level, Object message) 337 { 338 if (message instanceof String ) 339 logger.db(level, (String ) message); 340 else 341 logger.dbo(level, "", message); 342 } 343 } | Popular Tags |