/* * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.util.logging; import java.io.*; import java.text.*; import java.util.Date; import sun.util.logging.LoggingSupport; /** * Print a brief summary of the {@code LogRecord} in a human readable * format. The summary will typically be 1 or 2 lines. * *

* * Configuration: * The {@code SimpleFormatter} is initialized with the * format string * specified in the {@code java.util.logging.SimpleFormatter.format} * property to {@linkplain #format format} the log messages. * This property can be defined * in the {@linkplain LogManager#getProperty logging properties} * configuration file * or as a system property. If this property is set in both * the logging properties and system properties, * the format string specified in the system property will be used. * If this property is not defined or the given format string * is {@linkplain java.util.IllegalFormatException illegal}, * the default format is implementation-specific. * * @since 1.4 * @see java.util.Formatter */ public class SimpleFormatter extends Formatter { // format string for printing the log record private static final String format = LoggingSupport.getSimpleFormat(); private final Date dat = new Date(); /** * Format the given LogRecord. *

* The formatting can be customized by specifying the * format string * in the * {@code java.util.logging.SimpleFormatter.format} property. * The given {@code LogRecord} will be formatted as if by calling: *

     *    {@link String#format String.format}(format, date, source, logger, level, message, thrown);
     * 
* where the arguments are:
*
    *
  1. {@code format} - the {@link java.util.Formatter * java.util.Formatter} format string specified in the * {@code java.util.logging.SimpleFormatter.format} property * or the default format.
  2. *
  3. {@code date} - a {@link Date} object representing * {@linkplain LogRecord#getMillis event time} of the log record.
  4. *
  5. {@code source} - a string representing the caller, if available; * otherwise, the logger's name.
  6. *
  7. {@code logger} - the logger's name.
  8. *
  9. {@code level} - the {@linkplain Level#getLocalizedName * log level}.
  10. *
  11. {@code message} - the formatted log message * returned from the {@link Formatter#formatMessage(LogRecord)} * method. It uses {@link java.text.MessageFormat java.text} * formatting and does not use the {@code java.util.Formatter * format} argument.
  12. *
  13. {@code thrown} - a string representing * the {@linkplain LogRecord#getThrown throwable} * associated with the log record and its backtrace * beginning with a newline character, if any; * otherwise, an empty string.
  14. *
* *

Some example formats:
*

*

This method can also be overridden in a subclass. * It is recommended to use the {@link Formatter#formatMessage} * convenience method to localize and format the message field. * * @param record the log record to be formatted. * @return a formatted log record */ public synchronized String format(LogRecord record) { dat.setTime(record.getMillis()); String source; if (record.getSourceClassName() != null) { source = record.getSourceClassName(); if (record.getSourceMethodName() != null) { source += " " + record.getSourceMethodName(); } } else { source = record.getLoggerName(); } String message = formatMessage(record); String throwable = ""; if (record.getThrown() != null) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); pw.println(); record.getThrown().printStackTrace(pw); pw.close(); throwable = sw.toString(); } return String.format(format, dat, source, record.getLoggerName(), record.getLevel().getLocalizedLevelName(), message, throwable); } }