1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   
9   package java.util.logging;
10  
11  import java.io.*;
12  import java.text.*;
13  import java.util.Date;
14  
15  /**
16   * Print a brief summary of the LogRecord in a human readable
17   * format.  The summary will typically be 1 or 2 lines.
18   *
19   * @version %I%, %G%
20   * @since 1.4
21   */
22  
23  public class SimpleFormatter extends Formatter {
24  
25      Date dat = new Date();
26      private final static String format = "{0,date} {0,time}";
27      private MessageFormat formatter;
28  
29      private Object args[] = new Object[1];
30  
31      // Line separator string.  This is the value of the line.separator
32      // property at the moment that the SimpleFormatter was created.
33      private String lineSeparator = (String) java.security.AccessController.doPrivileged(
34                 new sun.security.action.GetPropertyAction("line.separator"));
35  
36      /**
37       * Format the given LogRecord.
38       * @param record the log record to be formatted.
39       * @return a formatted log record
40       */
41      public synchronized String format(LogRecord record) {
42      StringBuffer sb = new StringBuffer();
43      // Minimize memory allocations here.
44      dat.setTime(record.getMillis());
45      args[0] = dat;
46      StringBuffer text = new StringBuffer();
47      if (formatter == null) {
48          formatter = new MessageFormat(format);
49      }
50      formatter.format(args, text, null);
51      sb.append(text);
52      sb.append(" ");
53      if (record.getSourceClassName() != null) {  
54          sb.append(record.getSourceClassName());
55      } else {
56          sb.append(record.getLoggerName());
57      }
58      if (record.getSourceMethodName() != null) { 
59          sb.append(" ");
60          sb.append(record.getSourceMethodName());
61      }
62      sb.append(lineSeparator);
63      String message = formatMessage(record);
64      sb.append(record.getLevel().getLocalizedName());
65      sb.append(": ");
66      sb.append(message);
67      sb.append(lineSeparator);
68      if (record.getThrown() != null) {
69          try {
70              StringWriter sw = new StringWriter();
71              PrintWriter pw = new PrintWriter(sw);
72              record.getThrown().printStackTrace(pw);
73              pw.close();
74          sb.append(sw.toString());
75          } catch (Exception ex) {
76          }
77      }
78      return sb.toString();
79      }
80  }
81