< prev index next >

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

Print this page




  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.LocalDateTime;
  31 import java.time.ZoneId;
  32 import java.time.ZonedDateTime;
  33 import sun.util.logging.LoggingSupport;
  34 
  35 /**
  36  * Print a brief summary of the {@code LogRecord} in a human readable
  37  * format.  The summary will typically be 1 or 2 lines.
  38  *
  39  * <p>
  40  * <a name="formatting">
  41  * <b>Configuration:</b></a>
  42  * The {@code SimpleFormatter} is initialized with the
  43  * <a href="../Formatter.html#syntax">format string</a>
  44  * specified in the {@code java.util.logging.SimpleFormatter.format}
  45  * property to {@linkplain #format format} the log messages.
  46  * This property can be defined
  47  * in the {@linkplain LogManager#getProperty logging properties}
  48  * configuration file
  49  * or as a system property.  If this property is set in both
  50  * the logging properties and system properties,
  51  * the format string specified in the system property will be used.
  52  * If this property is not defined or the given format string
  53  * is {@linkplain java.util.IllegalFormatException illegal},
  54  * the default format is implementation-specific.
  55  *
  56  * @since 1.4
  57  * @see java.util.Formatter
  58  */
  59 
  60 public class SimpleFormatter extends Formatter {
  61 
  62     // format string for printing the log record
  63     private final String format = LoggingSupport.getSimpleFormat();
  64     private final ZoneId zoneId = ZoneId.systemDefault();




  65 
  66     /**
  67      * Format the given LogRecord.
  68      * <p>
  69      * The formatting can be customized by specifying the
  70      * <a href="../Formatter.html#syntax">format string</a>
  71      * in the <a href="#formatting">
  72      * {@code java.util.logging.SimpleFormatter.format}</a> property.
  73      * The given {@code LogRecord} will be formatted as if by calling:
  74      * <pre>
  75      *    {@link String#format String.format}(format, date, source, logger, level, message, thrown);
  76      * </pre>
  77      * where the arguments are:<br>
  78      * <ol>
  79      * <li>{@code format} - the {@link java.util.Formatter
  80      *     java.util.Formatter} format string specified in the
  81      *     {@code java.util.logging.SimpleFormatter.format} property
  82      *     or the default format.</li>
  83      * <li>{@code date} - a {@link ZonedDateTime} object representing
  84      *     {@linkplain LogRecord#getInstant() event time} of the log record


 135      *      <p>Since JDK 1.9, {@code java.util.logging} uses {@link
 136      *         java.time.Clock#systemUTC() java.time} to create more precise time
 137      *         stamps.
 138      *         The format above can be used to add a {@code .%1$tN} to the
 139      *         date/time formatting so that nanoseconds will also be printed:
 140      *     <pre>
 141      *     Feb 06, 2015 5:33:10.279216000 PM example.Main main
 142      *     INFO: This is a test
 143      *     </pre></li>
 144      * </ul>
 145      * <p>This method can also be overridden in a subclass.
 146      * It is recommended to use the {@link Formatter#formatMessage}
 147      * convenience method to localize and format the message field.
 148      *
 149      * @param record the log record to be formatted.
 150      * @return a formatted log record
 151      */
 152     @Override
 153     public synchronized String format(LogRecord record) {
 154         ZonedDateTime zdt = ZonedDateTime.ofInstant(
 155                 record.getInstant(), zoneId);
 156         String source;
 157         if (record.getSourceClassName() != null) {
 158             source = record.getSourceClassName();
 159             if (record.getSourceMethodName() != null) {
 160                source += " " + record.getSourceMethodName();
 161             }
 162         } else {
 163             source = record.getLoggerName();
 164         }
 165         String message = formatMessage(record);
 166         String throwable = "";
 167         if (record.getThrown() != null) {
 168             StringWriter sw = new StringWriter();
 169             PrintWriter pw = new PrintWriter(sw);
 170             pw.println();
 171             record.getThrown().printStackTrace(pw);
 172             pw.close();
 173             throwable = sw.toString();
 174         }
 175         return String.format(format,


  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 sun.util.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


 138      *      <p>Since JDK 1.9, {@code java.util.logging} uses {@link
 139      *         java.time.Clock#systemUTC() java.time} to create more precise time
 140      *         stamps.
 141      *         The format above can be used to add a {@code .%1$tN} to the
 142      *         date/time formatting so that nanoseconds will also be printed:
 143      *     <pre>
 144      *     Feb 06, 2015 5:33:10.279216000 PM example.Main main
 145      *     INFO: This is a test
 146      *     </pre></li>
 147      * </ul>
 148      * <p>This method can also be overridden in a subclass.
 149      * It is recommended to use the {@link Formatter#formatMessage}
 150      * convenience method to localize and format the message field.
 151      *
 152      * @param record the log record to be formatted.
 153      * @return a formatted log record
 154      */
 155     @Override
 156     public synchronized String format(LogRecord record) {
 157         ZonedDateTime zdt = ZonedDateTime.ofInstant(
 158                 record.getInstant(), ZoneId.systemDefault());
 159         String source;
 160         if (record.getSourceClassName() != null) {
 161             source = record.getSourceClassName();
 162             if (record.getSourceMethodName() != null) {
 163                source += " " + record.getSourceMethodName();
 164             }
 165         } else {
 166             source = record.getLoggerName();
 167         }
 168         String message = formatMessage(record);
 169         String throwable = "";
 170         if (record.getThrown() != null) {
 171             StringWriter sw = new StringWriter();
 172             PrintWriter pw = new PrintWriter(sw);
 173             pw.println();
 174             record.getThrown().printStackTrace(pw);
 175             pw.close();
 176             throwable = sw.toString();
 177         }
 178         return String.format(format,
< prev index next >