Path: blob/master/src/java.logging/share/classes/java/util/logging/Formatter.java
41159 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package java.util.logging;2728/**29* A Formatter provides support for formatting LogRecords.30* <p>31* Typically each logging Handler will have a Formatter associated32* with it. The Formatter takes a LogRecord and converts it to33* a string.34* <p>35* Some formatters (such as the XMLFormatter) need to wrap head36* and tail strings around a set of formatted records. The getHeader37* and getTail methods can be used to obtain these strings.38*39* @since 1.440*/4142public abstract class Formatter {4344/**45* Construct a new formatter.46*/47protected Formatter() {48}4950/**51* Format the given log record and return the formatted string.52* <p>53* The resulting formatted String will normally include a54* localized and formatted version of the LogRecord's message field.55* It is recommended to use the {@link Formatter#formatMessage}56* convenience method to localize and format the message field.57*58* @param record the log record to be formatted.59* @return the formatted log record60*/61public abstract String format(LogRecord record);626364/**65* Return the header string for a set of formatted records.66* <p>67* This base class returns an empty string, but this may be68* overridden by subclasses.69*70* @param h The target handler (can be null)71* @return header string72*/73public String getHead(Handler h) {74return "";75}7677/**78* Return the tail string for a set of formatted records.79* <p>80* This base class returns an empty string, but this may be81* overridden by subclasses.82*83* @param h The target handler (can be null)84* @return tail string85*/86public String getTail(Handler h) {87return "";88}899091/**92* Localize and format the message string from a log record. This93* method is provided as a convenience for Formatter subclasses to94* use when they are performing formatting.95* <p>96* The message string is first localized to a format string using97* the record's ResourceBundle. (If there is no ResourceBundle,98* or if the message key is not found, then the key is used as the99* format string.) The format String uses java.text style100* formatting.101* <ul>102* <li>If there are no parameters, no formatter is used.103* <li>Otherwise, if the string contains "{{@literal<digit>}"104* where {@literal <digit>} is in [0-9],105* java.text.MessageFormat is used to format the string.106* <li>Otherwise no formatting is performed.107* </ul>108*109* @param record the log record containing the raw message110* @return a localized and formatted message111*/112public String formatMessage(LogRecord record) {113String format = record.getMessage();114java.util.ResourceBundle catalog = record.getResourceBundle();115if (catalog != null) {116try {117format = catalog.getString(format);118} catch (java.util.MissingResourceException ex) {119// Drop through. Use record message as format120}121}122// Do the formatting.123try {124Object parameters[] = record.getParameters();125if (parameters == null || parameters.length == 0) {126// No parameters. Just return format string.127return format;128}129// Is it a java.text style format?130// Ideally we could match with131// Pattern.compile("\\{\\d").matcher(format).find())132// However the cost is 14% higher, so we cheaply use indexOf133// and charAt to look for that pattern.134int index = -1;135int fence = format.length() - 1;136while ((index = format.indexOf('{', index+1)) > -1) {137if (index >= fence) break;138char digit = format.charAt(index+1);139if (digit >= '0' && digit <= '9') {140return java.text.MessageFormat.format(format, parameters);141}142}143return format;144145} catch (Exception ex) {146// Formatting failed: use localized format string.147return format;148}149}150}151152153