1 8 package org.jivesoftware.util.log; 9 10 15 public class Logger { 16 public final static char CATEGORY_SEPARATOR = '.'; 18 19 private final ErrorHandler m_errorHandler; 21 22 private final Logger m_parent; 24 25 private final String m_category; 27 28 private Logger[] m_children; 30 31 private LogTarget[] m_logTargets; 33 34 private boolean m_logTargetsForceSet; 36 37 private Priority m_priority; 39 40 private boolean m_priorityForceSet; 42 43 47 private boolean m_additivity; 48 49 58 Logger(final ErrorHandler errorHandler, 59 final String category, 60 final LogTarget[] logTargets, 61 final Logger parent) { 62 m_errorHandler = errorHandler; 63 m_category = category; 64 m_logTargets = logTargets; 65 m_parent = parent; 66 67 if (null == m_logTargets) { 68 unsetLogTargets(); 69 } 70 71 unsetPriority(); 72 } 73 74 79 public final boolean isDebugEnabled() { 80 return m_priority.isLowerOrEqual(Priority.DEBUG); 81 } 82 83 89 public final void debug(final String message, final Throwable throwable) { 90 if (isDebugEnabled()) { 91 output(Priority.DEBUG, message, throwable); 92 } 93 } 94 95 100 public final void debug(final String message) { 101 if (isDebugEnabled()) { 102 output(Priority.DEBUG, message, null); 103 } 104 } 105 106 111 public final boolean isInfoEnabled() { 112 return m_priority.isLowerOrEqual(Priority.INFO); 113 } 114 115 120 public final void info(final String message, final Throwable throwable) { 121 if (isInfoEnabled()) { 122 output(Priority.INFO, message, throwable); 123 } 124 } 125 126 131 public final void info(final String message) { 132 if (isInfoEnabled()) { 133 output(Priority.INFO, message, null); 134 } 135 } 136 137 142 public final boolean isWarnEnabled() { 143 return m_priority.isLowerOrEqual(Priority.WARN); 144 } 145 146 152 public final void warn(final String message, final Throwable throwable) { 153 if (isWarnEnabled()) { 154 output(Priority.WARN, message, throwable); 155 } 156 } 157 158 163 public final void warn(final String message) { 164 if (isWarnEnabled()) { 165 output(Priority.WARN, message, null); 166 } 167 } 168 169 174 public final boolean isErrorEnabled() { 175 return m_priority.isLowerOrEqual(Priority.ERROR); 176 } 177 178 184 public final void error(final String message, final Throwable throwable) { 185 if (isErrorEnabled()) { 186 output(Priority.ERROR, message, throwable); 187 } 188 } 189 190 195 public final void error(final String message) { 196 if (isErrorEnabled()) { 197 output(Priority.ERROR, message, null); 198 } 199 } 200 201 206 public final boolean isFatalErrorEnabled() { 207 return m_priority.isLowerOrEqual(Priority.FATAL_ERROR); 208 } 209 210 216 public final void fatalError(final String message, final Throwable throwable) { 217 if (isFatalErrorEnabled()) { 218 output(Priority.FATAL_ERROR, message, throwable); 219 } 220 } 221 222 227 public final void fatalError(final String message) { 228 if (isFatalErrorEnabled()) { 229 output(Priority.FATAL_ERROR, message, null); 230 } 231 } 232 233 242 public final void setAdditivity(final boolean additivity) { 243 m_additivity = additivity; 244 } 245 246 251 public final boolean isPriorityEnabled(final Priority priority) { 252 return m_priority.isLowerOrEqual(priority); 253 } 254 255 262 public final void log(final Priority priority, 263 final String message, 264 final Throwable throwable) { 265 if (m_priority.isLowerOrEqual(priority)) { 266 output(priority, message, throwable); 267 } 268 } 269 270 276 public final void log(final Priority priority, final String message) { 277 log(priority, message, null); 278 } 279 280 285 public synchronized void setPriority(final Priority priority) { 286 m_priority = priority; 287 m_priorityForceSet = true; 288 resetChildPriorities(false); 289 } 290 291 295 public synchronized void unsetPriority() { 296 unsetPriority(false); 297 } 298 299 306 public synchronized void unsetPriority(final boolean recursive) { 307 if (null != m_parent) 308 m_priority = m_parent.m_priority; 309 else 310 m_priority = Priority.DEBUG; 311 312 m_priorityForceSet = false; 313 resetChildPriorities(recursive); 314 } 315 316 321 public synchronized void setLogTargets(final LogTarget[] logTargets) { 322 m_logTargets = logTargets; 323 setupErrorHandlers(); 324 m_logTargetsForceSet = true; 325 resetChildLogTargets(false); 326 } 327 328 333 public synchronized void unsetLogTargets() { 334 unsetLogTargets(false); 335 } 336 337 342 public synchronized void unsetLogTargets(final boolean recursive) { 343 if (null != m_parent) 344 m_logTargets = m_parent.safeGetLogTargets(); 345 else 346 m_logTargets = null; 347 348 m_logTargetsForceSet = false; 349 resetChildLogTargets(recursive); 350 } 351 352 357 public synchronized Logger[] getChildren() { 358 if (null == m_children) return new Logger[0]; 359 360 final Logger[] children = new Logger[m_children.length]; 361 362 for (int i = 0; i < children.length; i++) { 363 children[i] = m_children[i]; 364 } 365 366 return children; 367 } 368 369 377 public synchronized Logger getChildLogger(final String subCategory) 378 throws IllegalArgumentException { 379 final int end = subCategory.indexOf(CATEGORY_SEPARATOR); 380 381 String nextCategory = null; 382 String remainder = null; 383 384 if (-1 == end) 385 nextCategory = subCategory; 386 else { 387 if (end == 0) { 388 throw new IllegalArgumentException ("Logger categories MUST not have empty elements"); 389 } 390 391 nextCategory = subCategory.substring(0, end); 392 remainder = subCategory.substring(end + 1); 393 } 394 395 String category = null; 397 if (m_category.equals("")) 398 category = nextCategory; 399 else { 400 category = m_category + CATEGORY_SEPARATOR + nextCategory; 401 } 402 403 if (null != m_children) { 406 for (int i = 0; i < m_children.length; i++) { 407 if (m_children[i].m_category.equals(category)) { 408 if (null == remainder) 409 return m_children[i]; 410 else 411 return m_children[i].getChildLogger(remainder); 412 } 413 } 414 } 415 416 final Logger child = new Logger(m_errorHandler, category, null, this); 418 419 if (null == m_children) { 421 m_children = new Logger[]{child}; 422 } 423 else { 424 final Logger[] children = new Logger[m_children.length + 1]; 425 System.arraycopy(m_children, 0, children, 0, m_children.length); 426 children[m_children.length] = child; 427 m_children = children; 428 } 429 430 if (null == remainder) 431 return child; 432 else 433 return child.getChildLogger(remainder); 434 } 435 436 445 public final Priority getPriority() { 446 return m_priority; 447 } 448 449 457 public final String getCategory() { 458 return m_category; 459 } 460 461 466 public LogTarget[] getLogTargets() { 467 return m_logTargets; 470 } 471 472 479 private final void output(final Priority priority, 480 final String message, 481 final Throwable throwable) { 482 final LogEvent event = new LogEvent(); 483 event.setCategory(m_category); 484 event.setContextMap(ContextMap.getCurrentContext(false)); 486 487 if (null != message) { 488 event.setMessage(message); 489 } 490 else { 491 event.setMessage(""); 492 } 493 494 event.setThrowable(throwable); 495 event.setPriority(priority); 496 497 event.setTime(System.currentTimeMillis()); 500 501 output(event); 502 } 503 504 private final void output(final LogEvent event) { 505 final LogTarget[] targets = m_logTargets; 509 510 if (null == targets) { 511 final String message = "LogTarget is null for category '" + m_category + "'"; 512 m_errorHandler.error(message, null, event); 513 } 514 else if (!m_additivity) { 515 fireEvent(event, targets); 516 } 517 else { 518 if (m_logTargetsForceSet) { 521 fireEvent(event, targets); 522 } 523 524 if (null != m_parent) { 526 m_parent.output(event); 527 } 528 } 529 } 530 531 private final void fireEvent(final LogEvent event, final LogTarget[] targets) { 532 for (int i = 0; i < targets.length; i++) { 533 targets[i].processEvent(event); 536 } 537 } 538 539 542 private synchronized void resetChildPriorities(final boolean recursive) { 543 if (null == m_children) return; 544 545 final Logger[] children = m_children; 546 547 for (int i = 0; i < children.length; i++) { 548 children[i].resetPriority(recursive); 549 } 550 } 551 552 557 private synchronized void resetPriority(final boolean recursive) { 558 if (recursive) { 559 m_priorityForceSet = false; 560 } 561 else if (m_priorityForceSet) { 562 return; 563 } 564 565 m_priority = m_parent.m_priority; 566 resetChildPriorities(recursive); 567 } 568 569 576 private synchronized LogTarget[] safeGetLogTargets() { 577 if (null == m_logTargets) { 578 if (null == m_parent) 579 return new LogTarget[0]; 580 else 581 return m_parent.safeGetLogTargets(); 582 } 583 else { 584 final LogTarget[] logTargets = new LogTarget[m_logTargets.length]; 585 586 for (int i = 0; i < logTargets.length; i++) { 587 logTargets[i] = m_logTargets[i]; 588 } 589 590 return logTargets; 591 } 592 } 593 594 597 private synchronized void resetChildLogTargets(final boolean recursive) { 598 if (null == m_children) return; 599 600 for (int i = 0; i < m_children.length; i++) { 601 m_children[i].resetLogTargets(recursive); 602 } 603 } 604 605 608 private synchronized void setupErrorHandlers() { 609 if (null == m_logTargets) return; 610 611 for (int i = 0; i < m_logTargets.length; i++) { 612 final LogTarget target = m_logTargets[i]; 613 if (target instanceof ErrorAware) { 614 ((ErrorAware)target).setErrorHandler(m_errorHandler); 615 } 616 } 617 } 618 619 624 private synchronized void resetLogTargets(final boolean recursive) { 625 if (recursive) { 626 m_logTargetsForceSet = false; 627 } 628 else if (m_logTargetsForceSet) { 629 return; 630 } 631 632 m_logTargets = m_parent.safeGetLogTargets(); 633 resetChildLogTargets(recursive); 634 } 635 } | Popular Tags |