1 18 package org.objectweb.util.monolog.wrapper.printwriter; 19 20 import org.objectweb.util.monolog.api.BasicLevel; 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.Level; 23 import org.objectweb.util.monolog.api.MonologFactoryListener; 24 import org.objectweb.util.monolog.api.TopicalLogger; 25 import org.objectweb.util.monolog.api.Logger; 26 import org.objectweb.util.monolog.api.MonologFactory; 27 import org.objectweb.util.monolog.wrapper.common.EnumrationImpl; 28 import org.objectweb.util.monolog.wrapper.common.LevelImpl; 29 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 import java.util.Vector ; 35 import java.util.Properties ; 36 import java.util.Map ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 40 46 public class LoggerImpl 47 implements TopicalLogger, MonologFactory { 48 49 public final static String PRINT_WRITER = "printwriter"; 50 51 54 private int level = BasicLevel.DEBUG; 55 private Level lev = null; 56 57 protected String name = null; 58 protected Vector topics = null; 59 protected Hashtable handlers = null; 60 protected boolean additivity = true; 61 protected Map levels = null; 62 63 66 private PrintWriter pw = null; 67 68 private boolean isOn = true; 69 70 75 public LoggerImpl() { 76 this(null); 77 } 78 79 85 public LoggerImpl(PrintWriter _pw) { 86 this("org.objectweb.util.monolog.wrapper.printwriter", _pw); 87 } 88 89 95 public LoggerImpl(String n, PrintWriter _pw) { 96 name = n; 97 pw = _pw; 98 } 99 100 public PrintWriter getPrintWriter() { 101 return pw; 102 } 103 104 public void addMonologFactoryListener(MonologFactoryListener mfl) { 105 } 106 107 public void removeMonologFactoryListener(MonologFactoryListener mfl) { 108 } 109 110 public void configure(Properties prop) throws Exception { 113 } 114 115 public Handler createHandler(String hn, String handlertype) { 118 return this; 119 } 120 121 public Handler removeHandler(String handlername) { 122 return this; 123 } 124 125 public Handler[] getHandlers() { 126 return new Handler[] {this}; 127 } 128 129 130 133 public synchronized Level defineLevel(String name, int value) { 134 Level l = new LevelImpl(name, value); 135 if (levels == null) { 136 levels = new HashMap (); 137 } 138 levels.put(new Integer (value), l); 139 return l; 140 } 141 142 public Level defineLevel(String name, String value) { 143 Level l = new LevelImpl(name, value, this); 144 if (levels == null) { 145 levels = new HashMap (); 146 } 147 levels.put(new Integer (value), l); 148 return l; 149 } 150 151 public Level getLevel(String name) { 152 Iterator it = levels.values().iterator(); 153 while(it.hasNext()) { 154 Level l = (Level) it.next(); 155 if (l.getName().equals(name)) { 156 return l; 157 } 158 } 159 return null; 160 } 161 162 165 public Level getLevel(int value) { 166 return (Level) levels.get(new Integer (value)); 167 } 168 169 172 public Level[] getLevels() { 173 return (Level[]) levels.values().toArray(new Level[levels.size()]); 174 } 175 176 public synchronized void removeLevel(String name) { 177 Iterator it = levels.values().iterator(); 178 while(it.hasNext()) { 179 Level l = (Level) it.next(); 180 if (l.getName().equals(name)) { 181 it.remove(); 182 } 183 } 184 } 185 186 187 190 public Logger getLogger(String key) { 191 return this; 192 } 193 194 public Logger getLogger(String key, String resourceBundleName) { 195 return this; 196 } 197 198 public String getResourceBundleName() { 199 return null; 200 } 201 202 public void setResourceBundleName(String resourceBundleName) { 203 } 204 205 public Logger[] getLoggers() { 206 return new Logger[]{this}; 207 } 208 209 public String getTopicPrefix() { 210 return null; 211 } 212 213 214 215 218 223 public void setIntLevel(int l) { 224 level = l; 225 } 226 227 232 public void setLevel(Level l) { 233 lev = l; 234 if (lev != null) { 235 level = lev.getIntValue(); 236 } 237 } 238 239 244 public int getCurrentIntLevel() { 245 return level; 246 } 247 248 253 public Level getCurrentLevel() { 254 return (lev == null 255 ? lev = new LevelImpl( 256 (level ==-1 257 ?"INHERIT" 258 :"INTER"), 259 level) 260 : lev); 261 } 262 263 269 public boolean isLoggable(int l) { 270 return pw != null && l >= level; 271 } 272 273 279 public boolean isLoggable(Level l) { 280 return pw != null && l != null && l.getIntValue() >= level; 281 } 282 283 288 public boolean isOn() { 289 return isOn; 290 } 291 292 297 public Enumeration getTopics() { 298 if (topics == null) { 299 String [] ar = {name}; 300 return new EnumrationImpl(ar); 301 } 302 else { 303 String [] str=new String [topics.size()]; 304 for (int i=0; i<topics.size(); i++ ) { 305 str[i]=(String )(topics.elementAt(i)); 306 } 307 return new EnumrationImpl(str); 308 } 309 } 310 311 314 public void log(int level, Object o) { 315 if (pw == null || !isOn() || o == null || !isLoggable(level)) { 316 return; 317 } 318 if (level == BasicLevel.ERROR) { 319 pw.println("**" + format(o.toString(), 2)); 320 } 321 else { 322 pw.println(format(o.toString(), 2)); 323 } 324 } 325 326 329 public void log(Level l, Object o) { 330 } 331 332 335 public void log(int level, Object o, Throwable t) { 336 if (pw == null || !isOn() || o == null || !isLoggable(level)) { 337 return; 338 } 339 pw.println(o.toString() + ":" + t.toString()); 340 } 341 342 345 public void log(Level l, Object o, Throwable t) { 346 } 347 348 351 public void log(int level, Object o, Object location, Object method) { 352 if (pw == null || !isOn() || o == null || !isLoggable(level)) { 353 return; 354 } 355 pw.println(location.toString() + "." + method.toString() + "(...) :" 356 + o.toString()); 357 } 358 359 362 public void log(Level l, Object o, Object location, 363 Object method) { 364 } 365 366 369 public void log(int level, Object o, Throwable t, Object location, Object method) { 370 if (pw == null || !isOn() || o == null || !isLoggable(level)) { 371 return; 372 } 373 pw.println(location.toString() + "." + method.toString() + "(...) :" 374 + o.toString() + " " + t.toString()); 375 } 376 377 380 public void log(Level l, Object o, Throwable t, Object location, 381 Object method) { 382 } 383 384 387 public void turnOn() { 388 isOn = true; 389 } 390 391 394 public void turnOff() { 395 isOn = false; 396 } 397 398 402 public String toString() { 403 return "Personnal implementation on PrintWriter object"; 404 } 405 406 407 410 public void addHandler(Handler h) throws Exception { 411 if (h != null) { 412 if (handlers == null) { 413 handlers = new Hashtable (); 414 } 415 handlers.put(h.getName(), h); 416 } 417 } 418 419 public void addTopic(String topic) throws Exception { 420 if (topics == null) { 421 topics = new Vector (); 422 topics.addElement(name); 423 } 424 topics.addElement(topic); 425 } 426 427 public void removeHandler(Handler h) throws Exception { 428 if (h != null && handlers != null) { 429 handlers.remove(h.getName()); 430 } 431 } 432 433 public void removeAllHandlers() throws Exception { 434 if (handlers!=null) { 435 handlers.clear(); 436 } 437 } 438 439 public void removeTopic(String topic) throws Exception { 440 if (topics != null) { 441 topics.removeElement(topic); 442 } 443 } 444 445 public void setAdditivity(boolean a) { 446 additivity = a; 447 } 448 449 public boolean getAdditivity() { 450 return additivity; 451 } 452 453 public Handler[] getHandler() { 454 Handler[] result; 455 if (handlers == null) 456 result = new Handler[0]; 457 else { 458 result = new Handler[handlers.size()]; 459 int i =0; 460 for (Enumeration e=handlers.elements(); e.hasMoreElements(); i++) { 461 result[i] = (Handler)(e.nextElement()); 462 } 463 } 464 return (result); 465 } 466 467 public Handler getHandler(String hn) { 468 return (handlers == null ? null : (Handler) handlers.get(hn)); 469 } 470 471 public String [] getTopic() { 472 if (topics == null) { 473 String [] ar = {name}; 474 return ar; 475 } 476 else { 477 String [] str=new String [topics.size()]; 478 for (int i=0; i<topics.size(); i++ ) { 479 str[i]=(String )(topics.elementAt(i)); 480 } 481 return str; 482 } 483 } 484 485 public String getName() { 488 return name; 489 } 490 491 public void setName(String n) { 492 name = n; 493 } 494 495 public String getType() { 496 return "logger"; 497 } 498 499 public String [] getAttributeNames() { 500 String [] ar = {PRINT_WRITER}; 501 return ar; 502 } 503 504 public Object getAttribute(String name) { 505 if (PRINT_WRITER.equalsIgnoreCase(name)) { 506 return pw; 507 } 508 return null; 509 } 510 511 public Object setAttribute(String name, Object value) { 512 if (PRINT_WRITER.equalsIgnoreCase(name) 513 && value != null 514 && value instanceof PrintWriter ) { 515 pw = (PrintWriter ) value; 516 } 517 return null; 518 } 519 520 526 public static String format(String msg, int removeTopStack) { 527 Throwable t = new Throwable ().fillInStackTrace(); 528 StringWriter sw = new StringWriter (); 529 t.printStackTrace(new PrintWriter (sw)); 530 String m = sw.getBuffer().toString(); 531 532 int deb = -1; 533 534 int fin = 0; 535 536 deb = -1; 538 for (int i = 0; i < (removeTopStack + 1); i++) { 539 deb = m.indexOf("\n", deb + 1); 540 } 541 542 deb = m.indexOf("at ", deb); 543 fin = m.indexOf("\n", deb); 544 m = m.substring(deb + 3, fin); 545 546 deb = m.indexOf("("); 548 fin = m.indexOf(":"); 549 m = m.substring(0, deb + 1) + m.substring(fin + 1, m.length()); 550 551 deb = m.indexOf("("); 553 int c1 = 0; 554 int c2 = 0; 555 int current = m.indexOf("."); 556 while (current != -1 && current < deb) { 557 c1 = c2; 558 c2 = current; 559 current = m.indexOf(".", current + 1); 560 } 561 m = m.substring(c1 + 1, m.length()); 562 563 return m + ": " + msg; 564 } 565 566 static { 567 if (BasicLevel.FATAL <= 0 568 || BasicLevel.ERROR <= 0 569 || BasicLevel.WARN <= 0 570 || BasicLevel.INFO <= 0 571 || BasicLevel.DEBUG <= 0) { 572 BasicLevel.FATAL = 50000; 573 BasicLevel.ERROR = 40000; 574 BasicLevel.WARN = 30000; 575 BasicLevel.INFO = 20000; 576 BasicLevel.DEBUG = 10000; 577 } 578 } 579 } 580 581 | Popular Tags |