jdk/src/share/classes/java/util/logging/Formatter.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3853 9d2382b74894
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * A Formatter provides support for formatting LogRecords.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Typically each logging Handler will have a Formatter associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * with it.  The Formatter takes a LogRecord and converts it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Some formatters (such as the XMLFormatter) need to wrap head
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and tail strings around a set of formatted records. The getHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and getTail methods can be used to obtain these strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public abstract class Formatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Construct a new formatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected Formatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Format the given log record and return the formatted string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The resulting formatted String will normally include a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * localized and formated version of the LogRecord's message field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * It is recommended to use the {@link Formatter#formatMessage}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * convenience method to localize and format the message field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param record the log record to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @return the formatted log record
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public abstract String format(LogRecord record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Return the header string for a set of formatted records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * This base class returns an empty string, but this may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * overriden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param   h  The target handler (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @return  header string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public String getHead(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Return the tail string for a set of formatted records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * This base class returns an empty string, but this may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * overriden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param   h  The target handler (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return  tail string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public String getTail(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Localize and format the message string from a log record.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * method is provided as a convenience for Formatter subclasses to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * use when they are performing formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The message string is first localized to a format string using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * the record's ResourceBundle.  (If there is no ResourceBundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * or if the message key is not found, then the key is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * format string.)  The format String uses java.text style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <li>If there are no parameters, no formatter is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <li>Otherwise, if the string contains "{0" then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *     java.text.MessageFormat  is used to format the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <li>Otherwise no formatting is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param  record  the log record containing the raw message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return   a localized and formatted message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public synchronized String formatMessage(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String format = record.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        java.util.ResourceBundle catalog = record.getResourceBundle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (catalog != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                format = catalog.getString(record.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } catch (java.util.MissingResourceException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // Drop through.  Use record message as format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                format = record.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // Do the formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Object parameters[] = record.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (parameters == null || parameters.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                // No parameters.  Just return format string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Is it a java.text style format?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // Ideally we could match with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // Pattern.compile("\\{\\d").matcher(format).find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // However the cost is 14% higher, so we cheaply check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // 1 of the first 4 parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return java.text.MessageFormat.format(format, parameters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // Formatting failed: use localized format string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}