001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source 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, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.log;
030:
031: import com.caucho.util.L10N;
032: import com.caucho.vfs.WriteStream;
033:
034: import java.util.logging.Formatter;
035: import java.util.logging.Handler;
036: import java.util.logging.LogRecord;
037:
038: /**
039: * Resin's rotating path-based log.
040: */
041: public class StreamHandler extends Handler {
042: private static L10N L = new L10N(StreamHandler.class);
043:
044: private WriteStream _os;
045:
046: private Formatter _formatter;
047:
048: private String _timestamp;
049:
050: public StreamHandler(WriteStream os) {
051: _os = os;
052: }
053:
054: /**
055: * Sets the timestamp.
056: */
057: public void setTimestamp(String timestamp) {
058: _timestamp = timestamp;
059: }
060:
061: /**
062: * Sets the formatter.
063: */
064: public void setFormatter(Formatter formatter) {
065: _formatter = formatter;
066: }
067:
068: /**
069: * Publishes the record.
070: */
071: public void publish(LogRecord record) {
072: if (record.getLevel().intValue() < getLevel().intValue())
073: return;
074:
075: try {
076: if (record == null) {
077: synchronized (_os) {
078: _os.println("no record");
079: _os.flush();
080: }
081: return;
082: }
083:
084: if (_formatter != null) {
085: String value = _formatter.format(record);
086:
087: synchronized (_os) {
088: _os.println(value);
089: _os.flush();
090: }
091:
092: return;
093: }
094:
095: String message = record.getMessage();
096: Throwable thrown = record.getThrown();
097:
098: synchronized (_os) {
099: if (_timestamp != null) {
100: _os.print(_timestamp);
101: }
102:
103: if (thrown != null) {
104: if (message != null
105: && !message.equals(thrown.toString())
106: && !message.equals(thrown.getMessage()))
107: _os.println(message);
108:
109: record.getThrown().printStackTrace(
110: _os.getPrintWriter());
111: } else {
112: _os.println(record.getMessage());
113: }
114: _os.flush();
115: }
116: } catch (Throwable e) {
117: e.printStackTrace();
118: }
119: }
120:
121: /**
122: * Flushes the buffer.
123: */
124: public void flush() {
125: }
126:
127: /**
128: * Closes the handler.
129: */
130: public void close() {
131: }
132:
133: /**
134: * Returns the hash code.
135: */
136: public int hashCode() {
137: if (_os == null || _os.getPath() == null)
138: return super .hashCode();
139: else
140: return _os.getPath().hashCode();
141: }
142:
143: /**
144: * Test for equality.
145: */
146: public boolean equals(Object o) {
147: if (this == o)
148: return true;
149: else if (getClass() != o.getClass())
150: return false;
151:
152: StreamHandler handler = (StreamHandler) o;
153:
154: if (_os == null || handler._os == null)
155: return false;
156: else
157: return _os.getPath().equals(handler._os.getPath());
158: }
159:
160: public String toString() {
161: if (_os == null)
162: return "StreamHandler@" + System.identityHashCode(this )
163: + "[]";
164: else
165: return ("StreamHandler@" + System.identityHashCode(this )
166: + "[" + _os.getPath() + "]");
167: }
168: }
|