1 18 19 package org.objectweb.util.monolog.wrapper.log4j; 20 21 import org.apache.log4j.Appender; 22 import org.apache.log4j.Priority; 23 import org.apache.log4j.spi.LoggingEvent; 25 import org.objectweb.util.monolog.api.BasicLevel; 26 import org.objectweb.util.monolog.api.Handler; 27 import org.objectweb.util.monolog.api.Level; 28 import org.objectweb.util.monolog.api.TopicalLogger; 29 import org.objectweb.util.monolog.wrapper.common.AbstractFactory; 30 import org.objectweb.util.monolog.wrapper.common.EnumrationImpl; 31 32 import java.io.PrintWriter ; 33 import java.io.StringWriter ; 34 import java.util.ArrayList ; 35 import java.util.Enumeration ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 39 53 public class MonologCategory 54 extends org.apache.log4j.Logger 55 implements TopicalLogger { 56 57 protected boolean enable = true; 58 protected final static int DISABLE_OFF = -1; 59 protected final static int DISABLE_ON = org.apache.log4j.Level.FATAL.toInt(); 60 protected OwPriority interPriority = null; 61 protected byte depth = 2; 62 63 69 protected HashMap topicToparents = null; 70 71 74 protected ArrayList appenders = null; 75 76 79 protected org.apache.log4j.Logger categ = null; 80 81 87 private final static String instanceFQN 88 = "org.objectweb.util.monolog.wrapper.log4j.MonologCategory"; 89 90 94 public MonologCategory(String _initialName) { 95 super(_initialName); 96 topicToparents = new HashMap (); 97 } 98 99 103 public MonologCategory(org.apache.log4j.Logger c) { 104 super(c.getName()); 105 categ = c; 106 } 107 108 116 public static String format(String msg, int removeTopStack) { 117 Throwable t = new Throwable ().fillInStackTrace(); 118 StringWriter sw = new StringWriter (); 119 t.printStackTrace(new PrintWriter (sw)); 120 String m = sw.getBuffer().toString(); 121 122 int deb = -1,fin = 0; 123 124 deb = -1; 126 for (int i = 0; i < (removeTopStack + 1); i++) { 127 deb = m.indexOf("\n", deb + 1); 128 } 129 130 deb = m.indexOf("at ", deb); 131 fin = m.indexOf("\n", deb); 132 m = m.substring(deb + 3, fin); 133 134 deb = m.indexOf("("); 136 fin = m.indexOf(":"); 137 m = m.substring(0, deb + 1) + m.substring(fin + 1, m.length()); 138 139 deb = m.indexOf("("); 141 int c1 = 0; 142 int c2 = 0; 143 int current = m.indexOf("."); 144 while (current != -1 && current < deb) { 145 c1 = c2; 146 c2 = current; 147 current = m.indexOf(".", current + 1); 148 } 149 m = m.substring(c1 + 1, m.length()); 150 151 return m + ": " + msg; 152 } 153 154 private org.apache.log4j.Level convertToLog4jLevel(int value) { 155 switch (value) { 156 case org.apache.log4j.Level.DEBUG_INT: 157 return org.apache.log4j.Level.DEBUG; 158 case org.apache.log4j.Level.INFO_INT: 159 return org.apache.log4j.Level.INFO; 160 case org.apache.log4j.Level.WARN_INT: 161 return org.apache.log4j.Level.WARN; 162 case org.apache.log4j.Level.ERROR_INT: 163 return org.apache.log4j.Level.ERROR; 164 case org.apache.log4j.Level.FATAL_INT: 165 return org.apache.log4j.Level.FATAL; 166 default: 167 if (interPriority == null) 168 interPriority = new OwPriority(value); 169 else 170 interPriority.level = value; 171 return interPriority; 172 } 173 } 174 175 177 185 public org.apache.log4j.Level getChainedLevel() { 186 if (categ != null) { 187 return categ.getEffectiveLevel(); 188 } 189 if (level != null) { 190 return level; 191 } 192 org.apache.log4j.Level current = parent.getEffectiveLevel(); 193 org.apache.log4j.Logger[] cats = (org.apache.log4j.Logger[]) 194 topicToparents.values().toArray(new org.apache.log4j.Logger[0]); 195 for (int i = 0; i < cats.length; i++) { 196 org.apache.log4j.Level neo = cats[i].getEffectiveLevel(); 197 if (neo.isGreaterOrEqual(current)) { 198 current = neo; 199 } 200 } 201 return current; 202 } 203 204 209 public void callAppenders(LoggingEvent event) { 210 if (categ != null) { 211 categ.callAppenders(event); 212 } 213 callAppenders(event, false); 214 } 215 216 239 public synchronized boolean callAppenders(LoggingEvent event, boolean called) { 240 org.apache.log4j.Level l = event.getLevel(); 241 if (called 242 || (level != null 243 && l.isGreaterOrEqual(level))) { 244 for (Enumeration en = getAllAppenders(); 245 en.hasMoreElements();) { 246 ((Appender) en.nextElement()).doAppend(event); 247 } 248 if (additive) { 249 if (parent instanceof MonologCategory) 250 ((MonologCategory) parent).callAppenders(event, true); 251 else 252 parent.callAppenders(event); 253 for (Iterator it = topicToparents.values().iterator(); it.hasNext();) { 254 org.apache.log4j.Logger c = (org.apache.log4j.Logger) it.next(); 255 if (c instanceof MonologCategory) 256 ((MonologCategory) c).callAppenders(event, true); 257 else 258 c.callAppenders(event); 259 } 260 } 261 return true; 262 } 263 else if (level == null && additive) { 264 if (parent instanceof MonologCategory) { 265 called |= ((MonologCategory) parent).callAppenders(event, false); 266 } 267 else if (parent.isEnabledFor(l)) { 268 called = true; 269 parent.callAppenders(event); 270 } 271 for (Iterator it = topicToparents.values().iterator(); it.hasNext();) { 272 org.apache.log4j.Logger c = (org.apache.log4j.Logger) it.next(); 273 if (c instanceof MonologCategory) { 274 called |= ((MonologCategory) c).callAppenders(event, false); 275 } 276 else if (c.isEnabledFor(l)) { 277 called = true; 278 c.callAppenders(event); 279 } 280 } 281 if (called) { 282 for (Enumeration en = getAllAppenders(); 283 en.hasMoreElements();) { 284 ((Appender) en.nextElement()).doAppend(event); 285 } 286 } 287 } 288 return called; 289 } 290 291 292 294 297 public boolean isLoggable(int l) { 298 if (categ != null) { 299 return l >= categ.getEffectiveLevel().toInt(); 300 } 301 return l >= getEffectiveLevel().toInt(); 302 } 303 304 public boolean isLoggable(Level l) { 305 if (categ != null) { 306 return l.getIntValue() >= categ.getEffectiveLevel().toInt(); 307 } 308 return l.getIntValue() >= getEffectiveLevel().toInt(); 309 } 310 311 314 public boolean isOn() { 315 return enable; 316 } 317 318 322 public void log(int l, Object o) { 323 if (!enable || !isLoggable(l)) { 324 return; 325 } 326 if (categ != null) 327 categ.log(convertToLog4jLevel(l), (o == null?o:o.toString()), null); 328 else 329 forcedLog(instanceFQN, convertToLog4jLevel(l), (o == null?o:o.toString()), null); 330 } 331 332 public void log(Level l, Object o) { 333 if (!enable || !isLoggable(l.getIntValue())) { 334 return; 335 } 336 if (categ != null) 337 categ.log(convertToLog4jLevel(l.getIntValue()), 338 (o == null?o:o.toString()), null); 339 else 340 forcedLog(instanceFQN, convertToLog4jLevel(l.getIntValue()), 341 (o == null?o:o.toString()), null); 342 } 343 344 347 public void log(int l, Object o, Throwable t) { 348 if (!enable || !isLoggable(l)) { 349 return; 350 } 351 if (categ != null) 352 categ.log(convertToLog4jLevel(l), 353 (o == null?o:o.toString()), t); 354 else 355 forcedLog(instanceFQN, convertToLog4jLevel(l), 356 (o == null?o:o.toString()), t); 357 } 358 359 public void log(Level l, Object o, Throwable t) { 360 if (!enable || !isLoggable(l.getIntValue())) { 361 return; 362 } 363 if (categ != null) 364 categ.log(convertToLog4jLevel(l.getIntValue()), 365 (o == null?o:o.toString()), t); 366 else 367 forcedLog(instanceFQN, convertToLog4jLevel(l.getIntValue()), 368 (o == null?o:o.toString()), t); 369 } 370 371 375 public void log(int l, Object o, Object location, Object method) { 376 if (!enable || !isLoggable(l)) { 377 return; 378 } 379 if (categ != null) 380 categ.log(convertToLog4jLevel(l), (o == null?o:o.toString()), null); 381 else 382 forcedLog(instanceFQN, convertToLog4jLevel(l), 383 (location == null?"":location.toString()) 384 + (method == null?"":method.toString()) 385 + (o == null?o:o.toString()), 386 null); 387 } 388 389 public void log(Level l, Object o, Object location, Object method) { 390 if (!enable || !isLoggable(l.getIntValue())) { 391 return; 392 } 393 if (categ != null) 394 categ.log(convertToLog4jLevel(l.getIntValue()), (o == null?o:o.toString()), null); 395 else 396 forcedLog(instanceFQN, convertToLog4jLevel(l.getIntValue()), 397 (location == null?"":location.toString()) 398 + (method == null?"":method.toString()) 399 + (o == null?o:o.toString()), 400 null); 401 } 402 403 407 public void log(int l, Object o, Throwable t, Object location, 408 Object method) { 409 if (!enable || !isLoggable(l)) { 410 return; 411 } 412 if (categ != null) 413 categ.log(convertToLog4jLevel(l), (o == null?o:o.toString()), t); 414 else 415 forcedLog(instanceFQN, convertToLog4jLevel(l), 416 (location == null?"":location.toString()) 417 + (method == null?"":method.toString()) 418 + (o == null?o:o.toString()), 419 t); 420 } 421 422 public void log(Level l, Object o, Throwable t, Object location, 423 Object method) { 424 if (!enable || !isLoggable(l.getIntValue())) { 425 return; 426 } 427 if (categ != null) 428 categ.log(convertToLog4jLevel(l.getIntValue()), (o == null?o:o.toString()), t); 429 else 430 forcedLog(instanceFQN, convertToLog4jLevel(l.getIntValue()), 431 (location == null?"":location.toString()) 432 + (method == null?"":method.toString()) 433 + (o == null?o:o.toString()), 434 t); 435 } 436 437 440 public void turnOn() { 441 enable = true; 442 } 443 444 447 public void turnOff() { 448 enable = false; 449 } 450 451 453 456 public void setIntLevel(int level) { 457 if (level == BasicLevel.INHERIT) { 458 if (categ != null) 459 categ.setLevel(null); 460 else 461 super.setLevel(null); 462 return; 463 } 464 if (categ != null) 465 categ.setLevel(convertToLog4jLevel(level)); 466 else 467 super.setLevel(convertToLog4jLevel(level)); 468 } 469 470 public void setLevel(Level l) { 471 if (l == null || l.getIntValue() == BasicLevel.INHERIT) { 472 if (categ != null) 473 categ.setLevel(null); 474 else 475 super.setLevel(null); 476 return; 477 } 478 if (categ != null) 479 categ.setLevel(convertToLog4jLevel(l.getIntValue())); 480 else 481 super.setLevel(convertToLog4jLevel(l.getIntValue())); 482 } 483 484 487 public int getCurrentIntLevel() { 488 if (categ != null) 489 return categ.getLevel().toInt(); 490 else 491 return (level != null ? level.toInt() : BasicLevel.INHERIT); 492 } 493 494 public Level getCurrentLevel() { 495 org.apache.log4j.Level p = null; 496 if (categ != null) 497 p = categ.getLevel(); 498 else 499 p = level; 500 if (p != null) 501 return LevelImpl.getLevel(p.toInt()); 502 else 503 return BasicLevel.LEVEL_INHERIT; 504 } 505 506 509 public void addHandler(Handler h) throws Exception { 510 if (h instanceof Appender) { 511 if (categ != null) 512 categ.addAppender((Appender) h); 513 else 514 super.addAppender((Appender) h); 515 } 516 else 517 throw new UnsupportedOperationException ( 518 "The type of the handler does not match with this wrapper"); 519 } 520 521 524 public void addTopic(String topic) throws Exception { 525 if (categ == null) { 526 Object p = parent; 527 String n = name; 528 org.apache.log4j.Logger.getLogger(topic, new BetaCF(this)); 529 topicToparents.put(topic, parent); 530 parent = (org.apache.log4j.Logger) p; 531 name = n; 532 } 533 } 534 535 public Handler[] getHandler() { 536 ArrayList al = new ArrayList (); 537 if (categ != null) { 538 for (Enumeration en = categ.getAllAppenders(); en.hasMoreElements();) { 539 Appender a = (Appender) en.nextElement(); 540 if (a instanceof Handler) { 541 al.add(a); 542 } 543 else { 544 al.add(new GenericHandler(a)); 545 } 546 } 547 } 548 else { 549 for (Enumeration en = getAllAppenders(); en.hasMoreElements();) { 550 Appender a = (Appender) en.nextElement(); 551 if (a instanceof Handler) { 552 al.add(a); 553 } 554 else { 555 al.add(new GenericHandler(a)); 556 } 557 } 558 } 559 return (Handler[]) al.toArray(new Handler[0]); 560 } 561 562 public Handler getHandler(String hn) { 563 Appender a = null; 564 if (categ != null) { 565 a = categ.getAppender(hn); 566 } 567 else { 568 a = getAppender(hn); 569 } 570 if (a instanceof Handler) { 571 return (Handler) a; 572 } 573 else { 574 return new GenericHandler(a); 575 } 576 } 577 578 public void removeAllHandlers() throws Exception { 579 if (categ != null) { 580 categ.removeAllAppenders(); 581 } 582 else { 583 removeAllAppenders(); 584 } 585 } 586 587 590 public Enumeration getTopics() { 591 return new EnumrationImpl( 592 topicToparents.keySet().toArray(new String [0])); 593 } 594 595 598 public String [] getTopic() { 599 String [] res = null; 600 if (categ != null) { 601 res = new String [1]; 602 res[0] = AbstractFactory.getTopicWithoutPrefix(categ.getName()); 603 return res; 604 } 605 res = new String [topicToparents.size() + 1]; 606 res[0] = AbstractFactory.getTopicWithoutPrefix(name); 607 int i=1; 608 for (Iterator it = topicToparents.keySet().iterator(); it.hasNext();) { 609 res[i] = AbstractFactory.getTopicWithoutPrefix((String ) it.next()); 610 i++; 611 } 612 return res; 613 } 614 615 618 public void removeHandler(Handler h) throws Exception { 619 if (h instanceof Appender) { 620 if (categ != null) { 621 categ.removeAppender((Appender) h); 622 if (h instanceof GenericHandler) { 623 categ.removeAppender(((GenericHandler) h).getAppender()); 624 } 625 } 626 else { 627 super.removeAppender((Appender) h); 628 if (h instanceof GenericHandler) { 629 super.removeAppender(((GenericHandler) h).getAppender()); 630 } 631 } 632 } 633 else 634 throw new UnsupportedOperationException ( 635 "The type of the handler does not match with this wrapper"); 636 } 637 638 641 public void removeTopic(String topic) throws Exception { 642 if (categ == null) 643 topicToparents.remove(topic); 644 } 645 646 public void setName(String n) { 649 name = n; 650 } 651 652 public String getType() { 653 return "logger"; 654 } 655 656 public String [] getAttributeNames() { 657 return new String [0]; 658 } 659 660 public Object getAttribute(String name) { 661 return null; 662 } 663 664 public Object setAttribute(String name, Object value) { 665 return null; 666 } 667 668 public static class OwPriority extends org.apache.log4j.Level { 671 672 protected int level = 10000; 673 674 public OwPriority(int l) { 675 super(l, "INTER", l); 676 this.level = l; 677 } 678 679 public boolean isGreaterOrEqual(Priority pr) { 680 return level >= pr.toInt(); 681 } 682 } 683 684 public static class BetaCF implements org.apache.log4j.spi.LoggerFactory { 685 MonologCategory mc = null; 686 687 public BetaCF(MonologCategory mc) { 688 this.mc = mc; 689 } 690 691 public org.apache.log4j.Logger makeNewLoggerInstance(String name) { 692 return mc; 693 } 694 } 695 696 } 697 | Popular Tags |