1 25 26 package org.objectweb.easybeans.log; 27 28 import java.io.PrintWriter ; 29 import java.io.StringWriter ; 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 JDKFormatter extends Formatter { 40 41 44 private static final String PATTERN_DATE_TIME = "{0,date,short} {0,time,medium}"; 45 46 49 private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 50 51 54 private Date date = new Date (); 55 56 61 @Override 62 public synchronized String format(final LogRecord logRecord) { 63 64 StringBuilder fullMessage = new StringBuilder (); 65 date.setTime(logRecord.getMillis()); 67 fullMessage.append(MessageFormat.format(PATTERN_DATE_TIME, date)); 68 69 fullMessage.append(" ("); 71 fullMessage.append(logRecord.getLevel().getLocalizedName().substring(0, 1)); 72 fullMessage.append(") "); 73 74 if (logRecord.getSourceClassName() != null) { 75 String [] splitNames = logRecord.getSourceClassName().split("\\."); 77 fullMessage.append(splitNames[splitNames.length - 1]); 78 } else { 79 fullMessage.append(logRecord.getLoggerName()); 80 } 81 if (logRecord.getSourceMethodName() != null) { 82 fullMessage.append("."); 83 fullMessage.append(logRecord.getSourceMethodName()); 84 } 85 fullMessage.append(" : "); 86 87 String userMessage = formatMessage(logRecord); 89 fullMessage.append(userMessage); 90 91 fullMessage.append(LINE_SEPARATOR); 93 94 if (logRecord.getThrown() != null) { 96 try { 97 StringWriter stringWriter = new StringWriter (); 98 PrintWriter printWriter = new PrintWriter (stringWriter); 99 logRecord.getThrown().printStackTrace(printWriter); 100 printWriter.close(); 101 fullMessage.append(stringWriter.toString()); 102 } catch (Exception ex) { 103 System.err.println("Formatter error" + ex.getMessage()); 104 } 105 } 106 return fullMessage.toString(); 107 } 108 } 109 | Popular Tags |