KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > log > StreamHandler


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source 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, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.log;
30
31 import com.caucho.util.L10N;
32 import com.caucho.vfs.WriteStream;
33
34 import java.util.logging.Formatter JavaDoc;
35 import java.util.logging.Handler JavaDoc;
36 import java.util.logging.LogRecord JavaDoc;
37
38 /**
39  * Resin's rotating path-based log.
40  */

41 public class StreamHandler extends Handler JavaDoc {
42   private static L10N L = new L10N(StreamHandler.class);
43   
44   private WriteStream _os;
45
46   private Formatter JavaDoc _formatter;
47
48   private String JavaDoc _timestamp;
49
50   public StreamHandler(WriteStream os)
51   {
52     _os = os;
53   }
54
55   /**
56    * Sets the timestamp.
57    */

58   public void setTimestamp(String JavaDoc timestamp)
59   {
60     _timestamp = timestamp;
61   }
62
63   /**
64    * Sets the formatter.
65    */

66   public void setFormatter(Formatter JavaDoc formatter)
67   {
68     _formatter = formatter;
69   }
70
71   /**
72    * Publishes the record.
73    */

74   public void publish(LogRecord JavaDoc record)
75   {
76     if (record.getLevel().intValue() < getLevel().intValue())
77       return;
78
79     try {
80       if (record == null) {
81     synchronized (_os) {
82       _os.println("no record");
83       _os.flush();
84     }
85     return;
86       }
87
88       if (_formatter != null) {
89     String JavaDoc value = _formatter.format(record);
90
91     synchronized (_os) {
92       _os.println(value);
93       _os.flush();
94     }
95     
96     return;
97       }
98       
99       String JavaDoc message = record.getMessage();
100       Throwable JavaDoc thrown = record.getThrown();
101
102       synchronized (_os) {
103     if (_timestamp != null) {
104       _os.print(_timestamp);
105     }
106       
107     if (thrown != null) {
108       if (message != null &&
109           ! message.equals(thrown.toString()) &&
110           ! message.equals(thrown.getMessage()))
111         _os.println(message);
112     
113       record.getThrown().printStackTrace(_os.getPrintWriter());
114     }
115     else {
116       _os.println(record.getMessage());
117     }
118     _os.flush();
119       }
120     } catch (Throwable JavaDoc e) {
121       e.printStackTrace();
122     }
123   }
124
125   /**
126    * Flushes the buffer.
127    */

128   public void flush()
129   {
130   }
131
132   /**
133    * Closes the handler.
134    */

135   public void close()
136   {
137   }
138
139   /**
140    * Returns the hash code.
141    */

142   public int hashCode()
143   {
144     if (_os == null || _os.getPath() == null)
145       return super.hashCode();
146     else
147     return _os.getPath().hashCode();
148   }
149
150   /**
151    * Test for equality.
152    */

153   public boolean equals(Object JavaDoc o)
154   {
155     if (this == o)
156       return true;
157     else if (getClass() != o.getClass())
158       return false;
159
160     StreamHandler handler = (StreamHandler) o;
161
162     if (_os == null || handler._os == null)
163       return false;
164     else
165       return _os.getPath().equals(handler._os.getPath());
166   }
167
168   public String JavaDoc toString()
169   {
170     if (_os == null)
171       return "StreamHandler@" + System.identityHashCode(this) + "[]";
172     else
173       return ("StreamHandler@" + System.identityHashCode(this)
174           + "[" + _os.getPath() + "]");
175   }
176 }
177
Popular Tags