1 9 10 package org.mmbase.util.logging; 11 12 import java.util.Iterator ; 13 import java.lang.reflect.Method ; 14 15 import org.mmbase.util.ResourceWatcher; 16 import org.mmbase.util.ResourceLoader; 17 import org.mmbase.util.xml.DocumentReader; 18 19 62 63 64 public class Logging { 65 66 private static Class logClass = SimpleTimeStampImpl.class; private static boolean configured = false; 68 private static final Logger log = getLoggerInstance(Logging.class); 70 75 public final static String PAGE_CATEGORY = "org.mmbase.PAGE"; 76 77 private static ResourceLoader resourceLoader; 78 79 82 private static ResourceWatcher configWatcher; 83 84 private static String machineName = "localhost"; 85 86 87 private Logging() { 88 } 90 91 92 95 public static String getMachineName() { 96 return machineName; 97 } 98 101 public static void setMachineName(String mn) { 102 machineName = mn; 103 } 104 105 106 114 115 public static void configure (ResourceLoader rl, String configFile) { 116 resourceLoader = rl; 117 configWatcher = new ResourceWatcher(rl) { 118 public void onChange(String s) { 119 configure(resourceLoader, s); 120 } 121 }; 122 123 if (configFile == null) { 124 log.info("No configfile given, default configuration will be used."); 125 return; 126 } 127 128 134 log.info("Configuring logging with " + configFile); 135 137 configWatcher.add(configFile); 138 configWatcher.setDelay(10 * 1000); configWatcher.start(); 140 141 DocumentReader reader; 142 try { 143 reader = new DocumentReader(resourceLoader.getInputSource(configFile), Logging.class); 144 } catch (Exception e) { 145 log.error("Could not open " + configFile + " " + e, e); 146 return; 147 } 148 if (reader == null) { 149 log.error("No " + configFile); 150 return; 151 } 152 153 String classToUse = SimpleImpl.class.getName(); String configuration = "stderr,info"; try { String claz = reader.getElementValue("logging.class"); 157 if (claz != null) { 158 classToUse = claz; 159 } 160 String config = reader.getElementValue("logging.configuration"); 161 if (config != null) configuration = config; 162 } catch (Exception e) { 163 log.error("Exception during parsing: " + e); 164 log.error(stackTrace(e)); 165 } 166 167 168 log.info("Class to use for logging " + classToUse); 169 Class logClassCopy = logClass; try { logClass = Class.forName(classToUse); 175 if (configured) { 176 if (! logClassCopy.equals(logClass)) { 177 log.warn("Tried to change logging implementation from " + logClassCopy + " to " + logClass + ". This is not really possible (most static instances are unreachable). Trying anyway as requested, if this gives strange results, you might need to restart."); 178 } 179 } 180 181 } catch (ClassNotFoundException e) { 182 log.error("Could not find class " + classToUse); 183 log.error(e.toString()); 184 logClass = logClassCopy; 185 } catch (Throwable e) { 186 log.error("Exception to find class " + classToUse + ": " + e); 187 log.info("Falling back to " + logClassCopy.getName()); 188 logClass = logClassCopy; 189 } 190 configureClass(configuration); 192 configured = true; 193 log.service("Logging configured"); 194 log.debug("Now watching " + configWatcher.getResources()); 195 log.debug("Replacing wrappers " + LoggerWrapper.getWrappers()); 196 Iterator wrappers = LoggerWrapper.getWrappers().iterator(); 197 while (wrappers.hasNext()) { 198 LoggerWrapper wrapper = (LoggerWrapper) wrappers.next(); 199 wrapper.setLogger(getLoggerInstance(wrapper.getName())); 200 log.debug("Replaced logger " + wrapper.getName()); 201 } 202 } 203 204 209 210 public static void configureClass(String configuration) { 211 try { Method conf = logClass.getMethod("configure", new Class [] { String .class } ); 214 conf.invoke(null, new Object [] { configuration } ); 215 } catch (NoSuchMethodException e) { 216 log.debug("Could not find configure method in " + logClass.getName()); 217 } catch (java.lang.reflect.InvocationTargetException e) { 219 log.error("Invocation Exception while configuration class. " + e.getMessage(), e); 220 } catch (Exception e) { 221 log.error("", e); 222 } 223 } 224 225 228 public static ResourceLoader getResourceLoader() { 229 return resourceLoader; 230 } 231 232 237 238 public static Logger getLoggerInstance (String s) { 239 try { 241 Method getIns = logClass.getMethod("getLoggerInstance", new Class [] { String .class } ); 242 Logger logger = (Logger) getIns.invoke(null, new Object [] {s}); 243 if (configured) { 244 return logger; 245 } else { 246 return new LoggerWrapper(logger, s); 247 } 248 } catch (Exception e) { 249 log.warn(e); 250 return SimpleImpl.getLoggerInstance(s); 251 } 252 } 253 254 258 public static Logger getLoggerInstance(Class cl) { 259 return getLoggerInstance(cl.getName()); 260 } 261 262 266 279 280 285 public static void shutdown() { 286 try { 287 if (logClass != null) { 288 Method shutdown = logClass.getMethod("shutdown", new Class [] {} ); 289 shutdown.invoke(null, new Object [] {} ); 290 } 291 } catch (NoSuchMethodException e) { 292 } catch (Exception e) { 294 System.err.println(e + stackTrace(e)); 295 } 296 297 } 298 299 306 public static String stackTrace() { 307 return stackTrace(-1); 308 } 309 310 313 public static String stackTrace(int max) { 314 Exception e = new Exception ("logging.stacktrace"); 315 321 return stackTrace(e, max); 322 } 323 324 336 public static String stackTrace(Throwable e) { 337 return stackTrace(e, -1); 338 } 339 340 344 public static String stackTrace(Throwable e, int max) { 345 StackTraceElement [] stackTrace = e.getStackTrace(); 346 String message = e.getMessage(); 347 StringBuffer buf = new StringBuffer (e.getClass().getName() + ": "); 348 if (message == null) { 349 350 } else { 351 buf.append(message); 352 } 353 for (int i = 0; i < stackTrace.length; i++) { 354 if (i == max) break; 355 buf.append("\n at ").append(stackTrace[i]); 356 } 357 Throwable t = e.getCause(); 358 if (t != null) { 359 buf.append(stackTrace(t, max)); 360 } 361 return buf.toString(); 362 } 363 364 367 public static String applicationStacktrace() { 368 Exception e = new Exception ("logging.showApplicationStacktrace"); 369 return applicationStacktrace(e); 370 } 371 372 375 public static String applicationStacktrace(Throwable e) { 376 StringBuffer buf = new StringBuffer ("Application stacktrace"); 377 378 StackTraceElement stackTrace[] = e.getStackTrace(); 380 384 boolean mmbaseClassesFound = false; 385 int appended = 0; 386 for (int i = 0; i < stackTrace.length; i++) { 387 String className = stackTrace[i].getClassName(); 388 389 if (className.indexOf("org.mmbase") > -1) { 390 mmbaseClassesFound = true; 391 if (className.indexOf("bridge.jsp.taglib") > -1) { 393 buf.append("\n at ").append(stackTrace[i]); 394 appended++; 395 } 396 } else { 397 if (mmbaseClassesFound) { 398 buf.append("\n at ").append(stackTrace[i]); 400 appended++; 401 break; 402 } 403 if (className.indexOf("_jsp") > -1) { 405 buf.append("\n at ").append(stackTrace[i]); 406 appended++; 407 } 408 } 409 } 410 if (appended == 0) { 411 for (int i = 2; i < stackTrace.length; i++) { 412 buf.append("\n at ").append(stackTrace[i]); 413 } 414 } 415 return buf.toString(); 416 } 417 418 } 419
| Popular Tags
|