jdk/src/java.logging/share/classes/java/util/logging/Formatter.java
author dfuchs
Thu, 06 Apr 2017 14:38:15 +0100
changeset 44538 a603c475d649
parent 38884 48a0335cdadc
permissions -rw-r--r--
8178139: Minor typo in API documentation of java.util.logging.Logger Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    23
 * questions.
2
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
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
    55
     * localized and formatted version of the LogRecord's message field.
2
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
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
    69
     * overridden by subclasses.
2
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
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
    82
     * overridden by subclasses.
2
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.
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   104
     * <li>Otherwise, if the string contains "{{@literal<digit>}"
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   105
     *     where {@literal <digit>} is in [0-9],
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   106
     *     java.text.MessageFormat is used to format the string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <li>Otherwise no formatting is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * </ul>
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
     */
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   113
    public String formatMessage(LogRecord record) {
2
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 {
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   118
                format = catalog.getString(format);
2
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Do the formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            Object parameters[] = record.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (parameters == null || parameters.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                // No parameters.  Just return format string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // Is it a java.text style format?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Ideally we could match with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // Pattern.compile("\\{\\d").matcher(format).find())
38884
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   133
            // However the cost is 14% higher, so we cheaply use indexOf
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   134
            // and charAt to look for that pattern.
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   135
            int index = -1;
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   136
            int fence = format.length() - 1;
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   137
            while ((index = format.indexOf('{', index+1)) > -1) {
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   138
                if (index >= fence) break;
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   139
                char digit = format.charAt(index+1);
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   140
                if (digit >= '0' & digit <= '9') {
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   141
                   return java.text.MessageFormat.format(format, parameters);
48a0335cdadc 8153666: Optimize Formatter.formatMessage
dfuchs
parents: 25859
diff changeset
   142
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
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
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // Formatting failed: use localized format string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}