1 /*g 2 * Copyright (c) 2000, 2012, 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.text.*; 31 import java.util.Date; 32 import sun.util.logging.LoggingSupport; 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 private static final String format = LoggingSupport.getSimpleFormat(); 63 private final Date dat = new Date(); 64 65 /** 66 * Format the given LogRecord. 67 * <p> 68 * The formatting can be customized by specifying the 69 * <a href="../Formatter.html#syntax">format string</a> 70 * in the <a href="#formatting"> 71 * {@code java.util.logging.SimpleFormatter.format}</a> property. 72 * The given {@code LogRecord} will be formatted as if by calling: 73 * <pre> 74 * {@link String#format String.format}(format, date, source, logger, level, message, thrown); 75 * </pre> 76 * where the arguments are:<br> 77 * <ol> 78 * <li>{@code format} - the {@link java.util.Formatter 79 * java.util.Formatter} format string specified in the 80 * {@code java.util.logging.SimpleFormatter.format} property 81 * or the default format.</li> 82 * <li>{@code date} - a {@link Date} object representing 83 * {@linkplain LogRecord#getMillis event time} of the log record.</li> 84 * <li>{@code source} - a string representing the caller, if available; 85 * otherwise, the logger's name.</li> 86 * <li>{@code logger} - the logger's name.</li> 87 * <li>{@code level} - the {@linkplain Level#getLocalizedName 88 * log level}.</li> 89 * <li>{@code message} - the formatted log message 90 * returned from the {@link Formatter#formatMessage(LogRecord)} 91 * method. It uses {@link java.text.MessageFormat java.text} 92 * formatting and does not use the {@code java.util.Formatter 93 * format} argument.</li> 94 * <li>{@code thrown} - a string representing 95 * the {@linkplain LogRecord#getThrown throwable} 96 * associated with the log record and its backtrace 97 * beginning with a newline character, if any; 98 * otherwise, an empty string.</li> 99 * </ol> 100 * 101 * <p>Some example formats:<br> 102 * <ul> 103 * <li> {@code java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n"} 104 * <p>This prints 1 line with the log level ({@code 4$}), 105 * the log message ({@code 5$}) and the timestamp ({@code 1$}) in 106 * a square bracket. 107 * <pre> 108 * WARNING: warning message [Tue Mar 22 13:11:31 PDT 2011] 109 * </pre></li> 110 * <li> {@code java.util.logging.SimpleFormatter.format="%1$tc %2$s%n%4$s: %5$s%6$s%n"} 111 * <p>This prints 2 lines where the first line includes 112 * the timestamp ({@code 1$}) and the source ({@code 2$}); 113 * the second line includes the log level ({@code 4$}) and 114 * the log message ({@code 5$}) followed with the throwable 115 * and its backtrace ({@code 6$}), if any: 116 * <pre> 117 * Tue Mar 22 13:11:31 PDT 2011 MyClass fatal 118 * SEVERE: several message with an exception 119 * java.lang.IllegalArgumentException: invalid argument 120 * at MyClass.mash(MyClass.java:9) 121 * at MyClass.crunch(MyClass.java:6) 122 * at MyClass.main(MyClass.java:3) 123 * </pre></li> 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"} 125 * <p>This prints 2 lines similar to the example above 126 * with a different date/time formatting and does not print 127 * the throwable and its backtrace: 128 * <pre> 129 * Mar 22, 2011 1:11:31 PM MyClass fatal 130 * SEVERE: several message with an exception 131 * </pre></li> 132 * </ul> 133 * <p>This method can also be overridden in a subclass. 134 * It is recommended to use the {@link Formatter#formatMessage} 135 * convenience method to localize and format the message field. 136 * 137 * @param record the log record to be formatted. 138 * @return a formatted log record 139 */ format(LogRecord record)140 public synchronized String format(LogRecord record) { 141 dat.setTime(record.getMillis()); 142 String source; 143 if (record.getSourceClassName() != null) { 144 source = record.getSourceClassName(); 145 if (record.getSourceMethodName() != null) { 146 source += " " + record.getSourceMethodName(); 147 } 148 } else { 149 source = record.getLoggerName(); 150 } 151 String message = formatMessage(record); 152 String throwable = ""; 153 if (record.getThrown() != null) { 154 StringWriter sw = new StringWriter(); 155 PrintWriter pw = new PrintWriter(sw); 156 pw.println(); 157 record.getThrown().printStackTrace(pw); 158 pw.close(); 159 throwable = sw.toString(); 160 } 161 return String.format(format, 162 dat, 163 source, 164 record.getLoggerName(), 165 record.getLevel().getLocalizedLevelName(), 166 message, 167 throwable); 168 } 169 } 170