| Formatter.java |
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 /**
12 * A Formatter provides support for formatting LogRecords.
13 * <p>
14 * Typically each logging Handler will have a Formatter associated
15 * with it. The Formatter takes a LogRecord and converts it to
16 * a string.
17 * <p>
18 * Some formatters (such as the XMLFormatter) need to wrap head
19 * and tail strings around a set of formatted records. The getHeader
20 * and getTail methods can be used to obtain these strings.
21 *
22 * @version %I%, %G%
23 * @since 1.4
24 */
25
26 public abstract class Formatter {
27
28 /**
29 * Construct a new formatter.
30 */
31 protected Formatter() {
32 }
33
34 /**
35 * Format the given log record and return the formatted string.
36 * <p>
37 * The resulting formatted String will normally include a
38 * localized and formated version of the LogRecord's message field.
39 * The Formatter.formatMessage convenience method can (optionally)
40 * be used to localize and format the message field.
41 *
42 * @param record the log record to be formatted.
43 * @return the formatted log record
44 */
45 public abstract String format(LogRecord record);
46
47
48 /**
49 * Return the header string for a set of formatted records.
50 * <p>
51 * This base class returns an empty string, but this may be
52 * overriden by subclasses.
53 *
54 * @param h The target handler (can be null)
55 * @return header string
56 */
57 public String getHead(Handler h) {
58 return "";
59 }
60
61 /**
62 * Return the tail string for a set of formatted records.
63 * <p>
64 * This base class returns an empty string, but this may be
65 * overriden by subclasses.
66 *
67 * @param h The target handler (can be null)
68 * @return tail string
69 */
70 public String getTail(Handler h) {
71 return "";
72 }
73
74
75 /**
76 * Localize and format the message string from a log record. This
77 * method is provided as a convenience for Formatter subclasses to
78 * use when they are performing formatting.
79 * <p>
80 * The message string is first localized to a format string using
81 * the record's ResourceBundle. (If there is no ResourceBundle,
82 * or if the message key is not found, then the key is used as the
83 * format string.) The format String uses java.text style
84 * formatting.
85 * <ul>
86 * <li>If there are no parameters, no formatter is used.
87 * <li>Otherwise, if the string contains "{0" then
88 * java.text.MessageFormat is used to format the string.
89 * <li>Otherwise no formatting is performed.
90 * </ul>
91 * <p>
92 *
93 * @param record the log record containing the raw message
94 * @return a localized and formatted message
95 */
96 public synchronized String formatMessage(LogRecord record) {
97 String format = record.getMessage();
98 java.util.ResourceBundle catalog = record.getResourceBundle();
99 if (catalog != null) {
100 // // We cache catalog lookups. This is mostly to avoid the
101 // // cost of exceptions for keys that are not in the catalog.
102 // if (catalogCache == null) {
103 // catalogCache = new HashMap();
104 // }
105 // format = (String)catalogCache.get(record.essage);
106 // if (format == null) {
107 try {
108 format = catalog.getString(record.getMessage());
109 } catch (java.util.MissingResourceException ex) {
110 // Drop through. Use record message as format
111 format = record.getMessage();
112 }
113 // catalogCache.put(record.message, format);
114 // }
115 }
116 // Do the formatting.
117 try {
118 Object parameters[] = record.getParameters();
119 if (parameters == null || parameters.length == 0) {
120 // No parameters. Just return format string.
121 return format;
122 }
123 // Is is a java.text style format?
124 // Ideally we could match with
125 // Pattern.compile("\\{\\d").matcher(format).find())
126 // However the cost is 14% higher, so we cheaply check for
127 // 1 of the first 4 parameters
128 if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
129 format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
130 return java.text.MessageFormat.format(format, parameters);
131 }
132 return format;
133
134 } catch (Exception ex) {
135 // Formatting failed: use localized format string.
136 return format;
137 }
138 }
139 }
140
141
142
143