1 9 10 package org.mmbase.util.logging.log4j; 11 12 import org.mmbase.util.logging.Logger; 13 import org.mmbase.util.logging.Level; 14 import org.mmbase.util.logging.Logging; 15 16 import org.mmbase.util.ResourceWatcher; 17 import org.mmbase.util.ResourceLoader; 18 19 import org.apache.log4j.xml.DOMConfigurator; 20 21 import java.io.*; 22 23 import java.io.PrintStream ; 24 import java.io.File ; 25 26 43 44 public final class Log4jImpl extends org.apache.log4j.Logger implements Logger { 45 47 private final static org.apache.log4j.spi.LoggerRepository repository = new LoggerRepository(getRootLogger()); 49 private static Logger log = Logging.getLoggerInstance(Log4jImpl.class); 50 51 private static final String classname = Log4jImpl.class.getName(); 52 53 private static ResourceWatcher configWatcher; 54 55 58 protected Log4jImpl(String name) { 59 super(name); 60 } 61 62 66 public static Log4jImpl getLoggerInstance(String name) { 67 try { 68 return (Log4jImpl) repository.getLogger(name); 69 } catch (ClassCastException e) { 70 Log4jImpl root = (Log4jImpl) getRootLogger(); root.error("ClassCastException, probably you've forgotten a class attribute in your configuration file. It must say class=\"" + Log4jImpl.class.getName() + "\""); 72 return root; 73 } 74 } 75 76 77 88 89 public static void configure(String s) { 90 91 log.info("logging configurationfile : " + s); 92 93 ResourceLoader rl = Logging.getResourceLoader(); 94 95 log.info("using " + rl + " for resolving " + s); 96 configWatcher = new ResourceWatcher (rl) { 97 public void onChange(String s) { 98 doConfigure(resourceLoader.getResourceAsStream(s)); 99 } 100 }; 101 102 configWatcher.clear(); 103 configWatcher.add(s); 104 105 doConfigure(rl.getResourceAsStream(s)); 106 107 configWatcher.setDelay(10 * 1000); configWatcher.start(); 109 log = getLoggerInstance(Log4jImpl.class.getName()); 110 111 Log4jImpl err = getLoggerInstance("STDERR"); 112 if(err.getLevel() != Log4jLevel.FATAL) { 114 log.service("Redirecting stderr to MMBase logging (If you don't like this, then put the STDER logger to 'fatal')"); 115 System.setErr(new LoggerStream(err)); 116 } 117 } 118 119 protected static void doConfigure(InputStream i) { 120 DOMConfigurator domConfigurator = new DOMConfigurator(); 121 domConfigurator.doConfigure(i, repository); 122 } 123 126 protected static void doConfigure(File f) { 127 log.info("Parsing " + f.getAbsolutePath()); 128 try { 129 doConfigure(new FileInputStream(f)); 130 } catch (java.io.FileNotFoundException e) { 131 log.error("Could not find " + f + " to configure logging: " + e.toString()); 132 } 133 134 } 135 136 public void setLevel(Level p) { 137 switch (p.toInt()) { 138 case Level.TRACE_INT: setLevel(Log4jLevel.TRACE); break; 139 case Level.DEBUG_INT: setLevel(Log4jLevel.DEBUG); break; 140 case Level.SERVICE_INT: setLevel(Log4jLevel.SERVICE); break; 141 case Level.INFO_INT: setLevel(Log4jLevel.INFO); break; 142 case Level.WARN_INT: setLevel(Log4jLevel.WARN); break; 143 case Level.ERROR_INT: setLevel(Log4jLevel.ERROR); break; 144 case Level.FATAL_INT: setLevel(Log4jLevel.FATAL); break; 145 case Level.OFF_INT: setLevel(Log4jLevel.OFF); break; 146 default: break; 147 } 148 } 149 150 154 public static org.apache.log4j.Category getInstance(String name) { 155 return getLogger(name); 156 } 157 158 public static org.apache.log4j.Logger getLogger(String name) { 159 return repository.getLogger(name); 160 } 161 162 165 public final void trace(Object message) { 166 if (repository.isDisabled(Log4jLevel.TRACE_INT)) { 168 return; 169 } 170 if (Log4jLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel())) 171 forcedLog(classname, Log4jLevel.TRACE, message, null); 173 } 174 175 public final void trace(Object message, Throwable t) { 176 if (repository.isDisabled(Log4jLevel.TRACE_INT)) { 178 return; 179 } 180 if (Log4jLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel())) 181 forcedLog(classname, Log4jLevel.TRACE, message, t); 183 } 184 185 188 public final void service(Object message) { 189 if (repository.isDisabled(Log4jLevel.SERVICE_INT)) { 191 return; 192 } 193 if (Log4jLevel.SERVICE.isGreaterOrEqual(this.getEffectiveLevel())) 194 forcedLog(classname, Log4jLevel.SERVICE, message, null); 196 } 197 198 public final void service(Object message, Throwable t) { 199 if (repository.isDisabled(Log4jLevel.SERVICE_INT)) { 201 return; 202 } 203 if (Log4jLevel.SERVICE.isGreaterOrEqual(this.getEffectiveLevel())) 204 forcedLog(classname, Log4jLevel.SERVICE, message, t); 206 } 207 208 public final boolean isServiceEnabled() { 209 if(repository.isDisabled( Log4jLevel.SERVICE_INT)) 210 return false; 211 return Log4jLevel.SERVICE.isGreaterOrEqual(this.getEffectiveLevel()); 212 } 213 214 public final boolean isTraceEnabled() { 215 if(repository.isDisabled( Log4jLevel.TRACE_INT)) 216 return false; 217 return Log4jLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel()); 218 } 219 220 public static void shutdown() { 221 Log4jImpl err = getLoggerInstance("STDERR"); 222 if(err.getLevel() != Log4jLevel.FATAL) { 223 log.service("System stderr now going to stdout"); 224 System.setErr(System.out); 225 } 226 log.service("Shutting down log4j"); 227 repository.shutdown(); 228 } 229 230 237 238 private static class LoggerStream extends PrintStream { 239 240 private Logger log; 241 242 private int checkCount = 0; 245 LoggerStream (Log4jImpl l) throws IllegalArgumentException { 246 super(System.out); 247 if (l == null) { 248 throw new IllegalArgumentException ("logger == null"); 249 } 250 log = l; 251 } 252 253 private LoggerStream () { 254 super(System.out); 256 } 257 public void print (char[] s) { log.warn(new String (s)); } 259 public void print (String s) { log.warn(s); } 260 public void print (Object s) { log.warn(s.toString()); } 261 public void println (char[] s) { log.warn(new String (s)); } 262 public void println (String s) { 263 if (checkCount > 0) { 266 System.out.println(s); 267 } else { 268 checkCount++; 269 log.trace("6"); log.warn(s); 270 checkCount--; 271 } 272 } 273 public void println (Object s) { 274 if (Exception .class.isAssignableFrom(s.getClass())) { 277 log.fatal(s.toString()); } else { 279 log.warn(s.toString()); 280 } 281 } 282 283 } 284 285 } 286 287 | Popular Tags |