Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

TerseLoggingFormatter.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc;
00002 
00003 import java.io.*;
00004 import java.nio.charset.Charset;
00005 import java.util.*;
00006 import java.util.logging.*;
00007 import java.text.SimpleDateFormat;
00008 
00019 public class TerseLoggingFormatter extends Formatter {
00020 
00024         private LogManager manager = LogManager.getLogManager();
00025 
00029         final private static SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
00030         // dateFmt.format(d)
00031 
00032         final private static Date date = new Date();
00033 
00039   public String format(LogRecord record) {
00040                 StringBuffer sb = new StringBuffer(500);
00041                 date.setTime(record.getMillis());
00042 
00043                 String sourceClassName = record.getSourceClassName();
00044                 if (sourceClassName.lastIndexOf(".") != -1) {
00045                         // Remove the package from the source class name for briefity.
00046                         sourceClassName = sourceClassName.substring((sourceClassName.lastIndexOf(".")+1));
00047                 }
00048 
00049     sb.append(record.getThreadID());
00050                 sb.append(" ");
00051                 sb.append(dateFmt.format(date));
00052                 sb.append(" ");
00053                 sb.append(sourceClassName);
00054                 sb.append(".");
00055                 sb.append(record.getSourceMethodName());
00056                 sb.append("() ");
00057                 sb.append(record.getMessage());
00058 
00059                 Object[] parameters = record.getParameters();
00060                 if (parameters != null && parameters.length > 0) {
00061 
00062                         for (int x = 0; x < parameters.length; x++) {
00063                                 if (parameters[x] != null) {
00064                                         sb.append("\n  param("+x+") "+parameters[x].getClass().getName()+" "+parameters[x]);
00065                                 }
00066                                 else {
00067                                         sb.append("\n  param("+x+") null");
00068                                 }
00069                         }
00070                 }
00071 
00072                 // terminate the line.
00073                 sb.append("\n");
00074 
00075     if (record.getThrown() != null) {
00076                         Throwable th = record.getThrown();
00077                         while (th != null) {
00078                                 sb.append(th.toString());
00079                                 sb.append("\n");
00080                                 StackTraceElement trace[] = th.getStackTrace();
00081                                 for (int i = 0; i < trace.length; i++) {
00082                                         StackTraceElement frame = trace[i];
00083                                         sb.append("  ");
00084                                         sb.append(frame.getClassName());
00085                                         sb.append(".");
00086                                         sb.append(frame.getMethodName());
00087                                         sb.append("(");
00088                                         sb.append(frame.getLineNumber());
00089                                         sb.append(")\n");
00090                                 }
00091                                 th = th.getCause();
00092                         }
00093                 }
00094 
00095                 // done; return the formatted string.
00096     return sb.toString();
00097   }
00098 
00105   public String getHead(Handler h) {
00106                 return "";
00107   }
00108 
00112   public String getTail(Handler h) {
00113     return "";
00114   }
00115 }

Generated on Mon Jul 14 17:19:20 2003 for SOSC by doxygen1.2.15