KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > LogRecord


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

16 package org.apache.log4j.lf5;
17
18 import java.io.IOException JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 /**
23  * LogRecord. A LogRecord encapsulates the details of your desired log
24  * request.
25  *
26  * @author Michael J. Sikorsky
27  * @author Robert Shaw
28  */

29
30 // Contributed by ThoughtWorks Inc.
31

32 public abstract class LogRecord implements java.io.Serializable JavaDoc {
33   //--------------------------------------------------------------------------
34
// Constants:
35
//--------------------------------------------------------------------------
36

37   //--------------------------------------------------------------------------
38
// Protected Variables:
39
//--------------------------------------------------------------------------
40
protected static long _seqCount = 0;
41
42   protected LogLevel _level;
43   protected String JavaDoc _message;
44   protected long _sequenceNumber;
45   protected long _millis;
46   protected String JavaDoc _category;
47   protected String JavaDoc _thread;
48   protected String JavaDoc _thrownStackTrace;
49   protected Throwable JavaDoc _thrown;
50   protected String JavaDoc _ndc;
51   protected String JavaDoc _location;
52
53   //--------------------------------------------------------------------------
54
// Private Variables:
55
//--------------------------------------------------------------------------
56

57   //--------------------------------------------------------------------------
58
// Constructors:
59
//--------------------------------------------------------------------------
60

61   public LogRecord() {
62     super();
63
64     _millis = System.currentTimeMillis();
65     _category = "Debug";
66     _message = "";
67     _level = LogLevel.INFO;
68     _sequenceNumber = getNextId();
69     _thread = Thread.currentThread().toString();
70     _ndc = "";
71     _location = "";
72   }
73
74   //--------------------------------------------------------------------------
75
// Public Methods:
76
//--------------------------------------------------------------------------
77

78   /**
79    * Get the level of this LogRecord.
80    *
81    * @return The LogLevel of this record.
82    * @see #setLevel(LogLevel)
83    * @see LogLevel
84    */

85   public LogLevel getLevel() {
86     return (_level);
87   }
88
89   /**
90    * Set the level of this LogRecord.
91    *
92    * @param level The LogLevel for this record.
93    * @see #getLevel()
94    * @see LogLevel
95    */

96   public void setLevel(LogLevel level) {
97     _level = level;
98   }
99
100   /**
101    * Abstract method. Must be overridden to indicate what log level
102    * to show in red.
103    */

104   public abstract boolean isSevereLevel();
105
106   /**
107    * @return true if getThrown().toString() is a non-empty string.
108    */

109   public boolean hasThrown() {
110     Throwable JavaDoc thrown = getThrown();
111     if (thrown == null) {
112       return false;
113     }
114     String JavaDoc thrownString = thrown.toString();
115     return thrownString != null && thrownString.trim().length() != 0;
116   }
117
118   /**
119    * @return true if isSevereLevel() or hasThrown() returns true.
120    */

121   public boolean isFatal() {
122     return isSevereLevel() || hasThrown();
123   }
124
125   /**
126    * Get the category asscociated with this LogRecord. For a more detailed
127    * description of what a category is see setCategory().
128    *
129    * @return The category of this record.
130    * @see #setCategory(String)
131    */

132   public String JavaDoc getCategory() {
133     return (_category);
134   }
135
136   /**
137    * Set the category associated with this LogRecord. A category represents
138    * a hierarchical dot (".") separated namespace for messages.
139    * The definition of a category is application specific, but a common convention
140    * is as follows:
141    *
142    * <p>
143    * When logging messages
144    * for a particluar class you can use its class name:
145    * com.thoughtworks.framework.servlet.ServletServiceBroker.<br><br>
146    * Futhermore, to log a message for a particular method in a class
147    * add the method name:
148    * com.thoughtworks.framework.servlet.ServletServiceBroker.init().
149    * </p>
150    *
151    * @param category The category for this record.
152    * @see #getCategory()
153    */

154   public void setCategory(String JavaDoc category) {
155     _category = category;
156   }
157
158   /**
159    * Get the message asscociated with this LogRecord.
160    *
161    * @return The message of this record.
162    * @see #setMessage(String)
163    */

164   public String JavaDoc getMessage() {
165     return (_message);
166   }
167
168   /**
169    * Set the message associated with this LogRecord.
170    *
171    * @param message The message for this record.
172    * @see #getMessage()
173    */

174   public void setMessage(String JavaDoc message) {
175     _message = message;
176   }
177
178   /**
179    * Get the sequence number associated with this LogRecord. Sequence numbers
180    * are generally assigned when a LogRecord is constructed. Sequence numbers
181    * start at 0 and increase with each newly constructed LogRocord.
182    *
183    * @return The sequence number of this record.
184    * @see #setSequenceNumber(long)
185    */

186   public long getSequenceNumber() {
187     return (_sequenceNumber);
188   }
189
190   /**
191    * Set the sequence number assocsiated with this LogRecord. A sequence number
192    * will automatically be assigned to evey newly constructed LogRecord, however,
193    * this method can override the value.
194    *
195    * @param number The sequence number.
196    * @see #getSequenceNumber()
197    */

198   public void setSequenceNumber(long number) {
199     _sequenceNumber = number;
200   }
201
202   /**
203    * Get the event time of this record in milliseconds from 1970.
204    * When a LogRecord is constructed the event time is set but may be
205    * overridden by calling setMillis();
206    *
207    * @return The event time of this record in milliseconds from 1970.
208    * @see #setMillis(long)
209    */

210   public long getMillis() {
211     return _millis;
212   }
213
214   /**
215    * Set the event time of this record. When a LogRecord is constructed
216    * the event time is set but may be overridden by calling this method.
217    *
218    * @param millis The time in milliseconds from 1970.
219    * @see #getMillis()
220    */

221   public void setMillis(long millis) {
222     _millis = millis;
223   }
224
225   /**
226    * Get the thread description asscociated with this LogRecord. When a
227    * LogRecord is constructed, the thread description is set by calling:
228    * Thread.currentThread().toString(). You may supply a thread description
229    * of your own by calling the setThreadDescription(String) method.
230    *
231    * @return The thread description of this record.
232    * @see #setThreadDescription(String)
233    */

234   public String JavaDoc getThreadDescription() {
235     return (_thread);
236   }
237
238   /**
239    * Set the thread description associated with this LogRecord. When a
240    * LogRecord is constructed, the thread description is set by calling:
241    * Thread.currentThread().toString(). You may supply a thread description
242    * of your own by calling this method.
243    *
244    * @param threadDescription The description of the thread for this record.
245    * @see #getThreadDescription()
246    */

247   public void setThreadDescription(String JavaDoc threadDescription) {
248     _thread = threadDescription;
249   }
250
251   /**
252    * Get the stack trace in a String-based format for the associated Throwable
253    * of this LogRecord. The stack trace in a String-based format is set
254    * when the setThrown(Throwable) method is called.
255    *
256    * <p>
257    * Why do we need this method considering that we
258    * have the getThrown() and setThrown() methods?
259    * A Throwable object may not be serializable, however, a String representation
260    * of it is. Users of LogRecords should generally call this method over
261    * getThrown() for the reasons of serialization.
262    * </p>
263    *
264    * @return The Stack Trace for the asscoiated Throwable of this LogRecord.
265    * @see #setThrown(Throwable)
266    * @see #getThrown()
267    */

268   public String JavaDoc getThrownStackTrace() {
269     return (_thrownStackTrace);
270   }
271
272   /**
273    * Set the ThrownStackTrace for the log record.
274    *
275    * @param trace A String to associate with this LogRecord
276    * @see #getThrownStackTrace()
277    */

278   public void setThrownStackTrace(String JavaDoc trace) {
279     _thrownStackTrace = trace;
280   }
281
282   /**
283    * Get the Throwable associated with this LogRecord.
284    *
285    * @return The LogLevel of this record.
286    * @see #setThrown(Throwable)
287    * @see #getThrownStackTrace()
288    */

289   public Throwable JavaDoc getThrown() {
290     return (_thrown);
291   }
292
293   /**
294    * Set the Throwable associated with this LogRecord. When this method
295    * is called, the stack trace in a String-based format is made
296    * available via the getThrownStackTrace() method.
297    *
298    * @param thrown A Throwable to associate with this LogRecord.
299    * @see #getThrown()
300    * @see #getThrownStackTrace()
301    */

302   public void setThrown(Throwable JavaDoc thrown) {
303     if (thrown == null) {
304       return;
305     }
306     _thrown = thrown;
307     StringWriter JavaDoc sw = new StringWriter JavaDoc();
308     PrintWriter JavaDoc out = new PrintWriter JavaDoc(sw);
309     thrown.printStackTrace(out);
310     out.flush();
311     _thrownStackTrace = sw.toString();
312     try {
313       out.close();
314       sw.close();
315     } catch (IOException JavaDoc e) {
316       // Do nothing, this should not happen as it is StringWriter.
317
}
318     out = null;
319     sw = null;
320   }
321
322   /**
323    * Return a String representation of this LogRecord.
324    */

325   public String JavaDoc toString() {
326     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
327     buf.append("LogRecord: [" + _level + ", " + _message + "]");
328     return (buf.toString());
329   }
330
331   /**
332    * Get the NDC (nested diagnostic context) for this record.
333    *
334    * @return The string representing the NDC.
335    */

336   public String JavaDoc getNDC() {
337     return _ndc;
338   }
339
340   /**
341    * Set the NDC (nested diagnostic context) for this record.
342    *
343    * @param ndc A string representing the NDC.
344    */

345   public void setNDC(String JavaDoc ndc) {
346     _ndc = ndc;
347   }
348
349   /**
350    * Get the location in code where this LogRecord originated.
351    *
352    * @return The string containing the location information.
353    */

354   public String JavaDoc getLocation() {
355     return _location;
356   }
357
358   /**
359    * Set the location in code where this LogRecord originated.
360    *
361    * @param location A string containing location information.
362    */

363   public void setLocation(String JavaDoc location) {
364     _location = location;
365   }
366
367   /**
368    * Resets that sequence number to 0.
369    *
370    */

371   public static synchronized void resetSequenceNumber() {
372     _seqCount = 0;
373   }
374
375   //--------------------------------------------------------------------------
376
// Protected Methods:
377
//--------------------------------------------------------------------------
378

379   protected static synchronized long getNextId() {
380     _seqCount++;
381     return _seqCount;
382   }
383   //--------------------------------------------------------------------------
384
// Private Methods:
385
//--------------------------------------------------------------------------
386

387   //--------------------------------------------------------------------------
388
// Nested Top-Level Classes or Interfaces:
389
//--------------------------------------------------------------------------
390

391 }
392
393
394
395
Popular Tags