1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: PatternFormatter.java 2090 2011-03-07 04:13:05Z dmsmith $
21   */
22  package org.crosswire.common.util;
23  
24  import java.io.PrintWriter;
25  import java.io.StringWriter;
26  import java.text.MessageFormat;
27  import java.util.Date;
28  import java.util.logging.Formatter;
29  import java.util.logging.LogManager;
30  import java.util.logging.LogRecord;
31  import java.util.logging.Logger;
32  
33  /**
34   * Formats a log entry by pattern.
35   * <p>
36   * <ul>
37   * <li>{0} is the Date</li>
38   * <li>{1} is the name of the logger</li>
39   * <li>{2} is the level of the record</li>
40   * <li>{3} is the message</li>
41   * <li>{4} is the throwable</li>
42   * <li>{5} is the class name (typically the same as the logger's name)</li>
43   * <li>{6} is the method name</li>
44   * <li>{7} is the line number</li>
45   * <li>{8} is the system supplied new line</li>
46   * </ul>
47   * 
48   * @see gnu.lgpl.License for license details.<br>
49   *      The copyright to this program is held by it's authors.
50   * @author DM Smith [dmsmith555 at yahoo dot com]
51   * @author Joe Walker [joe at eireneh dot com]
52   */
53  public class PatternFormatter extends Formatter {
54      /**
55       * Format the given LogRecord.
56       * 
57       * @param record
58       *            the log record to be formatted.
59       * @return a formatted log record
60       */
61      @Override
62      public synchronized String format(LogRecord record) {
63          // Minimize memory allocations here.
64          dat.setTime(record.getMillis());
65          String throwable = "";
66          if (record.getThrown() != null) {
67              StringWriter sw = new StringWriter();
68              PrintWriter pw = new PrintWriter(sw);
69              record.getThrown().printStackTrace(pw);
70              pw.close();
71              throwable = sw.toString();
72          }
73  
74          String format = LogManager.getLogManager().getProperty(PatternFormatter.class.getName() + ".format");
75          String loggerName = record.getLoggerName();
76          Logger logger = LogManager.getLogManager().getLogger(loggerName);
77  
78          for (Logger aLogger = logger; aLogger != null; aLogger = aLogger.getParent()) {
79              String property = null;
80              String aLoggerName = aLogger.getName();
81  
82              if (aLoggerName != null) {
83                  property = LogManager.getLogManager().getProperty(aLoggerName + ".format");
84              }
85  
86              if (property != null) {
87                  format = property;
88                  break;
89              }
90          }
91  
92          if (format == null) {
93              format = DEFAULT_FORMAT;
94          }
95  
96          return MessageFormat.format(format,
97                  dat, // 0
98                  record.getLoggerName(), // 1
99                  record.getLevel().getLocalizedName(), // 2
100                 formatMessage(record), // 3
101                 throwable, // 4
102                 record.getSourceClassName(), // 5
103                 record.getSourceMethodName(), // 6
104                 Long.valueOf(record.getSequenceNumber()), // 7
105                 lineSeparator // 8
106         );
107     }
108 
109     private Date dat = new Date();
110     private static final String DEFAULT_FORMAT = "{1}({2}): {3}{8} {4}";
111 
112     // Line separator string. This is the value of the line.separator
113     // property at the moment that the PatternFormatter was created.
114     private String lineSeparator = System.getProperty("line.separator", "\r\n");
115 }
116