0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage java.util.logging;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.text.*;
0N/Aimport java.util.Date;
3888N/Aimport sun.util.logging.LoggingSupport;
0N/A
0N/A/**
3888N/A * Print a brief summary of the {@code LogRecord} in a human readable
0N/A * format. The summary will typically be 1 or 2 lines.
0N/A *
3888N/A * <p>
3888N/A * <a name="formatting">
3888N/A * <b>Configuration:</b></a>
3888N/A * The {@code SimpleFormatter} is initialized with the
3888N/A * <a href="../Formatter.html#syntax">format string</a>
3888N/A * specified in the {@code java.util.logging.SimpleFormatter.format}
3888N/A * property to {@linkplain #format format} the log messages.
3888N/A * This property can be defined
3888N/A * in the {@linkplain LogManager#getProperty logging properties}
3888N/A * configuration file
3888N/A * or as a system property. If this property is set in both
3888N/A * the logging properties and system properties,
3888N/A * the format string specified in the system property will be used.
3888N/A * If this property is not defined or the given format string
3888N/A * is {@linkplain java.util.IllegalFormatException illegal},
3888N/A * the default format is implementation-specific.
3888N/A *
0N/A * @since 1.4
3888N/A * @see java.util.Formatter
0N/A */
0N/A
0N/Apublic class SimpleFormatter extends Formatter {
0N/A
3888N/A // format string for printing the log record
3888N/A private static final String format = LoggingSupport.getSimpleFormat();
3888N/A private final Date dat = new Date();
0N/A
0N/A /**
0N/A * Format the given LogRecord.
0N/A * <p>
3888N/A * The formatting can be customized by specifying the
3888N/A * <a href="../Formatter.html#syntax">format string</a>
3888N/A * in the <a href="#formatting">
3888N/A * {@code java.util.logging.SimpleFormatter.format}</a> property.
3888N/A * The given {@code LogRecord} will be formatted as if by calling:
3888N/A * <pre>
3888N/A * {@link String#format String.format}(format, date, source, logger, level, message, thrown);
3888N/A * </pre>
3888N/A * where the arguments are:<br>
3888N/A * <ol>
3888N/A * <li>{@code format} - the {@link java.util.Formatter
3888N/A * java.util.Formatter} format string specified in the
3888N/A * {@code java.util.logging.SimpleFormatter.format} property
3888N/A * or the default format.</li>
3888N/A * <li>{@code date} - a {@link Date} object representing
3888N/A * {@linkplain LogRecord#getMillis event time} of the log record.</li>
3888N/A * <li>{@code source} - a string representing the caller, if available;
3888N/A * otherwise, the logger's name.</li>
3888N/A * <li>{@code logger} - the logger's name.</li>
3888N/A * <li>{@code level} - the {@linkplain Level#getLocalizedName
3888N/A * log level}.</li>
3888N/A * <li>{@code message} - the formatted log message
3888N/A * returned from the {@link Formatter#formatMessage(LogRecord)}
3888N/A * method. It uses {@link java.text.MessageFormat java.text}
3888N/A * formatting and does not use the {@code java.util.Formatter
3888N/A * format} argument.</li>
3888N/A * <li>{@code thrown} - a string representing
3888N/A * the {@linkplain LogRecord#getThrown throwable}
3888N/A * associated with the log record and its backtrace
3888N/A * beginning with a newline character, if any;
3888N/A * otherwise, an empty string.</li>
3888N/A * </ol>
3888N/A *
3888N/A * <p>Some example formats:<br>
3888N/A * <ul>
3888N/A * <li> {@code java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n"}
3888N/A * <p>This prints 1 line with the log level ({@code 4$}),
3888N/A * the log message ({@code 5$}) and the timestamp ({@code 1$}) in
3888N/A * a square bracket.
3888N/A * <pre>
3888N/A * WARNING: warning message [Tue Mar 22 13:11:31 PDT 2011]
3888N/A * </pre></li>
3888N/A * <li> {@code java.util.logging.SimpleFormatter.format="%1$tc %2$s%n%4$s: %5$s%6$s%n"}
3888N/A * <p>This prints 2 lines where the first line includes
3888N/A * the timestamp ({@code 1$}) and the source ({@code 2$});
3888N/A * the second line includes the log level ({@code 4$}) and
3888N/A * the log message ({@code 5$}) followed with the throwable
3888N/A * and its backtrace ({@code 6$}), if any:
3888N/A * <pre>
3888N/A * Tue Mar 22 13:11:31 PDT 2011 MyClass fatal
3888N/A * SEVERE: several message with an exception
3888N/A * java.lang.IllegalArgumentException: invalid argument
3888N/A * at MyClass.mash(MyClass.java:9)
3888N/A * at MyClass.crunch(MyClass.java:6)
3888N/A * at MyClass.main(MyClass.java:3)
3888N/A * </pre></li>
3888N/A * <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"}
3888N/A * <p>This prints 2 lines similar to the example above
3888N/A * with a different date/time formatting and does not print
3888N/A * the throwable and its backtrace:
3888N/A * <pre>
3888N/A * Mar 22, 2011 1:11:31 PM MyClass fatal
3888N/A * SEVERE: several message with an exception
3888N/A * </pre></li>
3888N/A * </ul>
3888N/A * <p>This method can also be overridden in a subclass.
0N/A * It is recommended to use the {@link Formatter#formatMessage}
0N/A * convenience method to localize and format the message field.
0N/A *
0N/A * @param record the log record to be formatted.
0N/A * @return a formatted log record
0N/A */
0N/A public synchronized String format(LogRecord record) {
0N/A dat.setTime(record.getMillis());
3888N/A String source;
0N/A if (record.getSourceClassName() != null) {
3888N/A source = record.getSourceClassName();
3888N/A if (record.getSourceMethodName() != null) {
3888N/A source += " " + record.getSourceMethodName();
3888N/A }
0N/A } else {
3888N/A source = record.getLoggerName();
0N/A }
0N/A String message = formatMessage(record);
3888N/A String throwable = "";
0N/A if (record.getThrown() != null) {
3888N/A StringWriter sw = new StringWriter();
3888N/A PrintWriter pw = new PrintWriter(sw);
3888N/A pw.println();
3888N/A record.getThrown().printStackTrace(pw);
3888N/A pw.close();
3888N/A throwable = sw.toString();
0N/A }
3888N/A return String.format(format,
3888N/A dat,
3888N/A source,
3888N/A record.getLoggerName(),
5720N/A record.getLevel().getLocalizedLevelName(),
3888N/A message,
3888N/A throwable);
0N/A }
0N/A}