    1   /* OneLineSimpleLogger
    2    *
    3    * $Id: OneLineSimpleLogger.java 2818 2004-11-17 02:55:48Z stack-sf $
    4    *
    5    * Created on Jul 22, 2004
    6    *
    7    * Copyright (C) 2004 Internet Archive.
    8    *
    9    * This file is part of the Heritrix web crawler (crawler.archive.org).
    10   *
    11   * Heritrix is free software; you can redistribute it and/or modify
    12   * it under the terms of the GNU Lesser Public License as published by
    13   * the Free Software Foundation; either version 2.1 of the License, or
    14   * any later version.
    15   *
    16   * Heritrix is distributed in the hope that it will be useful,
    17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19   * GNU Lesser Public License for more details.
    20   *
    21   * You should have received a copy of the GNU Lesser Public License
    22   * along with Heritrix; if not, write to the Free Software
    23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24   */
    25  package org.archive.util;
    26
    27  import java.io.PrintWriter;
    28  import java.io.StringWriter;
    29  import java.text.FieldPosition;
    30  import java.text.SimpleDateFormat;
    31  import java.util.Date;
    32  import java.util.logging.ConsoleHandler;
    33  import java.util.logging.Handler;
    34  import java.util.logging.Level;
    35  import java.util.logging.LogRecord;
    36  import java.util.logging.Logger;
    37  import java.util.logging.SimpleFormatter;
    38
    39
    40
    41  /***
    42   * Logger that writes entry on one line with less verbose date.
    43   *
    44   * @author stack
    45   * @version $Revision: 2818 $, $Date: 2004-11-17 02:55:48 +0000 (Wed, 17 Nov 2004) $
    46   */
    47  public class OneLineSimpleLogger extends SimpleFormatter {
    48
    49      /***
    50       * Date instance.
    51       *
    52       * Keep around instance of date.
    53       */
    54      private Date date = new Date();
    55
    56      /***
    57       * Field position instance.
    58       *
    59       * Keep around this instance.
    60       */
    61      private FieldPosition position = new FieldPosition(0);
    62
    63      /***
    64       * MessageFormatter for date.
    65       */
    66      private SimpleDateFormat formatter =
    67          new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
    68
    69      /***
    70       * Persistent buffer in which we conjure the log.
    71       */
    72      private StringBuffer buffer = new StringBuffer();
    73
    74
    75      public OneLineSimpleLogger() {
    76          super();
    77      }
    78
    79      public synchronized String format(LogRecord record) {
    80          this.buffer.setLength(0);
    81          this.date.setTime(record.getMillis());
    82          this.position.setBeginIndex(0);
    83          this.formatter.format(this.date, buffer, this.position);
    84          buffer.append(' ');
    85          buffer.append(record.getLevel().getLocalizedName());
    86          buffer.append(' ');
    87          if (record.getSourceClassName() != null) {
    88              buffer.append(record.getSourceClassName());
    89          } else {
    90              buffer.append(record.getLoggerName());
    91          }
    92          buffer.append(' ');
    93          String methodName = record.getSourceMethodName();
    94          methodName = (methodName == null || methodName.length() <= 0)?
    95              "-": methodName;
    96          buffer.append(methodName);
    97          buffer.append(' ');
    98          buffer.append(formatMessage(record));
    99          buffer.append(System.getProperty("line.separator"));
    100         if (record.getThrown() != null) {
    101     	    try {
    102     	        StringWriter writer = new StringWriter();
    103     	        PrintWriter printer = new PrintWriter(writer);
    104     	        record.getThrown().printStackTrace(printer);
    105     	        writer.close();
    106     	        buffer.append(writer.toString());
    107     	    } catch (Exception e) {
    108     	        buffer.append("Failed to get stack trace: " + e.getMessage());
    109     	    }
    110         }
    111         return buffer.toString();
    112     }
    113
    114     public static Logger setConsoleHandler() {
    115         Logger logger = Logger.getLogger("");
    116         Handler [] hs = logger.getHandlers();
    117         for (int i = 0; i < hs.length; i++) {
    118             Handler h = hs[0];
    119             if (h instanceof ConsoleHandler) {
    120                 h.setFormatter(new OneLineSimpleLogger());
    121             }
    122         }
    123         return logger;
    124     }
    125
    126     /***
    127      * Test this logger.
    128      */
    129     public static void main(String[] args) {
    130         Logger logger = setConsoleHandler();
    131         logger = Logger.getLogger("Test");
    132         logger.severe("Does this come out?");
    133         logger.severe("Does this come out?");
    134         logger.severe("Does this come out?");
    135         logger.log(Level.SEVERE, "hello", new RuntimeException("test"));
    136     }
    137 }