1 21 package oracle.toplink.essentials.logging; 23 24 import java.util.*; 25 import java.util.logging.XMLFormatter ; 26 import java.util.logging.LogRecord ; 27 import java.util.logging.Level ; 28 29 34 public class TopLinkXMLFormatter extends XMLFormatter { 35 private void a2(StringBuffer sb, int x) { 37 if (x < 10) { 38 sb.append('0'); 39 } 40 sb.append(x); 41 } 42 43 private void appendISO8601(StringBuffer sb, long millis) { 45 Calendar date = Calendar.getInstance(); 46 date.setTime(new Date(millis)); 47 sb.append(date.get(Calendar.YEAR)); 48 sb.append('-'); 49 a2(sb, date.get(Calendar.MONTH) + 1); 50 sb.append('-'); 51 a2(sb, date.get(Calendar.DATE)); 52 sb.append('T'); 53 a2(sb, date.get(Calendar.HOUR_OF_DAY)); 54 sb.append(':'); 55 a2(sb, date.get(Calendar.MINUTE)); 56 sb.append(':'); 57 a2(sb, date.get(Calendar.SECOND)); 58 } 59 60 private void escape(StringBuffer sb, String text) { 64 if (text == null) { 65 text = "<null>"; 66 } 67 for (int i = 0; i < text.length(); i++) { 68 char ch = text.charAt(i); 69 if (ch == '<') { 70 sb.append("<"); 71 } else if (ch == '>') { 72 sb.append(">"); 73 } else if (ch == '&') { 74 sb.append("&"); 75 } else { 76 sb.append(ch); 77 } 78 } 79 } 80 81 86 public String format(LogRecord record0) { 87 if (!(record0 instanceof TopLinkLogRecord)) { 88 return super.format(record0); 89 } else { 90 TopLinkLogRecord record = (TopLinkLogRecord)record0; 91 92 StringBuffer sb = new StringBuffer (500); 93 sb.append("<record>\n"); 94 95 if (record.shouldPrintDate()) { 96 sb.append(" <date>"); 97 appendISO8601(sb, record.getMillis()); 98 sb.append("</date>\n"); 99 100 sb.append(" <millis>"); 101 sb.append(record.getMillis()); 102 sb.append("</millis>\n"); 103 } 104 105 sb.append(" <sequence>"); 106 sb.append(record.getSequenceNumber()); 107 sb.append("</sequence>\n"); 108 109 String name = record.getLoggerName(); 110 if (name != null) { 111 sb.append(" <logger>"); 112 escape(sb, name); 113 sb.append("</logger>\n"); 114 } 115 116 sb.append(" <level>"); 117 escape(sb, record.getLevel().toString()); 118 sb.append("</level>\n"); 119 120 if (record.getSourceClassName() != null) { 121 sb.append(" <class>"); 122 escape(sb, record.getSourceClassName()); 123 sb.append("</class>\n"); 124 } 125 126 if (record.getSourceMethodName() != null) { 127 sb.append(" <method>"); 128 escape(sb, record.getSourceMethodName()); 129 sb.append("</method>\n"); 130 } 131 132 if (record.getSessionString() != null) { 133 sb.append(" <session>"); 134 sb.append(record.getSessionString()); 135 sb.append("</session>\n"); 136 } 137 138 if (record.getConnection() != null) { 139 sb.append(" <connection>"); 140 sb.append(String.valueOf(System.identityHashCode(record.getConnection()))); 141 sb.append("</connection>\n"); 142 } 143 144 if (record.shouldPrintThread()) { 145 sb.append(" <thread>"); 146 sb.append(record.getThreadID()); 147 sb.append("</thread>\n"); 148 } 149 150 if (record.getMessage() != null) { 151 String message = formatMessage(record); 153 sb.append(" <message>"); 154 escape(sb, message); 155 sb.append("</message>"); 156 sb.append("\n"); 157 } 158 159 ResourceBundle bundle = record.getResourceBundle(); 162 try { 163 if ((bundle != null) && (bundle.getString(record.getMessage()) != null)) { 164 sb.append(" <key>"); 165 escape(sb, record.getMessage()); 166 sb.append("</key>\n"); 167 sb.append(" <catalog>"); 168 escape(sb, record.getResourceBundleName()); 169 sb.append("</catalog>\n"); 170 Object [] parameters = record.getParameters(); 171 for (int i = 0; i < parameters.length; i++) { 172 sb.append(" <param>"); 173 try { 174 escape(sb, parameters[i].toString()); 175 } catch (Exception ex) { 176 sb.append("???"); 177 } 178 sb.append("</param>\n"); 179 } 180 } 181 } catch (Exception ex) { 182 } 184 185 if (record.getThrown() != null) { 186 Throwable th = record.getThrown(); 188 sb.append(" <exception>\n"); 189 sb.append(" <message>"); 190 escape(sb, th.toString()); 191 sb.append("</message>\n"); 192 193 if ((record.getLevel().intValue() == Level.SEVERE.intValue()) || 194 ((record.getLevel().intValue() <= Level.WARNING.intValue()) && record.shouldLogExceptionStackTrace())) { 195 StackTraceElement [] trace = th.getStackTrace(); 196 for (int i = 0; i < trace.length; i++) { 197 StackTraceElement frame = trace[i]; 198 sb.append(" <frame>\n"); 199 sb.append(" <class>"); 200 escape(sb, frame.getClassName()); 201 sb.append("</class>\n"); 202 sb.append(" <method>"); 203 escape(sb, frame.getMethodName()); 204 sb.append("</method>\n"); 205 if (frame.getLineNumber() >= 0) { 207 sb.append(" <line>"); 208 sb.append(frame.getLineNumber()); 209 sb.append("</line>\n"); 210 } 211 sb.append(" </frame>\n"); 212 } 213 } 214 215 sb.append(" </exception>\n"); 216 } 217 218 sb.append("</record>\n"); 219 return sb.toString(); 220 } 221 } 222 } 223 | Popular Tags |