View Javadoc

1   package org.lsst.ccs.utilities.logging;
2   
3   import java.util.Date;
4   import java.util.logging.LogRecord;
5   import java.util.logging.SimpleFormatter;
6   
7   /**
8    * A simple text formatter. The differences with the standard <TT>SimpleFormatter</TT>:
9    * <UL>
10   *     <LI/> shows the sequence number of the LogRecord
11   *     <LI/> uses a different format String that the SimpleFormatter
12   *     <LI/> uses a StackTraceFormat object to show StackTraces
13   * </UL>
14   * @ImplNote
15   * Side effect: read and modifies the default StackTraceFormat depth
16   * (TODO: to be changed!)
17   * @author bamade
18   */
19  // Date: 15/04/2014
20  
21  public class TextFormatter extends SimpleFormatter{
22      String format ;
23      private final Date dat = new Date();
24  
25      public TextFormatter() {
26              format = LogPropertiesLoader.loaderGetStringProperty("org.lsst.ccs.utilities.logging.TextFormatter.format", "%2$s:%5$s%n%6$s") ;
27              StackTraceFormats.setDepthFromInitialProperties();
28      }
29  
30      public String getFormat() {
31          return format;
32      }
33  
34      public void setFormat(String format) {
35          this.format = format;
36      }
37  
38      public synchronized String format(LogRecord record) {
39          //System.out.println(" depth" + StackTraceFormats.getDepth());
40          String indexStr = String.valueOf(record.getSequenceNumber());
41  
42          dat.setTime(record.getMillis());
43          String source;
44          if (record.getSourceClassName() != null) {
45              source = record.getSourceClassName();
46              if (record.getSourceMethodName() != null) {
47                  source += " " + record.getSourceMethodName();
48              }
49          } else {
50              source = record.getLoggerName();
51          }
52          String message = formatMessage(record);
53          String throwableStr = "";
54          Throwable throwable = record.getThrown() ;
55          if (throwable != null) {
56              throwableStr = StackTraceFormats.toString(throwable) ;
57          }
58          String res = indexStr + " " +String.format(format,
59                  dat,
60                  source,
61                  record.getLoggerName(),
62                  record.getLevel(),
63                  message,
64                  throwableStr);
65          return res;
66      }
67  
68  }
69