Save This Page
Home » openjdk-7 » java » util » logging » [javadoc | source]
    1   /*
    2    * Copyright 2000-2006 Sun Microsystems, Inc.  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.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   
   27   package java.util.logging;
   28   
   29   import java.io;
   30   import java.text;
   31   import java.util.Date;
   32   
   33   /**
   34    * Print a brief summary of the LogRecord in a human readable
   35    * format.  The summary will typically be 1 or 2 lines.
   36    *
   37    * @since 1.4
   38    */
   39   
   40   public class SimpleFormatter extends Formatter {
   41   
   42       Date dat = new Date();
   43       private final static String format = "{0,date} {0,time}";
   44       private MessageFormat formatter;
   45   
   46       private Object args[] = new Object[1];
   47   
   48       // Line separator string.  This is the value of the line.separator
   49       // property at the moment that the SimpleFormatter was created.
   50       private String lineSeparator = java.security.AccessController.doPrivileged(
   51                  new sun.security.action.GetPropertyAction("line.separator"));
   52   
   53       /**
   54        * Format the given LogRecord.
   55        * <p>
   56        * This method can be overridden in a subclass.
   57        * It is recommended to use the {@link Formatter#formatMessage}
   58        * convenience method to localize and format the message field.
   59        *
   60        * @param record the log record to be formatted.
   61        * @return a formatted log record
   62        */
   63       public synchronized String format(LogRecord record) {
   64           StringBuffer sb = new StringBuffer();
   65           // Minimize memory allocations here.
   66           dat.setTime(record.getMillis());
   67           args[0] = dat;
   68           StringBuffer text = new StringBuffer();
   69           if (formatter == null) {
   70               formatter = new MessageFormat(format);
   71           }
   72           formatter.format(args, text, null);
   73           sb.append(text);
   74           sb.append(" ");
   75           if (record.getSourceClassName() != null) {
   76               sb.append(record.getSourceClassName());
   77           } else {
   78               sb.append(record.getLoggerName());
   79           }
   80           if (record.getSourceMethodName() != null) {
   81               sb.append(" ");
   82               sb.append(record.getSourceMethodName());
   83           }
   84           sb.append(lineSeparator);
   85           String message = formatMessage(record);
   86           sb.append(record.getLevel().getLocalizedName());
   87           sb.append(": ");
   88           sb.append(message);
   89           sb.append(lineSeparator);
   90           if (record.getThrown() != null) {
   91               try {
   92                   StringWriter sw = new StringWriter();
   93                   PrintWriter pw = new PrintWriter(sw);
   94                   record.getThrown().printStackTrace(pw);
   95                   pw.close();
   96                   sb.append(sw.toString());
   97               } catch (Exception ex) {
   98               }
   99           }
  100           return sb.toString();
  101       }
  102   }

Save This Page
Home » openjdk-7 » java » util » logging » [javadoc | source]