1 17 package org.apache.log; 18 19 import org.apache.log.util.LoggerListener; 20 21 27 public class Logger 28 { 29 private static final Logger[] EMPTY_SET = new Logger[ 0 ]; 30 31 34 public static final char CATEGORY_SEPARATOR = '.'; 35 36 private final ErrorHandler m_errorHandler; 38 39 private final LoggerListener m_loggerListener; 41 42 private final Logger m_parent; 44 45 private final String m_category; 47 48 private Logger[] m_children; 50 51 private LogTarget[] m_logTargets; 53 54 private boolean m_logTargetsForceSet; 56 57 private Priority m_priority; 59 60 private boolean m_priorityForceSet; 62 63 67 private boolean m_additivity; 68 69 78 Logger( final ErrorHandler errorHandler, 79 final LoggerListener loggerListener, 80 final String category, 81 final LogTarget[] logTargets, 82 final Logger parent ) 83 { 84 m_errorHandler = errorHandler; 85 m_loggerListener = loggerListener; 86 m_category = category; 87 m_logTargets = logTargets; 88 m_parent = parent; 89 90 if( null == m_logTargets ) 91 { 92 unsetLogTargets(); 93 } 94 95 unsetPriority(); 96 } 97 98 103 public final boolean isDebugEnabled() 104 { 105 return m_priority.isLowerOrEqual( Priority.DEBUG ); 106 } 107 108 114 public final void debug( final String message, final Throwable throwable ) 115 { 116 if( isDebugEnabled() ) 117 { 118 output( Priority.DEBUG, message, throwable ); 119 } 120 } 121 122 127 public final void debug( final String message ) 128 { 129 if( isDebugEnabled() ) 130 { 131 output( Priority.DEBUG, message, null ); 132 } 133 } 134 135 140 public final boolean isInfoEnabled() 141 { 142 return m_priority.isLowerOrEqual( Priority.INFO ); 143 } 144 145 151 public final void info( final String message, final Throwable throwable ) 152 { 153 if( isInfoEnabled() ) 154 { 155 output( Priority.INFO, message, throwable ); 156 } 157 } 158 159 164 public final void info( final String message ) 165 { 166 if( isInfoEnabled() ) 167 { 168 output( Priority.INFO, message, null ); 169 } 170 } 171 172 177 public final boolean isWarnEnabled() 178 { 179 return m_priority.isLowerOrEqual( Priority.WARN ); 180 } 181 182 188 public final void warn( final String message, final Throwable throwable ) 189 { 190 if( isWarnEnabled() ) 191 { 192 output( Priority.WARN, message, throwable ); 193 } 194 } 195 196 201 public final void warn( final String message ) 202 { 203 if( isWarnEnabled() ) 204 { 205 output( Priority.WARN, message, null ); 206 } 207 } 208 209 214 public final boolean isErrorEnabled() 215 { 216 return m_priority.isLowerOrEqual( Priority.ERROR ); 217 } 218 219 225 public final void error( final String message, final Throwable throwable ) 226 { 227 if( isErrorEnabled() ) 228 { 229 output( Priority.ERROR, message, throwable ); 230 } 231 } 232 233 238 public final void error( final String message ) 239 { 240 if( isErrorEnabled() ) 241 { 242 output( Priority.ERROR, message, null ); 243 } 244 } 245 246 251 public final boolean isFatalErrorEnabled() 252 { 253 return m_priority.isLowerOrEqual( Priority.FATAL_ERROR ); 254 } 255 256 262 public final void fatalError( final String message, final Throwable throwable ) 263 { 264 if( isFatalErrorEnabled() ) 265 { 266 output( Priority.FATAL_ERROR, message, throwable ); 267 } 268 } 269 270 275 public final void fatalError( final String message ) 276 { 277 if( isFatalErrorEnabled() ) 278 { 279 output( Priority.FATAL_ERROR, message, null ); 280 } 281 } 282 283 292 public final void setAdditivity( final boolean additivity ) 293 { 294 m_additivity = additivity; 295 } 296 297 302 public final boolean isPriorityEnabled( final Priority priority ) 303 { 304 return m_priority.isLowerOrEqual( priority ); 305 } 306 307 314 public final void log( final Priority priority, 315 final String message, 316 final Throwable throwable ) 317 { 318 if( m_priority.isLowerOrEqual( priority ) ) 319 { 320 output( priority, message, throwable ); 321 } 322 } 323 324 330 public final void log( final Priority priority, final String message ) 331 { 332 if( m_priority.isLowerOrEqual( priority ) ) 333 { 334 log( priority, message, null ); 335 } 336 } 337 338 343 public synchronized void setPriority( final Priority priority ) 344 { 345 m_priority = priority; 346 m_priorityForceSet = true; 347 resetChildPriorities( false ); 348 } 349 350 354 public synchronized void unsetPriority() 355 { 356 unsetPriority( false ); 357 } 358 359 366 public synchronized void unsetPriority( final boolean recursive ) 367 { 368 if( null != m_parent ) 369 { 370 m_priority = m_parent.m_priority; 371 } 372 else 373 { 374 m_priority = Priority.DEBUG; 375 } 376 377 m_priorityForceSet = false; 378 resetChildPriorities( recursive ); 379 } 380 381 386 public synchronized void setLogTargets( final LogTarget[] logTargets ) 387 { 388 if( null != logTargets ) 389 { 390 for( int i = 0; i < logTargets.length; i++ ) 393 { 394 if( null == logTargets[ i ] ) 395 { 396 final String message = "logTargets[ " + i + " ]"; 397 throw new NullPointerException ( message ); 398 } 399 } 400 } 401 402 m_logTargets = logTargets; 403 404 setupErrorHandlers(); 405 m_logTargetsForceSet = true; 406 resetChildLogTargets( false ); 407 } 408 409 414 public synchronized void unsetLogTargets() 415 { 416 unsetLogTargets( false ); 417 } 418 419 425 public synchronized void unsetLogTargets( final boolean recursive ) 426 { 427 if( null != m_parent ) 428 { 429 m_logTargets = m_parent.safeGetLogTargets(); 430 } 431 else 432 { 433 m_logTargets = null; 434 } 435 436 m_logTargetsForceSet = false; 437 resetChildLogTargets( recursive ); 438 } 439 440 445 public synchronized Logger[] getChildren() 446 { 447 if( null == m_children ) 448 { 449 return EMPTY_SET; 450 } 451 452 final Logger[] children = new Logger[ m_children.length ]; 453 454 for( int i = 0; i < children.length; i++ ) 455 { 456 children[ i ] = m_children[ i ]; 457 } 458 459 return children; 460 } 461 462 470 public synchronized Logger getChildLogger( final String subCategory ) 471 throws IllegalArgumentException 472 { 473 final int end = subCategory.indexOf( CATEGORY_SEPARATOR ); 474 475 String nextCategory = null; 476 String remainder = null; 477 478 if( -1 == end ) 479 { 480 nextCategory = subCategory; 481 } 482 else 483 { 484 if( end == 0 ) 485 { 486 throw new IllegalArgumentException ( "Logger categories MUST not have empty elements" ); 487 } 488 489 nextCategory = subCategory.substring( 0, end ); 490 remainder = subCategory.substring( end + 1 ); 491 } 492 493 String category = null; 495 if( m_category.equals( "" ) ) 496 { 497 category = nextCategory; 498 } 499 else 500 { 501 category = m_category + CATEGORY_SEPARATOR + nextCategory; 502 } 503 504 if( null != m_children ) 507 { 508 for( int i = 0; i < m_children.length; i++ ) 509 { 510 if( m_children[ i ].m_category.equals( category ) ) 511 { 512 if( null == remainder ) 513 { 514 return m_children[ i ]; 515 } 516 else 517 { 518 return m_children[ i ].getChildLogger( remainder ); 519 } 520 } 521 } 522 } 523 524 final Logger child = 526 new Logger( m_errorHandler, m_loggerListener, category, null, this ); 527 528 if( m_additivity ) 529 { 530 child.setAdditivity( true ); 531 } 532 533 m_loggerListener.loggerCreated( child.m_category, child ); 534 535 536 if( null == m_children ) 538 { 539 m_children = new Logger[]{child}; 540 } 541 else 542 { 543 final Logger[] children = new Logger[ m_children.length + 1 ]; 544 System.arraycopy( m_children, 0, children, 0, m_children.length ); 545 children[ m_children.length ] = child; 546 m_children = children; 547 } 548 549 if( null == remainder ) 550 { 551 return child; 552 } 553 else 554 { 555 return child.getChildLogger( remainder ); 556 } 557 } 558 559 566 private final void output( final Priority priority, 567 final String message, 568 final Throwable throwable ) 569 { 570 final LogEvent event = new LogEvent(); 571 event.setCategory( m_category ); 572 event.setContextMap( ContextMap.getCurrentContext( false ) ); 573 574 if( null != message ) 575 { 576 event.setMessage( message ); 577 } 578 else 579 { 580 event.setMessage( "" ); 581 } 582 583 event.setThrowable( throwable ); 584 event.setPriority( priority ); 585 586 event.setTime( System.currentTimeMillis() ); 589 590 output( event ); 591 } 592 593 private final void output( final LogEvent event ) 594 { 595 final LogTarget[] targets = m_logTargets; 599 600 if( null == targets ) 601 { 602 final String message = "LogTarget is null for category '" + m_category + "'"; 603 m_errorHandler.error( message, null, event ); 604 } 605 else if( !m_additivity ) 606 { 607 fireEvent( event, targets ); 608 } 609 else 610 { 611 if( m_logTargetsForceSet ) 614 { 615 fireEvent( event, targets ); 616 } 617 618 if( null != m_parent ) 620 { 621 m_parent.output( event ); 622 } 623 } 624 } 625 626 private final void fireEvent( final LogEvent event, final LogTarget[] targets ) 627 { 628 for( int i = 0; i < targets.length; i++ ) 629 { 630 targets[ i ].processEvent( event ); 633 } 634 } 635 636 639 private synchronized void resetChildPriorities( final boolean recursive ) 640 { 641 if( null == m_children ) 642 { 643 return; 644 } 645 646 final Logger[] children = m_children; 647 648 for( int i = 0; i < children.length; i++ ) 649 { 650 children[ i ].resetPriority( recursive ); 651 } 652 } 653 654 660 private synchronized void resetPriority( final boolean recursive ) 661 { 662 if( recursive ) 663 { 664 m_priorityForceSet = false; 665 } 666 else if( m_priorityForceSet ) 667 { 668 return; 669 } 670 671 m_priority = m_parent.m_priority; 672 resetChildPriorities( recursive ); 673 } 674 675 682 private synchronized LogTarget[] safeGetLogTargets() 683 { 684 if( null == m_logTargets ) 685 { 686 if( null == m_parent ) 687 { 688 return new LogTarget[ 0 ]; 689 } 690 else 691 { 692 return m_parent.safeGetLogTargets(); 693 } 694 } 695 else 696 { 697 final LogTarget[] logTargets = new LogTarget[ m_logTargets.length ]; 698 for( int i = 0; i < logTargets.length; i++ ) 699 { 700 logTargets[ i ] = m_logTargets[ i ]; 701 } 702 703 return logTargets; 704 } 705 } 706 707 710 private synchronized void resetChildLogTargets( final boolean recursive ) 711 { 712 if( null == m_children ) 713 { 714 return; 715 } 716 717 for( int i = 0; i < m_children.length; i++ ) 718 { 719 m_children[ i ].resetLogTargets( recursive ); 720 } 721 } 722 723 726 private synchronized void setupErrorHandlers() 727 { 728 if( null == m_logTargets ) 729 { 730 return; 731 } 732 733 for( int i = 0; i < m_logTargets.length; i++ ) 734 { 735 final LogTarget target = m_logTargets[ i ]; 736 if( target instanceof ErrorAware ) 737 { 738 ( (ErrorAware)target ).setErrorHandler( m_errorHandler ); 739 } 740 } 741 } 742 743 749 private synchronized void resetLogTargets( final boolean recursive ) 750 { 751 if( recursive ) 752 { 753 m_logTargetsForceSet = false; 754 } 755 else if( m_logTargetsForceSet ) 756 { 757 return; 758 } 759 760 m_logTargets = m_parent.safeGetLogTargets(); 761 resetChildLogTargets( recursive ); 762 } 763 } 764 | Popular Tags |