1 21 package oracle.toplink.essentials.logging; 23 24 import java.util.logging.SimpleFormatter ; 25 import java.util.logging.LogRecord ; 26 import java.util.logging.Level ; 27 import java.io.*; 28 import java.text.*; 29 import java.util.Date ; 30 import oracle.toplink.essentials.internal.security.*; 31 32 38 public class TopLinkSimpleFormatter extends SimpleFormatter { 39 Date dat = new Date (); 40 private final static String format = "{0,date} {0,time}"; 41 private MessageFormat formatter; 42 private Object [] args = new Object [1]; 43 44 private String lineSeparator = (String )PrivilegedAccessHelper.getLineSeparator(); 47 48 53 public synchronized String format(LogRecord record0) { 54 if (!(record0 instanceof TopLinkLogRecord)) { 55 return super.format(record0); 56 } else { 57 TopLinkLogRecord record = (TopLinkLogRecord)record0; 58 59 StringBuffer sb = new StringBuffer (); 60 61 if (record.shouldPrintDate()) { 62 dat.setTime(record.getMillis()); 64 args[0] = dat; 65 StringBuffer text = new StringBuffer (); 66 if (formatter == null) { 67 formatter = new MessageFormat(format); 68 } 69 formatter.format(args, text, null); 70 sb.append(text); 71 sb.append(" "); 72 } 73 if (record.getSourceClassName() != null) { 74 sb.append(record.getSourceClassName()); 75 } else { 76 sb.append(record.getLoggerName()); 77 } 78 if (record.getSourceMethodName() != null) { 79 sb.append(" "); 80 sb.append(record.getSourceMethodName()); 81 } 82 if (record.getSessionString() != null) { 83 sb.append(" "); 84 sb.append(record.getSessionString()); 85 } 86 if (record.getConnection() != null) { 87 sb.append(" "); 88 sb.append(AbstractSessionLog.CONNECTION_STRING + "(" + String.valueOf(System.identityHashCode(record.getConnection())) + ")"); 89 } 90 if (record.shouldPrintThread()) { 91 sb.append(" "); 92 sb.append(AbstractSessionLog.THREAD_STRING + "(" + String.valueOf(record.getThreadID()) + ")"); 93 } 94 sb.append(lineSeparator); 95 String message = formatMessage(record); 96 sb.append(record.getLevel().getLocalizedName()); 97 sb.append(": "); 98 sb.append(message); 99 sb.append(lineSeparator); 100 if (record.getThrown() != null) { 101 try { 102 StringWriter sw = new StringWriter(); 103 PrintWriter pw = new PrintWriter(sw); 104 if (record.getLevel().intValue() == Level.SEVERE.intValue()) { 105 record.getThrown().printStackTrace(pw); 106 } else if (record.getLevel().intValue() <= Level.WARNING.intValue()) { 107 if (record.shouldLogExceptionStackTrace()) { 108 record.getThrown().printStackTrace(pw); 109 } else { 110 pw.write(record.getThrown().toString()); 111 pw.write(lineSeparator); 112 } 113 } 114 pw.close(); 115 sb.append(sw.toString()); 116 } catch (Exception ex) { 117 } 118 } 119 return sb.toString(); 120 } 121 } 122 } 123 | Popular Tags |