1 21 package oracle.toplink.essentials.logging; 23 24 import java.util.*; 25 import java.text.*; 26 import java.io.*; 27 import oracle.toplink.essentials.sessions.Session; 28 import oracle.toplink.essentials.internal.databaseaccess.Accessor; 29 import oracle.toplink.essentials.internal.localization.*; 30 import oracle.toplink.essentials.exceptions.*; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 33 45 public abstract class AbstractSessionLog implements SessionLog, java.lang.Cloneable { 46 47 50 protected int level; 51 52 55 protected static SessionLog defaultLog; 56 57 60 protected Session session; 61 62 65 protected String sessionType; 66 67 70 protected String sessionHashCode; 71 72 75 protected static String SEVERE_PREFIX = null; 76 77 80 protected static String WARNING_PREFIX = null; 81 82 85 protected static String INFO_PREFIX = null; 86 87 90 protected static String CONFIG_PREFIX = null; 91 92 95 protected static String FINE_PREFIX = null; 96 97 100 protected static String FINER_PREFIX = null; 101 102 105 protected static String FINEST_PREFIX = null; 106 107 110 protected static String TOPLINK_PREFIX = null; 111 112 115 protected static String CONNECTION_STRING = "Connection"; 116 117 120 protected static String THREAD_STRING = "Thread"; 121 122 125 protected Writer writer; 126 127 130 protected DateFormat dateFormat; 131 132 137 protected Boolean shouldLogExceptionStackTrace; 138 139 144 protected Boolean shouldPrintDate; 145 146 151 protected Boolean shouldPrintThread; 152 153 158 protected Boolean shouldPrintSession; 159 160 165 protected Boolean shouldPrintConnection; 166 167 171 public AbstractSessionLog() { 172 this.writer = new PrintWriter(System.out); 173 } 174 175 184 public int getLevel() { 185 return getLevel(null); 186 } 187 188 200 public int getLevel(String category) { 201 return level; 202 } 203 204 213 public void setLevel(int level) { 214 setLevel(level, null); 215 } 216 217 228 public void setLevel(int level, String category) { 229 this.level = level; 230 } 231 232 244 public boolean shouldLog(int level) { 245 return shouldLog(level, null); 246 } 247 248 263 public boolean shouldLog(int level, String category) { 264 return (this.level <= level) && !isOff(); 265 } 266 267 277 public static SessionLog getLog() { 278 if (defaultLog == null) { 279 defaultLog = new DefaultSessionLog(); 280 } 281 return defaultLog; 282 } 283 284 293 public static void setLog(SessionLog sessionLog) { 294 defaultLog = sessionLog; 295 defaultLog.setSession(null); 296 } 297 298 307 public Session getSession() { 308 return this.session; 309 } 310 311 320 public void setSession(Session session) { 321 if (this.session == null) { 322 this.session = session; 323 buildSessionType(); 324 buildSessionHashCode(); 325 } 326 } 327 328 340 public void log(int level, String message) { 341 if (!shouldLog(level)) { 342 return; 343 } 344 log(level, message, (Object [])null, false); 346 } 347 348 361 public void log(int level, String message, Object param) { 362 if (!shouldLog(level)) { 363 return; 364 } 365 log(level, message, new Object [] { param }); 366 } 367 368 383 public void log(int level, String message, Object param1, Object param2) { 384 if (!shouldLog(level)) { 385 return; 386 } 387 log(level, message, new Object [] { param1, param2 }); 388 } 389 390 407 public void log(int level, String message, Object param1, Object param2, Object param3) { 408 if (!shouldLog(level)) { 409 return; 410 } 411 log(level, message, new Object [] { param1, param2, param3 }); 412 } 413 414 427 public void log(int level, String message, Object [] params) { 428 log(level, message, params, true); 429 } 430 431 446 public void log(int level, String message, Object [] params, boolean shouldTranslate) { 447 if (!shouldLog(level)) { 448 return; 449 } 450 log(new SessionLogEntry(level, null, message, params, null, shouldTranslate)); 451 } 452 453 462 public abstract void log(SessionLogEntry sessionLogEntry); 463 464 468 public boolean shouldPrintSession() { 469 return (shouldPrintSession == null) || shouldPrintSession.booleanValue(); 470 } 471 472 476 public void setShouldPrintSession(boolean shouldPrintSession) { 477 if (shouldPrintSession) { 478 this.shouldPrintSession = Boolean.TRUE; 479 } else { 480 this.shouldPrintSession = Boolean.FALSE; 481 } 482 } 483 484 487 public boolean shouldPrintConnection() { 488 return (shouldPrintConnection == null) || shouldPrintConnection.booleanValue(); 489 } 490 491 494 public void setShouldPrintConnection(boolean shouldPrintConnection) { 495 if (shouldPrintConnection) { 496 this.shouldPrintConnection = Boolean.TRUE; 497 } else { 498 this.shouldPrintConnection = Boolean.FALSE; 499 } 500 } 501 502 506 public boolean shouldLogExceptionStackTrace() { 507 if (shouldLogExceptionStackTrace == null) { 508 return getLevel() <= FINER; 509 } else { 510 return shouldLogExceptionStackTrace.booleanValue(); 511 } 512 } 513 514 518 public void setShouldLogExceptionStackTrace(boolean shouldLogExceptionStackTrace) { 519 if (shouldLogExceptionStackTrace) { 520 this.shouldLogExceptionStackTrace = Boolean.TRUE; 521 } else { 522 this.shouldLogExceptionStackTrace = Boolean.FALSE; 523 } 524 } 525 526 529 public boolean shouldPrintDate() { 530 return (shouldPrintDate == null) || (shouldPrintDate.booleanValue()); 531 } 532 533 536 public void setShouldPrintDate(boolean shouldPrintDate) { 537 if (shouldPrintDate) { 538 this.shouldPrintDate = Boolean.TRUE; 539 } else { 540 this.shouldPrintDate = Boolean.FALSE; 541 } 542 } 543 544 548 public boolean shouldPrintThread() { 549 if (shouldPrintThread == null) { 550 return getLevel() <= FINE; 551 } else { 552 return shouldPrintThread.booleanValue(); 553 } 554 } 555 556 560 public void setShouldPrintThread(boolean shouldPrintThread) { 561 if (shouldPrintThread) { 562 this.shouldPrintThread = Boolean.TRUE; 563 } else { 564 this.shouldPrintThread = Boolean.FALSE; 565 } 566 } 567 568 577 public Writer getWriter() { 578 return writer; 579 } 580 581 590 public void setWriter(Writer writer) { 591 this.writer = writer; 592 } 593 594 599 protected DateFormat buildDefaultDateFormat() { 600 return new SimpleDateFormat("yyyy.MM.dd hh:mm:ss.SSS"); 601 } 602 603 608 public DateFormat getDateFormat() { 609 if (dateFormat == null) { 610 dateFormat = this.buildDefaultDateFormat(); 611 } 612 return dateFormat; 613 } 614 615 619 protected String getDateString(Date date) { 620 return this.getDateFormat().format(date); 621 } 622 623 626 protected String getSupplementDetailString(SessionLogEntry entry) { 627 StringWriter writer = new StringWriter(); 628 629 if (shouldPrintDate()) { 630 writer.write(getDateString(entry.getDate())); 631 writer.write("--"); 632 } 633 if (shouldPrintSession() && (entry.getSession() != null)) { 634 writer.write(this.getSessionString(entry.getSession())); 635 writer.write("--"); 636 } 637 if (shouldPrintConnection() && (entry.getConnection() != null)) { 638 writer.write(this.getConnectionString(entry.getConnection())); 639 writer.write("--"); 640 } 641 if (shouldPrintThread()) { 642 writer.write(this.getThreadString(entry.getThread())); 643 writer.write("--"); 644 } 645 return writer.toString(); 646 } 647 648 651 protected String getSessionString(Session session) { 652 if (session != null) { 656 return ((AbstractSession)session).getLogSessionString(); 657 } else { 658 return getSessionString(); 659 } 660 } 661 662 665 protected void buildSessionType() { 666 if (session != null) { 667 sessionType = ((AbstractSession)session).getSessionTypeString(); 668 } else { 669 sessionType = null; 670 } 671 } 672 673 676 protected void buildSessionHashCode() { 677 if (session != null) { 678 sessionHashCode = String.valueOf(System.identityHashCode(session)); 679 } else { 680 sessionHashCode = null; 681 } 682 } 683 684 687 protected String getSessionString() { 688 return sessionType + "(" + sessionHashCode + ")"; 689 } 690 691 694 protected String getConnectionString(Accessor connection) { 695 if (connection.getDatasourceConnection() == null){ 697 return CONNECTION_STRING + "(" + String.valueOf(System.identityHashCode(connection)) + ")"; 698 } else { 699 return CONNECTION_STRING + "(" + String.valueOf(System.identityHashCode(connection.getDatasourceConnection())) + ")"; 700 } 701 } 702 703 706 protected String getThreadString(Thread thread) { 707 return THREAD_STRING + "(" + String.valueOf(thread) + ")"; 708 } 709 710 713 714 protected void printPrefixString(int level) { 716 try { 717 switch (level) { 718 case SEVERE: 719 if (SEVERE_PREFIX == null) { 720 SEVERE_PREFIX = LoggingLocalization.buildMessage("toplink_severe"); 721 } 722 this.getWriter().write(SEVERE_PREFIX); 723 break; 724 case WARNING: 725 if (WARNING_PREFIX == null) { 726 WARNING_PREFIX = LoggingLocalization.buildMessage("toplink_warning"); 727 } 728 this.getWriter().write(WARNING_PREFIX); 729 break; 730 case INFO: 731 if (INFO_PREFIX == null) { 732 INFO_PREFIX = LoggingLocalization.buildMessage("toplink_info"); 733 } 734 this.getWriter().write(INFO_PREFIX); 735 break; 736 case CONFIG: 737 if (CONFIG_PREFIX == null) { 738 CONFIG_PREFIX = LoggingLocalization.buildMessage("toplink_config"); 739 } 740 this.getWriter().write(CONFIG_PREFIX); 741 break; 742 case FINE: 743 if (FINE_PREFIX == null) { 744 FINE_PREFIX = LoggingLocalization.buildMessage("toplink_fine"); 745 } 746 this.getWriter().write(FINE_PREFIX); 747 break; 748 case FINER: 749 if (FINER_PREFIX == null) { 750 FINER_PREFIX = LoggingLocalization.buildMessage("toplink_finer"); 751 } 752 this.getWriter().write(FINER_PREFIX); 753 break; 754 case FINEST: 755 if (FINEST_PREFIX == null) { 756 FINEST_PREFIX = LoggingLocalization.buildMessage("toplink_finest"); 757 } 758 this.getWriter().write(FINEST_PREFIX); 759 break; 760 default: 761 if (TOPLINK_PREFIX == null) { 762 TOPLINK_PREFIX = LoggingLocalization.buildMessage("toplink"); 763 } 764 this.getWriter().write(TOPLINK_PREFIX); 765 } 766 } catch (IOException exception) { 767 throw ValidationException.logIOError(exception); 768 } 769 } 770 771 777 public void setDateFormat(DateFormat dateFormat) { 778 this.dateFormat = dateFormat; 779 } 780 781 786 protected String formatMessage(SessionLogEntry entry) { 787 String message = entry.getMessage(); 788 if (entry.shouldTranslate()) { 789 if (entry.getLevel() > FINE) { 790 message = LoggingLocalization.buildMessage(message, entry.getParameters()); 791 } else { 792 message = TraceLocalization.buildMessage(message, entry.getParameters()); 793 } 794 } else { 795 if (message.indexOf("{0") >= 0) { 796 message = java.text.MessageFormat.format(message, entry.getParameters()); 797 } 798 } 799 return message; 800 } 801 802 811 public void throwing(Throwable throwable) { 812 if (shouldLog(FINER)) { 813 SessionLogEntry entry = new SessionLogEntry(null, throwable); 814 entry.setLevel(FINER); 815 log(entry); 816 } 817 } 818 819 824 public static int translateStringToLoggingLevel(String loggingLevel){ 825 if (loggingLevel == null){ 826 return INFO; 827 } else if (loggingLevel.equals("OFF")){ 828 return OFF; 829 } else if (loggingLevel.equals("SEVERE")){ 830 return SEVERE; 831 } else if (loggingLevel.equals("WARNING")){ 832 return WARNING; 833 } else if (loggingLevel.equals("INFO")){ 834 return INFO; 835 } else if (loggingLevel.equals("CONFIG")){ 836 return CONFIG; 837 } else if (loggingLevel.equals("FINE")){ 838 return FINE; 839 } else if (loggingLevel.equals("FINER")){ 840 return FINER; 841 } else if (loggingLevel.equals("FINEST")){ 842 return FINEST; 843 } else if (loggingLevel.equals("ALL")){ 844 return ALL; 845 } 846 return INFO; 847 } 848 849 854 public static String translateLoggingLevelToString(int loggingLevel){ 855 if (loggingLevel == OFF){ 856 return "OFF"; 857 } else if (loggingLevel == SEVERE){ 858 return "SEVERE"; 859 } else if (loggingLevel == WARNING){ 860 return "WARNING"; 861 } else if (loggingLevel == INFO){ 862 return "INFO"; 863 } else if (loggingLevel == CONFIG){ 864 return "CONFIG"; 865 } else if (loggingLevel == FINE){ 866 return "FINE"; 867 } else if (loggingLevel == FINER){ 868 return "FINER"; 869 } else if (loggingLevel == FINEST){ 870 return "FINEST"; 871 } else if (loggingLevel == ALL){ 872 return "ALL"; 873 } 874 return "INFO"; 875 } 876 877 887 public void severe(String message) { 888 log(SEVERE, message, (Object [])null); 889 } 890 891 901 public void warning(String message) { 902 log(WARNING, message, (Object [])null); 903 } 904 905 915 public void info(String message) { 916 log(INFO, message, (Object [])null); 917 } 918 919 929 public void config(String message) { 930 log(CONFIG, message, (Object [])null); 931 } 932 933 943 public void fine(String message) { 944 log(FINE, message, (Object [])null); 945 } 946 947 957 public void finer(String message) { 958 log(FINER, message, (Object [])null); 959 } 960 961 971 public void finest(String message) { 972 log(FINEST, message, (Object [])null); 973 } 974 975 986 public void logThrowable(int level, Throwable throwable) { 987 if (shouldLog(level)) { 989 log(new SessionLogEntry(null, level, null, throwable)); 990 } 991 } 992 993 997 public boolean isOff() { 998 return this.level == OFF; 999 } 1000 1001 1005 public Object clone() { 1006 try { 1007 return super.clone(); 1008 } catch (Exception exception) { 1009 return null; 1010 } 1011 } 1012} 1013 | Popular Tags |