< prev index next >

src/java.logging/share/classes/java/util/logging/SimpleFormatter.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package java.util.logging;
  28 
  29 import java.io.*;
  30 import java.time.ZoneId;
  31 import java.time.ZonedDateTime;
  32 import jdk.internal.logger.SimpleConsoleLogger;
  33 
  34 /**
  35  * Print a brief summary of the {@code LogRecord} in a human readable
  36  * format.  The summary will typically be 1 or 2 lines.
  37  *
  38  * <p>
  39  * <a name="formatting">
  40  * <b>Configuration:</b></a>
  41  * The {@code SimpleFormatter} is initialized with the
  42  * <a href="../Formatter.html#syntax">format string</a>
  43  * specified in the {@code java.util.logging.SimpleFormatter.format}
  44  * property to {@linkplain #format format} the log messages.
  45  * This property can be defined
  46  * in the {@linkplain LogManager#getProperty logging properties}
  47  * configuration file
  48  * or as a system property.  If this property is set in both
  49  * the logging properties and system properties,
  50  * the format string specified in the system property will be used.
  51  * If this property is not defined or the given format string
  52  * is {@linkplain java.util.IllegalFormatException illegal},
  53  * the default format is implementation-specific.
  54  *
  55  * @since 1.4
  56  * @see java.util.Formatter
  57  */
  58 
  59 public class SimpleFormatter extends Formatter {
  60 
  61     // format string for printing the log record
  62     static String getLoggingProperty(String name) {
  63         return LogManager.getLogManager().getProperty(name);
  64     }
  65 
  66     private final String format =
  67         SimpleConsoleLogger.getSimpleFormat(SimpleFormatter::getLoggingProperty);
  68 
  69     /**
  70      * Format the given LogRecord.
  71      * <p>
  72      * The formatting can be customized by specifying the
  73      * <a href="../Formatter.html#syntax">format string</a>
  74      * in the <a href="#formatting">
  75      * {@code java.util.logging.SimpleFormatter.format}</a> property.
  76      * The given {@code LogRecord} will be formatted as if by calling:
  77      * <pre>
  78      *    {@link String#format String.format}(format, date, source, logger, level, message, thrown);
  79      * </pre>
  80      * where the arguments are:<br>
  81      * <ol>
  82      * <li>{@code format} - the {@link java.util.Formatter
  83      *     java.util.Formatter} format string specified in the
  84      *     {@code java.util.logging.SimpleFormatter.format} property
  85      *     or the default format.</li>
  86      * <li>{@code date} - a {@link ZonedDateTime} object representing
  87      *     {@linkplain LogRecord#getInstant() event time} of the log record


   1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package java.util.logging;
  28 
  29 import java.io.*;
  30 import java.time.ZoneId;
  31 import java.time.ZonedDateTime;
  32 import jdk.internal.logger.SurrogateLogger;
  33 
  34 /**
  35  * Print a brief summary of the {@code LogRecord} in a human readable
  36  * format.  The summary will typically be 1 or 2 lines.
  37  *
  38  * <p>
  39  * <a name="formatting">
  40  * <b>Configuration:</b></a>
  41  * The {@code SimpleFormatter} is initialized with the
  42  * <a href="../Formatter.html#syntax">format string</a>
  43  * specified in the {@code java.util.logging.SimpleFormatter.format}
  44  * property to {@linkplain #format format} the log messages.
  45  * This property can be defined
  46  * in the {@linkplain LogManager#getProperty logging properties}
  47  * configuration file
  48  * or as a system property.  If this property is set in both
  49  * the logging properties and system properties,
  50  * the format string specified in the system property will be used.
  51  * If this property is not defined or the given format string
  52  * is {@linkplain java.util.IllegalFormatException illegal},
  53  * the default format is implementation-specific.
  54  *
  55  * @since 1.4
  56  * @see java.util.Formatter
  57  */
  58 
  59 public class SimpleFormatter extends Formatter {
  60 
  61     // format string for printing the log record
  62     static String getLoggingProperty(String name) {
  63         return LogManager.getLogManager().getProperty(name);
  64     }
  65 
  66     private final String format =
  67         SurrogateLogger.getSimpleFormat(SimpleFormatter::getLoggingProperty);
  68 
  69     /**
  70      * Format the given LogRecord.
  71      * <p>
  72      * The formatting can be customized by specifying the
  73      * <a href="../Formatter.html#syntax">format string</a>
  74      * in the <a href="#formatting">
  75      * {@code java.util.logging.SimpleFormatter.format}</a> property.
  76      * The given {@code LogRecord} will be formatted as if by calling:
  77      * <pre>
  78      *    {@link String#format String.format}(format, date, source, logger, level, message, thrown);
  79      * </pre>
  80      * where the arguments are:<br>
  81      * <ol>
  82      * <li>{@code format} - the {@link java.util.Formatter
  83      *     java.util.Formatter} format string specified in the
  84      *     {@code java.util.logging.SimpleFormatter.format} property
  85      *     or the default format.</li>
  86      * <li>{@code date} - a {@link ZonedDateTime} object representing
  87      *     {@linkplain LogRecord#getInstant() event time} of the log record


< prev index next >