1 7 8 9 package java.util.logging; 10 11 import java.io.*; 12 import java.nio.charset.Charset ; 13 import java.util.*; 14 15 28 29 public class XMLFormatter extends Formatter { 30 private LogManager manager = LogManager.getLogManager(); 31 32 private void a2(StringBuffer sb, int x) { 34 if (x < 10) { 35 sb.append('0'); 36 } 37 sb.append(x); 38 } 39 40 private void appendISO8601(StringBuffer sb, long millis) { 42 Date date = new Date(millis); 43 sb.append(date.getYear() + 1900); 44 sb.append('-'); 45 a2(sb, date.getMonth() + 1); 46 sb.append('-'); 47 a2(sb, date.getDate()); 48 sb.append('T'); 49 a2(sb, date.getHours()); 50 sb.append(':'); 51 a2(sb, date.getMinutes()); 52 sb.append(':'); 53 a2(sb, date.getSeconds()); 54 } 55 56 private void escape(StringBuffer sb, String text) { 60 if (text == null) { 61 text = "<null>"; 62 } 63 for (int i = 0; i < text.length(); i++) { 64 char ch = text.charAt(i); 65 if (ch == '<') { 66 sb.append("<"); 67 } else if (ch == '>') { 68 sb.append(">"); 69 } else if (ch == '&') { 70 sb.append("&"); 71 } else { 72 sb.append(ch); 73 } 74 } 75 } 76 77 82 public String format(LogRecord record) { 83 StringBuffer sb = new StringBuffer (500); 84 sb.append("<record>\n"); 85 86 sb.append(" <date>"); 87 appendISO8601(sb, record.getMillis()); 88 sb.append("</date>\n"); 89 90 sb.append(" <millis>"); 91 sb.append(record.getMillis()); 92 sb.append("</millis>\n"); 93 94 sb.append(" <sequence>"); 95 sb.append(record.getSequenceNumber()); 96 sb.append("</sequence>\n"); 97 98 String name = record.getLoggerName(); 99 if (name != null) { 100 sb.append(" <logger>"); 101 escape(sb, name); 102 sb.append("</logger>\n"); 103 } 104 105 sb.append(" <level>"); 106 escape(sb, record.getLevel().toString()); 107 sb.append("</level>\n"); 108 109 if (record.getSourceClassName() != null) { 110 sb.append(" <class>"); 111 escape(sb, record.getSourceClassName()); 112 sb.append("</class>\n"); 113 } 114 115 if (record.getSourceMethodName() != null) { 116 sb.append(" <method>"); 117 escape(sb, record.getSourceMethodName()); 118 sb.append("</method>\n"); 119 } 120 121 sb.append(" <thread>"); 122 sb.append(record.getThreadID()); 123 sb.append("</thread>\n"); 124 125 if (record.getMessage() != null) { 126 String message = formatMessage(record); 128 sb.append(" <message>"); 129 escape(sb, message); 130 sb.append("</message>"); 131 sb.append("\n"); 132 } 133 134 ResourceBundle bundle = record.getResourceBundle(); 137 try { 138 if (bundle != null && bundle.getString(record.getMessage()) != null) { 139 sb.append(" <key>"); 140 escape(sb, record.getMessage()); 141 sb.append("</key>\n"); 142 sb.append(" <catalog>"); 143 escape(sb, record.getResourceBundleName()); 144 sb.append("</catalog>\n"); 145 } 146 } catch (Exception ex) { 147 } 149 150 Object parameters[] = record.getParameters(); 151 if ( parameters != null && parameters.length != 0 154 && record.getMessage().indexOf("{") == -1 ) { 155 for (int i = 0; i < parameters.length; i++) { 156 sb.append(" <param>"); 157 try { 158 escape(sb, parameters[i].toString()); 159 } catch (Exception ex) { 160 sb.append("???"); 161 } 162 sb.append("</param>\n"); 163 } 164 } 165 166 if (record.getThrown() != null) { 167 Throwable th = record.getThrown(); 169 sb.append(" <exception>\n"); 170 sb.append(" <message>"); 171 escape(sb, th.toString()); 172 sb.append("</message>\n"); 173 StackTraceElement trace[] = th.getStackTrace(); 174 for (int i = 0; i < trace.length; i++) { 175 StackTraceElement frame = trace[i]; 176 sb.append(" <frame>\n"); 177 sb.append(" <class>"); 178 escape(sb, frame.getClassName()); 179 sb.append("</class>\n"); 180 sb.append(" <method>"); 181 escape(sb, frame.getMethodName()); 182 sb.append("</method>\n"); 183 if (frame.getLineNumber() >= 0) { 185 sb.append(" <line>"); 186 sb.append(frame.getLineNumber()); 187 sb.append("</line>\n"); 188 } 189 sb.append(" </frame>\n"); 190 } 191 sb.append(" </exception>\n"); 192 } 193 194 sb.append("</record>\n"); 195 return sb.toString(); 196 } 197 198 204 public String getHead(Handler h) { 205 StringBuffer sb = new StringBuffer (); 206 String encoding; 207 sb.append("<?xml version=\"1.0\""); 208 209 if (h != null) { 210 encoding = h.getEncoding(); 211 } else { 212 encoding = null; 213 } 214 215 if (encoding == null) { 216 encoding = sun.io.Converters.getDefaultEncodingName(); 218 } 219 try { 221 Charset cs = Charset.forName(encoding); 222 encoding = cs.name(); 223 } catch (Exception ex) { 224 } 227 228 sb.append(" encoding=\""); 229 sb.append(encoding); 230 sb.append("\""); 231 sb.append(" standalone=\"no\"?>\n"); 232 sb.append("<!DOCTYPE log SYSTEM \"logger.dtd\">\n"); 233 sb.append("<log>\n"); 234 return sb.toString(); 235 } 236 237 243 public String getTail(Handler h) { 244 return "</log>\n"; 245 } 246 } 247 | Popular Tags |