001: /* SinkHandlerLogRecord
002: *
003: * Created Aug 9, 2005
004: *
005: * Copyright (C) 2005 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023:
024: package org.archive.io;
025:
026: import java.io.StringWriter;
027: import java.util.Date;
028: import java.util.logging.Level;
029: import java.util.logging.LogRecord;
030:
031: import org.archive.crawler.framework.ToeThread;
032:
033: /**
034: * Version of LogRecord used by SinkHandler.
035: * Adds being able to mark the LogRecord as already-read and timestamping time
036: * of creation. Also adds a different {@link #toString()} implementation.
037: * Delegates all other calls to the passed LogRecord.
038: * @author stack
039: * @version $Date: 2006-08-15 04:39:00 +0000 (Tue, 15 Aug 2006) $ $Version$
040: */
041: public class SinkHandlerLogRecord extends LogRecord {
042: private static final long serialVersionUID = -7782942650334713560L;
043: boolean read = false;
044: private final LogRecord delegatee;
045: private final Date creationTime = new Date();
046: private static final int SHORT_MSG_LENGTH = 80;
047:
048: protected SinkHandlerLogRecord() {
049: this (null);
050: }
051:
052: public SinkHandlerLogRecord(final LogRecord record) {
053: super (record.getLevel(), record.getMessage());
054: // if available, append current processor name to message
055: // [ 1108006 ] alerts should show current processor
056: // http://sourceforge.net/tracker/index.php?func=detail&aid=1108006&group_id=73833&atid=539102
057: if (Thread.currentThread() instanceof ToeThread) {
058: String newMessage = this .getMessage();
059: ToeThread tt = (ToeThread) Thread.currentThread();
060: newMessage = newMessage + " (in thread '" + tt.getName()
061: + "'";
062: if (tt.getCurrentProcessorName().length() > 0) {
063: newMessage = newMessage + "; in processor '"
064: + tt.getCurrentProcessorName() + "'";
065: }
066: newMessage = newMessage + ")";
067: this .setMessage(newMessage);
068: }
069: this .delegatee = record;
070: }
071:
072: public boolean equals(final long id) {
073: return id == getSequenceNumber();
074: }
075:
076: public boolean equals(final SinkHandlerLogRecord compare) {
077: return equals(compare.getSequenceNumber());
078: }
079:
080: public boolean isRead() {
081: return this .read;
082: }
083:
084: /**
085: * Mark alert as seen (That is, isNew() no longer returns true).
086: */
087: public void setRead() {
088: this .read = true;
089: }
090:
091: /**
092: * @return Time of creation
093: */
094: public Date getCreationTime() {
095: return this .creationTime;
096: }
097:
098: public Level getLevel() {
099: return this .delegatee.getLevel();
100: }
101:
102: public String getLoggerName() {
103: return this .delegatee.getLoggerName();
104: }
105:
106: public String getShortMessage() {
107: String msg = getMessage();
108: return msg == null || msg.length() < SHORT_MSG_LENGTH ? msg
109: : msg.substring(0, SHORT_MSG_LENGTH) + "...";
110: }
111:
112: public Throwable getThrown() {
113: return this .delegatee.getThrown();
114: }
115:
116: public String getThrownToString() {
117: StringWriter sw = new StringWriter();
118: Throwable t = getThrown();
119: if (t == null) {
120: sw.write("No associated exception.");
121: } else {
122: String tStr = t.toString();
123: sw.write(tStr);
124: if (t.getMessage() != null && t.getMessage().length() > 0
125: && !tStr.endsWith(t.getMessage())) {
126: sw.write("\nMessage: ");
127: sw.write(t.getMessage());
128: }
129: if (t.getCause() != null) {
130: sw.write("\nCause: ");
131: t.getCause().printStackTrace(
132: new java.io.PrintWriter(sw));
133: }
134: sw.write("\nStacktrace: ");
135: t.printStackTrace(new java.io.PrintWriter(sw));
136: }
137: return sw.toString();
138: }
139:
140: public String toString() {
141: StringWriter sw = new StringWriter();
142: sw.write(getLevel().toString());
143: sw.write(" ");
144: sw.write(getMessage());
145: sw.write(getThrownToString());
146: return sw.toString();
147: }
148: }
|