1 52 53 package freemarker.log; 54 55 import java.util.HashMap ; 56 import java.util.Map ; 57 58 import freemarker.template.utility.ClassUtil; 59 60 71 public abstract class Logger 72 { 73 77 public static final int LIBRARY_AUTO = -1; 78 82 public static final int LIBRARY_NONE = 0; 83 87 public static final int LIBRARY_JAVA = 1; 88 92 public static final int LIBRARY_AVALON = 2; 93 97 public static final int LIBRARY_LOG4J = 3; 98 99 private static final String [] LIBINIT = 100 { 101 "freemarker.log", "Null", 102 "java.util.logging", "JDK14", 103 "org.apache.log", "Avalon", 104 "org.apache.log4j", "Log4J" 105 }; 106 107 private static int logLibrary; 108 private static LoggerFactory factory; 109 private static String categoryPrefix = ""; 110 111 private static final Map loggers = new HashMap (); 112 113 124 public static void selectLoggerLibrary(int library) 125 throws 126 ClassNotFoundException 127 { 128 synchronized (Logger.class) { 129 if(library < -1 || (library*2) >= LIBINIT.length) 130 { 131 throw new IllegalArgumentException (); 132 } 133 logLibrary = library; 134 factory = createFactory(); 135 } 136 } 137 138 149 public static void setCategoryPrefix(String prefix) 150 { 151 synchronized (Logger.class) { 152 if(prefix == null) 153 { 154 throw new IllegalArgumentException (); 155 } 156 categoryPrefix = prefix; 157 } 158 } 159 160 163 public abstract void debug(String message); 164 165 168 public abstract void debug(String message, Throwable t); 169 170 173 public abstract void info(String message); 174 175 178 public abstract void info(String message, Throwable t); 179 180 183 public abstract void warn(String message); 184 185 188 public abstract void warn(String message, Throwable t); 189 190 193 public abstract void error(String message); 194 195 198 public abstract void error(String message, Throwable t); 199 200 203 public abstract boolean isDebugEnabled(); 204 205 208 public abstract boolean isInfoEnabled(); 209 210 213 public abstract boolean isWarnEnabled(); 214 215 218 public abstract boolean isErrorEnabled(); 219 220 223 public abstract boolean isFatalEnabled(); 224 225 230 public static Logger getLogger(String category) 231 { 232 if (factory == null) { 233 synchronized (Logger.class) { 234 if (factory == null) { 235 try 236 { 237 selectLoggerLibrary(LIBRARY_AUTO); 238 } 239 catch(ClassNotFoundException e) 240 { 241 throw new RuntimeException (e.getMessage()); 243 } 244 } 245 } 246 } 247 248 category = categoryPrefix + category; 249 250 synchronized(loggers) 251 { 252 Logger logger = (Logger)loggers.get(category); 253 if(logger == null) 254 { 255 logger = factory.getLogger(category); 256 loggers.put(category, logger); 257 } 258 return logger; 259 } 260 } 261 262 private static LoggerFactory createFactory() 263 throws 264 ClassNotFoundException 265 { 266 if(logLibrary == LIBRARY_AUTO) 267 { 268 for(int i = LIBINIT.length / 2 - 1; i > 0; --i) 269 { 270 try 271 { 272 return createFactory(i); 273 } 274 catch(ClassNotFoundException e) 275 { 276 ; } 278 } 279 System.err.println("*** WARNING: FreeMarker logging suppressed."); 280 return new NullLoggerFactory(); 281 } 282 else 283 { 284 return createFactory(logLibrary); 285 } 286 } 287 288 private static LoggerFactory createFactory(int library) 289 throws 290 ClassNotFoundException 291 { 292 String requiredPackage = LIBINIT[library * 2]; 293 String factoryType = LIBINIT[library * 2 + 1]; 294 295 try 296 { 297 ClassUtil.forName(requiredPackage + ".Logger"); 298 return (LoggerFactory)Class.forName("freemarker.log." + factoryType + "LoggerFactory").newInstance(); 299 } 300 catch(IllegalAccessException e) 301 { 302 throw new IllegalAccessError (e.getMessage()); 304 } 305 catch(InstantiationException e) 306 { 307 throw new InstantiationError (e.getMessage()); 309 } 310 } 311 } 312 | Popular Tags |