KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > OneLineSimpleLogger


1 /* OneLineSimpleLogger
2  *
3  * $Id: OneLineSimpleLogger.java,v 1.2 2004/11/17 02:55:21 stack-sf Exp $
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 JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.text.FieldPosition JavaDoc;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.logging.ConsoleHandler JavaDoc;
33 import java.util.logging.Handler JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.LogRecord JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import java.util.logging.SimpleFormatter JavaDoc;
38
39
40
41 /**
42  * Logger that writes entry on one line with less verbose date.
43  *
44  * @author stack
45  * @version $Revision: 1.2 $, $Date: 2004/11/17 02:55:21 $
46  */

47 public class OneLineSimpleLogger extends SimpleFormatter JavaDoc {
48     
49     /**
50      * Date instance.
51      *
52      * Keep around instance of date.
53      */

54     private Date JavaDoc date = new Date JavaDoc();
55     
56     /**
57      * Field position instance.
58      *
59      * Keep around this instance.
60      */

61     private FieldPosition JavaDoc position = new FieldPosition JavaDoc(0);
62     
63     /**
64      * MessageFormatter for date.
65      */

66     private SimpleDateFormat JavaDoc formatter =
67         new SimpleDateFormat JavaDoc("MM/dd/yyyy HH:mm:ss Z");
68     
69     /**
70      * Persistent buffer in which we conjure the log.
71      */

72     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
73     
74
75     public OneLineSimpleLogger() {
76         super();
77     }
78     
79     public synchronized String JavaDoc format(LogRecord JavaDoc 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 JavaDoc 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 JavaDoc writer = new StringWriter JavaDoc();
103                 PrintWriter JavaDoc printer = new PrintWriter JavaDoc(writer);
104                 record.getThrown().printStackTrace(printer);
105                 writer.close();
106                 buffer.append(writer.toString());
107             } catch (Exception JavaDoc e) {
108                 buffer.append("Failed to get stack trace: " + e.getMessage());
109             }
110         }
111         return buffer.toString();
112     }
113     
114     public static Logger JavaDoc setConsoleHandler() {
115         Logger JavaDoc logger = Logger.getLogger("");
116         Handler JavaDoc [] hs = logger.getHandlers();
117         for (int i = 0; i < hs.length; i++) {
118             Handler JavaDoc h = hs[0];
119             if (h instanceof ConsoleHandler JavaDoc) {
120                 h.setFormatter(new OneLineSimpleLogger());
121             }
122         }
123         return logger;
124     }
125
126     /**
127      * Test this logger.
128      */

129     public static void main(String JavaDoc[] args) {
130         Logger JavaDoc 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 JavaDoc("test"));
136     }
137 }
138
Popular Tags