1 16 package org.apache.log4j; 17 18 import org.slf4j.LoggerFactory; 19 import org.slf4j.Marker; 20 import org.slf4j.MarkerFactory; 21 import org.slf4j.spi.LocationAwareLogger; 22 23 41 42 public class Category { 43 44 private String name; 45 46 private org.slf4j.Logger lbLogger; 47 private org.slf4j.spi.LocationAwareLogger locationAwareLogger; 48 49 private static Marker TRACE_MARKER = MarkerFactory.getMarker("TRACE"); 50 private static Marker FATAL_MARKER = MarkerFactory.getMarker("FATAL"); 51 52 Category(String name) { 53 this.name = name; 54 lbLogger = LoggerFactory.getLogger(name); 55 if(lbLogger instanceof LocationAwareLogger) { 56 locationAwareLogger = (LocationAwareLogger) lbLogger; 57 } 58 } 59 60 public static Logger getLogger(String name) { 61 return Log4jLoggerFactory.getLogger(name); 62 } 63 64 public static Logger getLogger(Class clazz) { 65 return getLogger(clazz.getName()); 66 } 67 68 73 public static Logger getRootLogger() { 74 return getLogger("root"); 75 } 76 77 82 public String getName() { 83 return name; 84 } 85 86 90 public boolean isTraceEnabled() { 91 return lbLogger.isTraceEnabled(); 92 } 93 94 97 public boolean isDebugEnabled() { 98 return lbLogger.isDebugEnabled(); 99 } 100 101 104 public boolean isInfoEnabled() { 105 return lbLogger.isInfoEnabled(); 106 } 107 108 111 public boolean isWarnEnabled() { 112 return lbLogger.isWarnEnabled(); 113 } 114 115 public boolean isEnabledFor(Priority p) { 116 return isEnabledFor(Level.toLevel(p.level)); 117 } 118 119 public boolean isEnabledFor(Level l) { 120 switch (l.level) { 121 case Level.TRACE_INT: 122 return lbLogger.isTraceEnabled(); 123 case Level.DEBUG_INT: 124 return lbLogger.isDebugEnabled(); 125 case Level.INFO_INT: 126 return lbLogger.isInfoEnabled(); 127 case Level.WARN_INT: 128 return lbLogger.isWarnEnabled(); 129 case Level.ERROR_INT: 130 return lbLogger.isErrorEnabled(); 131 case Priority.FATAL_INT: 132 return lbLogger.isErrorEnabled(); 133 } 134 return false; 135 } 136 137 140 public boolean isErrorEnabled() { 141 return lbLogger.isErrorEnabled(); 142 } 143 144 148 public void trace(Object message) { 149 lbLogger.debug(TRACE_MARKER, convertToString(message)); 150 } 151 152 156 public void trace(Object message, Throwable t) { 157 lbLogger.debug(TRACE_MARKER, convertToString(message), t); 158 } 159 160 164 public void trace(Object message, Object o) { 165 lbLogger.debug(TRACE_MARKER, convertToString(message), o); 166 } 167 168 172 public void trace(String message, Object arg1, Object arg2) { 173 lbLogger.debug(TRACE_MARKER, message, arg1, arg2); 174 } 175 176 180 public void debug(Object message) { 181 lbLogger.debug(convertToString(message)); 184 } 185 186 190 public void debug(Object message, Throwable t) { 191 lbLogger.debug(convertToString(message), t); 192 } 193 194 198 public void debug(Object message, Object o) { 199 lbLogger.debug(convertToString(message), o); 200 } 201 202 206 public void debug(String message, Object arg1, Object arg2) { 207 lbLogger.debug(message, arg1, arg2); 208 } 209 210 214 public void info(Object message) { 215 lbLogger.info(convertToString(message)); 216 } 217 218 222 public void info(Object message, Throwable t) { 223 lbLogger.info(convertToString(message), t); 224 } 225 226 230 public void info(Object message, Object o) { 231 lbLogger.info(convertToString(message), o); 232 } 233 234 238 public void info(String message, Object arg1, Object arg2) { 239 lbLogger.info(message, arg1, arg2); 240 } 241 242 246 public void warn(Object message) { 247 lbLogger.warn(convertToString(message)); 248 } 249 250 254 public void warn(Object message, Throwable t) { 255 lbLogger.warn(convertToString(message), t); 256 } 257 258 262 public void warn(Object message, Object o) { 263 lbLogger.warn(convertToString(message), o); 264 } 265 266 270 public void warn(String message, Object arg1, Object arg2) { 271 lbLogger.warn(message, arg1, arg2); 272 } 273 274 278 public void error(Object message) { 279 lbLogger.error(convertToString(message)); 280 } 281 282 286 public void error(Object message, Throwable t) { 287 lbLogger.error(convertToString(message), t); 288 } 289 290 294 public void error(Object message, Object o) { 295 lbLogger.error(convertToString(message), o); 296 } 297 298 302 public void error(String message, Object arg1, Object arg2) { 303 lbLogger.error(message, arg1, arg2); 304 } 305 306 310 public void fatal(Object message) { 311 lbLogger.error(FATAL_MARKER, convertToString(message)); 312 } 313 314 318 public void fatal(Object message, Throwable t) { 319 lbLogger.error(FATAL_MARKER, convertToString(message), t); 320 } 321 322 326 public void fatal(Object message, Object o) { 327 lbLogger.error(FATAL_MARKER, convertToString(message), o); 328 } 329 330 334 public void fatal(String message, Object arg1, Object arg2) { 335 lbLogger.error(FATAL_MARKER, message, arg1, arg2); 336 } 337 338 public void log(String FQCN, Priority p, Object msg, Throwable t) { 339 int levelInt = priorityToLevelInt(p); 340 if(locationAwareLogger != null) { 341 if(msg != null) { 342 locationAwareLogger.log(null, FQCN, levelInt, msg.toString(), t); 343 } else { 344 locationAwareLogger.log(null, FQCN, levelInt, null, t); 345 } 346 } else { 347 throw new UnsupportedOperationException ("The logger ["+lbLogger+"] does not seem to be location aware."); 348 } 349 350 } 351 352 private int priorityToLevelInt(Priority p) { 353 switch (p.level) { 354 case Level.TRACE_INT: 355 return LocationAwareLogger.TRACE_INT; 356 case Priority.DEBUG_INT: 357 return LocationAwareLogger.DEBUG_INT; 358 case Priority.INFO_INT: 359
|