KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > LogEvent


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log;
18
19 import java.io.ObjectStreamException JavaDoc;
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * This class encapsulates each individual log event.
24  * LogEvents usually originate at a Logger and are routed
25  * to LogTargets.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @author Peter Donald
29  */

30 public final class LogEvent
31     implements Serializable JavaDoc
32 {
33     //A Constant used when retrieving time relative to start of applicaiton start
34
private static final long START_TIME = System.currentTimeMillis();
35
36     ///The category that this LogEvent concerns. (Must not be null)
37
private String JavaDoc m_category;
38
39     ///The message to be logged. (Must not be null)
40
private String JavaDoc m_message;
41
42     ///The exception that caused LogEvent if any. (May be null)
43
private Throwable JavaDoc m_throwable;
44
45     ///The time in millis that LogEvent occurred
46
private long m_time;
47
48     ///The priority of LogEvent. (Must not be null)
49
private Priority m_priority;
50
51     ///The context map associated with LogEvent. (May be null).
52
private ContextMap m_contextMap;
53
54     /**
55      * Get Priority for LogEvent.
56      *
57      * @return the LogEvent Priority
58      */

59     public final Priority getPriority()
60     {
61         return m_priority;
62     }
63
64     /**
65      * Set the priority of LogEvent.
66      *
67      * @param priority the new LogEvent priority
68      */

69     public final void setPriority( final Priority priority )
70     {
71         m_priority = priority;
72     }
73
74     /**
75      * Get ContextMap associated with LogEvent
76      *
77      * @return the ContextMap
78      */

79     public final ContextMap getContextMap()
80     {
81         return m_contextMap;
82     }
83
84     /**
85      * Set the ContextMap for this LogEvent.
86      *
87      * @param contextMap the context map
88      */

89     public final void setContextMap( final ContextMap contextMap )
90     {
91         m_contextMap = contextMap;
92     }
93
94     /**
95      * Get the category that LogEvent relates to.
96      *
97      * @return the name of category
98      */

99     public final String JavaDoc getCategory()
100     {
101         return m_category;
102     }
103
104     /**
105      * Get the message associated with event.
106      *
107      * @return the message
108      */

109     public final String JavaDoc getMessage()
110     {
111         return m_message;
112     }
113
114     /**
115      * Get throwabe instance associated with event.
116      *
117      * @return the Throwable
118      */

119     public final Throwable JavaDoc getThrowable()
120     {
121         return m_throwable;
122     }
123
124     /**
125      * Get the absolute time of the log event.
126      *
127      * @return the absolute time
128      */

129     public final long getTime()
130     {
131         return m_time;
132     }
133
134     /**
135      * Get the time of the log event relative to start of application.
136      *
137      * @return the time
138      */

139     public final long getRelativeTime()
140     {
141         return m_time - START_TIME;
142     }
143
144     /**
145      * Set the LogEvent category.
146      *
147      * @param category the category
148      */

149     public final void setCategory( final String JavaDoc category )
150     {
151         m_category = category;
152     }
153
154     /**
155      * Set the message for LogEvent.
156      *
157      * @param message the message
158      */

159     public final void setMessage( final String JavaDoc message )
160     {
161         m_message = message;
162     }
163
164     /**
165      * Set the throwable for LogEvent.
166      *
167      * @param throwable the instance of Throwable
168      */

169     public final void setThrowable( final Throwable JavaDoc throwable )
170     {
171         m_throwable = throwable;
172     }
173
174     /**
175      * Set the absolute time of LogEvent.
176      *
177      * @param time the time
178      */

179     public final void setTime( final long time )
180     {
181         m_time = time;
182     }
183
184     /**
185      * Helper method that replaces deserialized priority with correct singleton.
186      *
187      * @return the singleton version of object
188      * @exception ObjectStreamException if an error occurs
189      */

190     private Object JavaDoc readResolve()
191         throws ObjectStreamException JavaDoc
192     {
193         if( null == m_category )
194         {
195             m_category = "";
196         }
197         if( null == m_message )
198         {
199             m_message = "";
200         }
201
202         String JavaDoc priorityName = "";
203         if( null != m_priority )
204         {
205             priorityName = m_priority.getName();
206         }
207
208         m_priority = Priority.getPriorityForName( priorityName );
209
210         return this;
211     }
212 }
213
Popular Tags