jdk/src/share/classes/java/util/logging/SimpleFormatter.java
author mchung
Mon, 26 Nov 2012 22:49:06 -0800
changeset 16098 9001e536ab4e
parent 9014 117374ccf1b7
child 16100 379f48d34516
permissions -rw-r--r--
6664509: Add logging context 6664528: Find log level matching its name or value given at construction time Reviewed-by: alanb, ahgross, jgish, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2006, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Date;
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    32
import sun.util.logging.LoggingSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    35
 * Print a brief summary of the {@code LogRecord} in a human readable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * format.  The summary will typically be 1 or 2 lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    38
 * <p>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    39
 * <a name="formatting">
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    40
 * <b>Configuration:</b></a>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    41
 * The {@code SimpleFormatter} is initialized with the
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    42
 * <a href="../Formatter.html#syntax">format string</a>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    43
 * specified in the {@code java.util.logging.SimpleFormatter.format}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    44
 * property to {@linkplain #format format} the log messages.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    45
 * This property can be defined
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    46
 * in the {@linkplain LogManager#getProperty logging properties}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    47
 * configuration file
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    48
 * or as a system property.  If this property is set in both
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    49
 * the logging properties and system properties,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    50
 * the format string specified in the system property will be used.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    51
 * If this property is not defined or the given format string
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    52
 * is {@linkplain java.util.IllegalFormatException illegal},
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    53
 * the default format is implementation-specific.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    54
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.4
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    56
 * @see java.util.Formatter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class SimpleFormatter extends Formatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    61
    // format string for printing the log record
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    62
    private static final String format = LoggingSupport.getSimpleFormat();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    63
    private final Date dat = new Date();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Format the given LogRecord.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <p>
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    68
     * The formatting can be customized by specifying the
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    69
     * <a href="../Formatter.html#syntax">format string</a>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    70
     * in the <a href="#formatting">
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    71
     * {@code java.util.logging.SimpleFormatter.format}</a> property.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    72
     * The given {@code LogRecord} will be formatted as if by calling:
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    73
     * <pre>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    74
     *    {@link String#format String.format}(format, date, source, logger, level, message, thrown);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    75
     * </pre>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    76
     * where the arguments are:<br>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    77
     * <ol>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    78
     * <li>{@code format} - the {@link java.util.Formatter
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    79
     *     java.util.Formatter} format string specified in the
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    80
     *     {@code java.util.logging.SimpleFormatter.format} property
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    81
     *     or the default format.</li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    82
     * <li>{@code date} - a {@link Date} object representing
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    83
     *     {@linkplain LogRecord#getMillis event time} of the log record.</li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    84
     * <li>{@code source} - a string representing the caller, if available;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    85
     *     otherwise, the logger's name.</li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    86
     * <li>{@code logger} - the logger's name.</li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    87
     * <li>{@code level} - the {@linkplain Level#getLocalizedName
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    88
     *     log level}.</li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    89
     * <li>{@code message} - the formatted log message
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    90
     *     returned from the {@link Formatter#formatMessage(LogRecord)}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    91
     *     method.  It uses {@link java.text.MessageFormat java.text}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    92
     *     formatting and does not use the {@code java.util.Formatter
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    93
     *     format} argument.</li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    94
     * <li>{@code thrown} - a string representing
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    95
     *     the {@linkplain LogRecord#getThrown throwable}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    96
     *     associated with the log record and its backtrace
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    97
     *     beginning with a newline character, if any;
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    98
     *     otherwise, an empty string.</li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
    99
     * </ol>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   100
     *
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   101
     * <p>Some example formats:<br>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   102
     * <ul>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   103
     * <li> {@code java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n"}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   104
     *     <p>This prints 1 line with the log level ({@code 4$}),
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   105
     *     the log message ({@code 5$}) and the timestamp ({@code 1$}) in
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   106
     *     a square bracket.
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   107
     *     <pre>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   108
     *     WARNING: warning message [Tue Mar 22 13:11:31 PDT 2011]
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   109
     *     </pre></li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   110
     * <li> {@code java.util.logging.SimpleFormatter.format="%1$tc %2$s%n%4$s: %5$s%6$s%n"}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   111
     *     <p>This prints 2 lines where the first line includes
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   112
     *     the timestamp ({@code 1$}) and the source ({@code 2$});
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   113
     *     the second line includes the log level ({@code 4$}) and
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   114
     *     the log message ({@code 5$}) followed with the throwable
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   115
     *     and its backtrace ({@code 6$}), if any:
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   116
     *     <pre>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   117
     *     Tue Mar 22 13:11:31 PDT 2011 MyClass fatal
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   118
     *     SEVERE: several message with an exception
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   119
     *     java.lang.IllegalArgumentException: invalid argument
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   120
     *             at MyClass.mash(MyClass.java:9)
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   121
     *             at MyClass.crunch(MyClass.java:6)
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   122
     *             at MyClass.main(MyClass.java:3)
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   123
     *     </pre></li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   124
     * <li> {@code java.util.logging.SimpleFormatter.format="%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%n"}
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   125
     *      <p>This prints 2 lines similar to the example above
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   126
     *         with a different date/time formatting and does not print
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   127
     *         the throwable and its backtrace:
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   128
     *     <pre>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   129
     *     Mar 22, 2011 1:11:31 PM MyClass fatal
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   130
     *     SEVERE: several message with an exception
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   131
     *     </pre></li>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   132
     * </ul>
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   133
     * <p>This method can also be overridden in a subclass.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * It is recommended to use the {@link Formatter#formatMessage}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * convenience method to localize and format the message field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param record the log record to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return a formatted log record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public synchronized String format(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        dat.setTime(record.getMillis());
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   142
        String source;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (record.getSourceClassName() != null) {
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   144
            source = record.getSourceClassName();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   145
            if (record.getSourceMethodName() != null) {
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   146
               source += " " + record.getSourceMethodName();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   147
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else {
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   149
            source = record.getLoggerName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        String message = formatMessage(record);
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   152
        String throwable = "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (record.getThrown() != null) {
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   154
            StringWriter sw = new StringWriter();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   155
            PrintWriter pw = new PrintWriter(sw);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   156
            pw.println();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   157
            record.getThrown().printStackTrace(pw);
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   158
            pw.close();
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   159
            throwable = sw.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   161
        return String.format(format,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   162
                             dat,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   163
                             source,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   164
                             record.getLoggerName(),
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9014
diff changeset
   165
                             record.getLevel().getLocalizedLevelName(),
9014
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   166
                             message,
117374ccf1b7 6381464: SimpleFormatter should use one single line format
mchung
parents: 5506
diff changeset
   167
                             throwable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}