KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > SinkHandlerLogRecord


1 /* SinkHandlerLogRecord
2  *
3  * Created Aug 9, 2005
4  *
5  * Copyright (C) 2005 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.archive.io;
25
26 import java.io.StringWriter JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30
31 import org.archive.crawler.framework.ToeThread;
32
33 /**
34  * Version of LogRecord used by SinkHandler.
35  * Adds being able to mark the LogRecord as already-read and timestamping time
36  * of creation. Also adds a different {@link #toString()} implementation.
37  * Delegates all other calls to the passed LogRecord.
38  * @author stack
39  * @version $Date: 2006/08/15 04:38:59 $ $Version$
40  */

41 public class SinkHandlerLogRecord extends LogRecord JavaDoc {
42     private static final long serialVersionUID = -7782942650334713560L;
43     boolean read = false;
44     private final LogRecord JavaDoc delegatee;
45     private final Date JavaDoc creationTime = new Date JavaDoc();
46     private static final int SHORT_MSG_LENGTH = 80;
47     
48     protected SinkHandlerLogRecord() {
49         this(null);
50     }
51
52     public SinkHandlerLogRecord(final LogRecord JavaDoc record) {
53         super(record.getLevel(), record.getMessage());
54         // if available, append current processor name to message
55
// [ 1108006 ] alerts should show current processor
56
// http://sourceforge.net/tracker/index.php?func=detail&aid=1108006&group_id=73833&atid=539102
57
if(Thread.currentThread() instanceof ToeThread) {
58             String JavaDoc newMessage = this.getMessage();
59             ToeThread tt = (ToeThread) Thread.currentThread();
60             newMessage = newMessage + " (in thread '"+tt.getName()+"'";
61             if(tt.getCurrentProcessorName().length()>0) {
62                 newMessage = newMessage + "; in processor '"
63                     +tt.getCurrentProcessorName() + "'";
64             }
65             newMessage = newMessage + ")";
66             this.setMessage(newMessage);
67         }
68         this.delegatee = record;
69     }
70     
71     public boolean equals(final long id) {
72         return id == getSequenceNumber();
73     }
74     
75     public boolean equals(final SinkHandlerLogRecord compare) {
76         return equals(compare.getSequenceNumber());
77     }
78     
79     public boolean isRead() {
80         return this.read;
81     }
82
83     /**
84      * Mark alert as seen (That is, isNew() no longer returns true).
85      */

86     public void setRead() {
87         this.read = true;
88     }
89     
90     /**
91      * @return Time of creation
92      */

93     public Date JavaDoc getCreationTime() {
94         return this.creationTime;
95     }
96     
97     public Level JavaDoc getLevel() {
98         return this.delegatee.getLevel();
99     }
100     
101     public String JavaDoc getLoggerName() {
102         return this.delegatee.getLoggerName();
103     }
104     
105     public String JavaDoc getShortMessage() {
106         String JavaDoc msg = getMessage();
107         return msg == null || msg.length() < SHORT_MSG_LENGTH?
108                 msg: msg.substring(0, SHORT_MSG_LENGTH) + "...";
109     }
110     
111     public Throwable JavaDoc getThrown() {
112         return this.delegatee.getThrown();
113     }
114     
115     public String JavaDoc getThrownToString() {
116         StringWriter JavaDoc sw = new StringWriter JavaDoc();
117         Throwable JavaDoc t = getThrown();
118         if (t == null) {
119             sw.write("No associated exception.");
120         } else {
121             String JavaDoc tStr = t.toString();
122             sw.write(tStr);
123             if (t.getMessage() != null && t.getMessage().length() > 0 &&
124                     !tStr.endsWith(t.getMessage())) {
125                 sw.write("\nMessage: ");
126                 sw.write(t.getMessage());
127             }
128             if (t.getCause() != null) {
129                 sw.write("\nCause: ");
130                 t.getCause().printStackTrace(new java.io.PrintWriter JavaDoc(sw));
131             }
132             sw.write("\nStacktrace: ");
133             t.printStackTrace(new java.io.PrintWriter JavaDoc(sw));
134         }
135         return sw.toString();
136     }
137     
138     public String JavaDoc toString() {
139         StringWriter JavaDoc sw = new StringWriter JavaDoc();
140         sw.write(getLevel().toString());
141         sw.write(" ");
142         sw.write(getMessage());
143         sw.write(getThrownToString());
144         return sw.toString();
145     }
146 }
147
Popular Tags