001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.logging;
027
028 import java.io.*;
029 import java.text.*;
030 import java.util.Date;
031
032 /**
033 * Print a brief summary of the LogRecord in a human readable
034 * format. The summary will typically be 1 or 2 lines.
035 *
036 * @version 1.22, 05/05/07
037 * @since 1.4
038 */
039
040 public class SimpleFormatter extends Formatter {
041
042 Date dat = new Date();
043 private final static String format = "{0,date} {0,time}";
044 private MessageFormat formatter;
045
046 private Object args[] = new Object[1];
047
048 // Line separator string. This is the value of the line.separator
049 // property at the moment that the SimpleFormatter was created.
050 private String lineSeparator = (String) java.security.AccessController
051 .doPrivileged(new sun.security.action.GetPropertyAction(
052 "line.separator"));
053
054 /**
055 * Format the given LogRecord.
056 * <p>
057 * This method can be overridden in a subclass.
058 * It is recommended to use the {@link Formatter#formatMessage}
059 * convenience method to localize and format the message field.
060 *
061 * @param record the log record to be formatted.
062 * @return a formatted log record
063 */
064 public synchronized String format(LogRecord record) {
065 StringBuffer sb = new StringBuffer();
066 // Minimize memory allocations here.
067 dat.setTime(record.getMillis());
068 args[0] = dat;
069 StringBuffer text = new StringBuffer();
070 if (formatter == null) {
071 formatter = new MessageFormat(format);
072 }
073 formatter.format(args, text, null);
074 sb.append(text);
075 sb.append(" ");
076 if (record.getSourceClassName() != null) {
077 sb.append(record.getSourceClassName());
078 } else {
079 sb.append(record.getLoggerName());
080 }
081 if (record.getSourceMethodName() != null) {
082 sb.append(" ");
083 sb.append(record.getSourceMethodName());
084 }
085 sb.append(lineSeparator);
086 String message = formatMessage(record);
087 sb.append(record.getLevel().getLocalizedName());
088 sb.append(": ");
089 sb.append(message);
090 sb.append(lineSeparator);
091 if (record.getThrown() != null) {
092 try {
093 StringWriter sw = new StringWriter();
094 PrintWriter pw = new PrintWriter(sw);
095 record.getThrown().printStackTrace(pw);
096 pw.close();
097 sb.append(sw.toString());
098 } catch (Exception ex) {
099 }
100 }
101 return sb.toString();
102 }
103 }
|