1 19 27 28 package org.netbeans.modules.exceptions; 29 30 import java.text.MessageFormat ; 31 import java.util.Date ; 32 import java.util.logging.Formatter ; 33 import java.util.logging.LogRecord ; 34 35 39 public class HTMLFormatter extends Formatter { 40 41 private final static String format = "{0,date} {0,time}"; 42 43 private String lineSeparator = "<br/>\n"; 44 45 public String format(LogRecord record) { 46 StringBuffer sb = new StringBuffer (); 47 Date date = new Date (); 48 date.setTime(record.getMillis()); 49 MessageFormat.format(format, date); 50 sb.append("<h5>"+record.getLoggerName()+"</h5>"); 51 sb.append("<i>"+record.getLevel().getLocalizedName()+"</i>"); 61 sb.append(": "); 62 sb.append(formatMessage(record)); 63 sb.append(lineSeparator); 64 if (record.getThrown() != null) print(record.getThrown(), sb); 65 return sb.toString(); 66 } 67 68 private void print(Throwable t, StringBuffer sb){ 69 sb.append(t.getMessage()+lineSeparator); 70 sb.append("<blockquote>\n"); 71 StackTraceElement [] trace = t.getStackTrace(); 72 for (int i = 0; i < trace.length; i++) sb.append(trace[i]+lineSeparator); 73 sb.append("</blockquote>\n"); 74 if (t.getCause()!= null){ 75 printCause(t.getCause(), sb, trace); 76 } 77 } 78 79 private void printCause(Throwable t, StringBuffer sb, StackTraceElement [] causedTrace){ 80 StackTraceElement [] trace = t.getStackTrace(); 81 int m = trace.length-1; 82 int n = causedTrace.length-1; 83 while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) { 84 m--; n--; 85 } 86 sb.append("Cause by: " + t.getMessage() + lineSeparator); 87 int framesInCommon = trace.length - 1 - m; 88 sb.append("<blockquote>\n"); 89 for (int i=0; i <= m; i++) sb.append(trace[i]+lineSeparator); 90 if (framesInCommon != 0) sb.append(framesInCommon + " more"); 91 sb.append("</blockquote>\n"); 92 if (t.getCause() != null) printCause(t.getCause(), sb, trace); 93 } 94 } 95 | Popular Tags |