001: /* OneLineSimpleLogger
002: *
003: * $Id: OneLineSimpleLogger.java 2818 2004-11-17 02:55:48Z stack-sf $
004: *
005: * Created on Jul 22, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.util;
026:
027: import java.io.PrintWriter;
028: import java.io.StringWriter;
029: import java.text.FieldPosition;
030: import java.text.SimpleDateFormat;
031: import java.util.Date;
032: import java.util.logging.ConsoleHandler;
033: import java.util.logging.Handler;
034: import java.util.logging.Level;
035: import java.util.logging.LogRecord;
036: import java.util.logging.Logger;
037: import java.util.logging.SimpleFormatter;
038:
039: /**
040: * Logger that writes entry on one line with less verbose date.
041: *
042: * @author stack
043: * @version $Revision: 2818 $, $Date: 2004-11-17 02:55:48 +0000 (Wed, 17 Nov 2004) $
044: */
045: public class OneLineSimpleLogger extends SimpleFormatter {
046:
047: /**
048: * Date instance.
049: *
050: * Keep around instance of date.
051: */
052: private Date date = new Date();
053:
054: /**
055: * Field position instance.
056: *
057: * Keep around this instance.
058: */
059: private FieldPosition position = new FieldPosition(0);
060:
061: /**
062: * MessageFormatter for date.
063: */
064: private SimpleDateFormat formatter = new SimpleDateFormat(
065: "MM/dd/yyyy HH:mm:ss Z");
066:
067: /**
068: * Persistent buffer in which we conjure the log.
069: */
070: private StringBuffer buffer = new StringBuffer();
071:
072: public OneLineSimpleLogger() {
073: super ();
074: }
075:
076: public synchronized String format(LogRecord record) {
077: this .buffer.setLength(0);
078: this .date.setTime(record.getMillis());
079: this .position.setBeginIndex(0);
080: this .formatter.format(this .date, buffer, this .position);
081: buffer.append(' ');
082: buffer.append(record.getLevel().getLocalizedName());
083: buffer.append(' ');
084: if (record.getSourceClassName() != null) {
085: buffer.append(record.getSourceClassName());
086: } else {
087: buffer.append(record.getLoggerName());
088: }
089: buffer.append(' ');
090: String methodName = record.getSourceMethodName();
091: methodName = (methodName == null || methodName.length() <= 0) ? "-"
092: : methodName;
093: buffer.append(methodName);
094: buffer.append(' ');
095: buffer.append(formatMessage(record));
096: buffer.append(System.getProperty("line.separator"));
097: if (record.getThrown() != null) {
098: try {
099: StringWriter writer = new StringWriter();
100: PrintWriter printer = new PrintWriter(writer);
101: record.getThrown().printStackTrace(printer);
102: writer.close();
103: buffer.append(writer.toString());
104: } catch (Exception e) {
105: buffer.append("Failed to get stack trace: "
106: + e.getMessage());
107: }
108: }
109: return buffer.toString();
110: }
111:
112: public static Logger setConsoleHandler() {
113: Logger logger = Logger.getLogger("");
114: Handler[] hs = logger.getHandlers();
115: for (int i = 0; i < hs.length; i++) {
116: Handler h = hs[0];
117: if (h instanceof ConsoleHandler) {
118: h.setFormatter(new OneLineSimpleLogger());
119: }
120: }
121: return logger;
122: }
123:
124: /**
125: * Test this logger.
126: */
127: public static void main(String[] args) {
128: Logger logger = setConsoleHandler();
129: logger = Logger.getLogger("Test");
130: logger.severe("Does this come out?");
131: logger.severe("Does this come out?");
132: logger.severe("Does this come out?");
133: logger.log(Level.SEVERE, "hello", new RuntimeException("test"));
134: }
135: }
|