KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > log > LogEvent


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.log;
54
55 import java.io.*;
56 import java.util.*;
57 import java.lang.ref.WeakReference JavaDoc;
58
59 /******************************************************************************
60  * LogEvent captures information that should be logged. LogEvents are one
61  * of four types: debug, info, warn or error. All LogEvents have a
62  * timestamp for when the event occurred and a reference to the thread
63  * that created it. Most have an embedded message, and some have an
64  * embedded exception.
65  *
66  * @author Brian S O'Neill
67  * @version
68  * <!--$$Revision:--> 5 <!-- $-->, <!--$$JustDate:--> 9/07/00 <!-- $-->
69  */

70 public class LogEvent extends EventObject {
71     /** Debug type of LogEvent */
72     public static final int DEBUG_TYPE = 1;
73
74     /** Info type of LogEvent */
75     public static final int INFO_TYPE = 2;
76
77     /** Warn type of LogEvent */
78     public static final int WARN_TYPE = 3;
79
80     /** Error type of LogEvent */
81     public static final int ERROR_TYPE = 4;
82
83     private int mType;
84     private Date mTimestamp;
85     private String JavaDoc mMessage;
86     private Throwable JavaDoc mThrowable;
87     private String JavaDoc mThreadName;
88     // WeakReference to a Thread.
89
private transient WeakReference JavaDoc mThread;
90     
91     public LogEvent(Log log, int type,
92                     String JavaDoc message, Throwable JavaDoc throwable,
93                     Thread JavaDoc thread, Date timestamp) {
94         super(log);
95         
96         if (type < DEBUG_TYPE || type > ERROR_TYPE) {
97             throw new IllegalArgumentException JavaDoc
98                 ("Type out of range: " + type);
99         }
100         
101         mType = type;
102         
103         if (message == null) {
104             if (throwable != null) {
105                 mMessage = throwable.getMessage();
106             }
107         }
108         else {
109             mMessage = message;
110         }
111         
112         mThrowable = throwable;
113         
114         if (thread == null) {
115             mThread = new WeakReference JavaDoc(Thread.currentThread());
116         }
117         else {
118             mThread = new WeakReference JavaDoc(thread);
119         }
120         
121         if (timestamp == null) {
122             mTimestamp = new Date();
123         }
124         else {
125             mTimestamp = timestamp;
126         }
127     }
128     
129     public LogEvent(Log log, int type,
130                     String JavaDoc message, Thread JavaDoc thread, Date timestamp) {
131         this(log, type, message, null, thread, timestamp);
132     }
133     
134     public LogEvent(Log log, int type,
135                     Throwable JavaDoc throwable, Thread JavaDoc thread, Date timestamp) {
136         this(log, type, null, throwable, thread, timestamp);
137     }
138     
139     public LogEvent(Log log, int type,
140                     String JavaDoc message, Thread JavaDoc thread) {
141         this(log, type, message, null, thread, null);
142     }
143     
144     public LogEvent(Log log, int type,
145                     Throwable JavaDoc throwable, Thread JavaDoc thread) {
146         this(log, type, null, throwable, thread, null);
147     }
148     
149     public LogEvent(Log log, int type,
150                     String JavaDoc message, Throwable JavaDoc throwable) {
151         this(log, type, message, throwable, null, null);
152     }
153     
154     public LogEvent(Log log, int type, String JavaDoc message) {
155         this(log, type, message, null, null, null);
156     }
157     
158     public LogEvent(Log log, int type, Throwable JavaDoc throwable) {
159         this(log, type, null, throwable, null, null);
160     }
161     
162     public Log getLogSource() {
163         return (Log)getSource();
164     }
165     
166     /**
167      * Returns the type of this LogEvent, which matches one of the defined
168      * type constants.
169      */

170     public int getType() {
171         return mType;
172     }
173     
174     /**
175      * Returns the date and time of this event.
176      */

177     public Date getTimestamp() {
178         return mTimestamp;
179     }
180     
181     /**
182      * Message may be null.
183      */

184     public String JavaDoc getMessage() {
185         return mMessage;
186     }
187     
188     /**
189      * Returns null if there is no exception logged.
190      */

191     public Throwable JavaDoc getException() {
192         return mThrowable;
193     }
194     
195     /**
196      * Returns null if there is no exception logged.
197      */

198     public String JavaDoc getExceptionStackTrace() {
199         Throwable JavaDoc t = getException();
200         if (t == null) {
201             return null;
202         }
203         StringWriter sw = new StringWriter();
204         PrintWriter pw = new PrintWriter(sw);
205         t.printStackTrace(pw);
206         return sw.toString();
207     }
208     
209     /**
210      * Returns the name of the thread that created this event.
211      */

212     public String JavaDoc getThreadName() {
213         if (mThreadName == null) {
214             Thread JavaDoc t = getThread();
215             if (t != null) {
216                 mThreadName = t.getName();
217             }
218         }
219         return mThreadName;
220     }
221     
222     /**
223      * Returns the thread that created this event, which may be null if
224      * this LogEvent was deserialized or the thread has been reclaimed.
225      */

226     public Thread JavaDoc getThread() {
227         return (Thread JavaDoc)mThread.get();
228     }
229     
230     public String JavaDoc toString() {
231         String JavaDoc msg;
232         if (getMessage() == null) {
233             msg = "null";
234         }
235         else {
236             msg = '"' + getMessage() + '"';
237         }
238         
239         return
240             getClass().getName() + "[" +
241             getTimestamp() + ',' +
242             getThreadName() + ',' +
243             msg +
244             "] from " + getSource();
245     }
246     
247     private void writeObject(ObjectOutputStream out) throws IOException {
248         getThreadName();
249         out.defaultWriteObject();
250     }
251     
252     private void readObject(ObjectInputStream in)
253         throws IOException, ClassNotFoundException JavaDoc {
254         in.defaultReadObject();
255     }
256 }
257
Popular Tags