1 61 62 package org.logicalcobwebs.logging.impl; 63 64 import org.logicalcobwebs.logging.Log; 65 import org.apache.log4j.Category; 66 import org.apache.log4j.ConsoleAppender; 67 import org.apache.log4j.PatternLayout; 68 import org.apache.log4j.Level; 69 import org.apache.log4j.Priority; 70 71 import java.util.Enumeration ; 72 73 84 public final class Log4JCategoryLog implements Log { 85 86 88 89 private static final String FQCN = Log4JCategoryLog.class.getName (); 90 91 private static boolean initialized = false; 92 private static final String LAYOUT = "%r [%t] %p %c{2} %x - %m%n"; 93 94 95 private Category category = null; 96 97 98 100 public Log4JCategoryLog () { 101 if (!initialized) { 102 initialize (); 103 } 104 } 105 106 109 public Log4JCategoryLog (String name) { 110 if (!initialized) { 111 initialize (); 112 } 113 this.category = Category.getInstance (name); 114 } 115 116 118 public Log4JCategoryLog (Category category) { 119 if (!initialized) { 120 initialize (); 121 } 122 this.category = category; 123 } 124 125 126 128 private void initialize () { 129 Category root = Category.getRoot (); 130 Enumeration appenders = root.getAllAppenders (); 131 if (appenders == null || !appenders.hasMoreElements ()) { 132 ConsoleAppender app = new ConsoleAppender (new PatternLayout (LAYOUT), 135 ConsoleAppender.SYSTEM_ERR); 136 app.setName ("commons-logging"); 137 138 root.addAppender (app); 139 root.setLevel (Level.INFO); 140 } 141 initialized = true; 142 } 143 144 148 public void trace (Object message) { 149 category.log (FQCN, Priority.DEBUG, message, null); 150 } 151 152 156 public void trace (Object message, Throwable t) { 157 category.log (FQCN, Priority.DEBUG, message, t); 158 } 159 160 163 public void debug (Object message) { 164 category.log (FQCN, Priority.DEBUG, message, null); 165 } 166 167 170 public void debug (Object message, Throwable t) { 171 category.log (FQCN, Priority.DEBUG, message, t); 172 } 173 174 177 public void info (Object message) { 178 category.log (FQCN, Priority.INFO, message, null); 179 } 180 181 184 public void info (Object message, Throwable t) { 185 category.log (FQCN, Priority.INFO, message, t); 186 } 187 188 191 public void warn (Object message) { 192 category.log (FQCN, Priority.WARN, message, null); 193 } 194 195 198 public void warn (Object message, Throwable t) { 199 category.log (FQCN, Priority.WARN, message, t); 200 } 201 202 205 public void error (Object message) { 206 category.log (FQCN, Priority.ERROR, message, null); 207 } 208 209 212 public void error (Object message, Throwable t) { 213 category.log (FQCN, Priority.ERROR, message, t); 214 } 215 216 219 public void fatal (Object message) { 220 category.log (FQCN, Priority.FATAL, message, null); 221 } 222 223 226 public void fatal (Object message, Throwable t) { 227 category.log (FQCN, Priority.FATAL, message, t); 228 } 229 230 233 public boolean isDebugEnabled () { 234 return category.isDebugEnabled (); 235 } 236 237 240 public boolean isErrorEnabled () { 241 return category.isEnabledFor (Priority.ERROR); 242 } 243 244 247 public boolean isFatalEnabled () { 248 return category.isEnabledFor (Priority.FATAL); 249 } 250 251 254 public boolean isInfoEnabled () { 255 return category.isInfoEnabled (); 256 } 257 258 262 public boolean isTraceEnabled () { 263 return category.isDebugEnabled (); 264 } 265 266 269 public boolean isWarnEnabled () { 270 return category.isEnabledFor (Priority.WARN); 271 } 272 } 273 274 | Popular Tags |