root/trunk/src/java/org/jcoderz/commons/logging/LogItem.java

Revision 1299, 13.5 kB (checked in by amandel, 3 years ago)
  • Log formatter ignores the category field but now outputs the last 9 characters of the thread name. In case of a ordinary log record the the name of the logger thread is used. This might be wrong in certain setups but is correct in many cases. The fawkeZ loggable records the thread name of the thread that created the loggable instance which is the correct thread name in all cases. #58
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.logging;
34
35import java.util.ArrayList;
36import java.util.Collections;
37import java.util.HashMap;
38import java.util.List;
39import java.util.Map;
40import java.util.Set;
41import java.util.logging.Level;
42
43import org.jcoderz.commons.BusinessImpact;
44import org.jcoderz.commons.Category;
45import org.jcoderz.commons.types.Date;
46import org.jcoderz.commons.util.Assert;
47import org.jcoderz.commons.util.Constants;
48
49
50
51/**
52 * This class is the base class of nested log information as it is used for
53 * displaying or transferring log messages.
54 *
55 */
56public abstract class LogItem
57{
58   /** This is a prefix of an internal parameter of a log message, i.e. a
59    *  parameter, which is not defined as parameter in the xml file. */
60   public static final String INTERNAL_PARAMETER_PREFIX = "_";
61
62   private LogItem mNestedEntry = null;
63   private LogItem mParentEntry = null;
64
65   private Date mTimestamp = null;
66   private Level mLogLevel = null;
67   private long mThreadId;
68   private BusinessImpact mBusinessImpact = null;
69   private Category mCategory = null;
70   private String mNodeId = null;
71   private String mInstanceId = null;
72   private String mTrackingNumber = null;
73   private String mSymbol = null;
74   private String mSymbolId = null;
75   private String mMessage = null;
76   private String mSourceClass = null;
77   private String mSourceMethod = null;
78   private String mSolution = null;
79   private String mThreadName = null;
80   private StringBuffer mMessageBuffer = null;
81
82   private final Map mParameters = new HashMap();
83
84   private final List mStackTraceLines = new ArrayList();
85
86   private String mType = null;
87
88   /**
89    * Sets the business impact of this.
90    *
91    * @param businessImpact The business impact to set.
92    */
93   public void setBusinessImpact (BusinessImpact businessImpact)
94   {
95      mBusinessImpact = businessImpact;
96   }
97
98   /**
99    * Sets the category of this.
100    *
101    * @param category The category to set.
102    */
103   public void setCategory (Category category)
104   {
105      mCategory = category;
106   }
107
108   /**
109    * Sets the instance id.
110    *
111    * @param instanceId The instance id to set.
112    */
113   public void setInstanceId (String instanceId)
114   {
115      mInstanceId = instanceId;
116   }
117
118   /**
119    * Sets the log level of this.
120    *
121    * @param logLevel The log level, which had been used for logging the
122    * message.
123    */
124   public void setLoggerLevel (Level logLevel)
125   {
126      mLogLevel = logLevel;
127   }
128
129   /**
130    * Sets the message text.
131    *
132    * @param message The text of the logged message.
133    */
134   public void setMessage (String message)
135   {
136      mMessageBuffer = new StringBuffer(String.valueOf(message));
137      mMessage = null;
138   }
139
140   /**
141    * Appends a new line with supplied message text to the already stored
142    * message. If no message has been stored yet, the supplied string is set
143    * as message.
144    *
145    * @param messageLine The new line of message.
146    */
147   public void appendMessageLine (String messageLine)
148   {
149      mMessage = null;
150      if (mMessageBuffer == null)
151      {
152         mMessageBuffer = new StringBuffer(String.valueOf(messageLine));
153      }
154      else
155      {
156         mMessageBuffer.append(Constants.LINE_SEPARATOR).append(
157               String.valueOf(messageLine));
158      }
159   }
160
161   /**
162    * Sets the node id.
163    *
164    * @param nodeId The node id to set.
165    */
166   public void setNodeId (String nodeId)
167   {
168      mNodeId = nodeId;
169   }
170
171   /**
172    * Sets the possible solution to resolve the error indicated by the logged
173    * message.
174    *
175    * @param solution The solution for the logged message.
176    */
177   public void setSolution (String solution)
178   {
179      mSolution = solution;
180   }
181
182   /**
183    * Sets the class name of the source location where the message was logged.
184    *
185    * @param sourceClass The name of the class where the message was logged,
186    */
187   public void setSourceClass (String sourceClass)
188   {
189      mSourceClass = sourceClass;
190   }
191
192   /**
193    * Sets the method name of the source location where the message was logged.
194    *
195    * @param sourceMethod The name of the method where the message was logged,
196    */
197   public void setSourceMethod (String sourceMethod)
198   {
199      mSourceMethod = sourceMethod;
200   }
201
202   /**
203    * Sets the message symbol.
204    *
205    * @param symbol The symbol for the logged message.
206    */
207   public void setSymbol (final String symbol)
208   {
209      mSymbol = symbol;
210   }
211
212   /**
213    * Sets the message symbol code.
214    *
215    * @param symbolId The id/code for the symbol of the logged message.
216    */
217   public void setSymbolId (final String symbolId)
218   {
219      mSymbolId = symbolId;
220   }
221
222   /**
223    * Sets the thread id.
224    *
225    * @param threadId The thread id to set.
226    */
227   public void setThreadId (long threadId)
228   {
229      mThreadId = threadId;
230   }
231
232   /**
233    * Sets the timestamp of the message.
234    *
235    * @param timestamp The timestamp of when the message was logged.
236    */
237   public void setTimestamp (final Date timestamp)
238   {
239      mTimestamp = timestamp;
240   }
241
242   /**
243    * Sets the tracking number from the parsed log line.
244    *
245    * @param trackingNumber The tracking number of the logged message.
246    */
247   public void setTrackingNumber (final String trackingNumber)
248   {
249      mTrackingNumber = trackingNumber;
250   }
251
252   /**
253    * Sets the type of this.
254    *
255    * @param type The type to set.
256    */
257   public void setType (final String type)
258   {
259      mType = type;
260   }
261
262   /**
263    * Sets the thread name for this LogItem.
264    *
265    * @param type The thread name to set..
266    */
267   public void setThreadName (final String threadName)
268   {
269      mThreadName = threadName;
270   }
271
272   /**
273    * Gets the business impact.
274    *
275    * @return BusinessImpact of the message.
276    */
277   public BusinessImpact getBusinessImpact ()
278   {
279      return mBusinessImpact;
280   }
281
282   /**
283    * Gets the category.
284    *
285    * @return Category of the message.
286    */
287   public Category getCategory ()
288   {
289      return mCategory;
290   }
291
292   /**
293    * Gets the id of the instance which logged this.
294    *
295    * @return Id of instance, which logged this.
296    */
297   public String getInstanceId ()
298   {
299      return mInstanceId;
300   }
301
302   /**
303    * Gets the logger level of this entry.
304    *
305    * @return log level used to log this.
306    */
307   public Level getLoggerLevel ()
308   {
309      return mLogLevel;
310   }
311
312   /**
313    * Gets the message of this entry.
314    *
315    * @return Message text.
316    */
317   public String getMessage ()
318   {
319      if (mMessage == null)
320      {
321         mMessage = (mMessageBuffer == null) ? null : mMessageBuffer.toString();
322      }
323      return mMessage;
324   }
325
326   /**
327    * Gets the id of the node which logged this.
328    *
329    * @return Id of the node qwhich logged this.
330    */
331   public String getNodeId ()
332   {
333      return mNodeId;
334   }
335
336   /**
337    * Gets the parameter names of the parameters for this as unmodifiable Set.
338    *
339    * @return Set with parameter names, migth be empty, never null.
340    */
341
342   public Set getParameterNames ()
343   {
344      return Collections.unmodifiableSet(mParameters.keySet());
345   }
346
347   /**
348    * Gets the List of parameter values for the specified parameter name as
349    * unmodifiable List.
350    *
351    * @param parameterName The name of the parameter for which to return the
352    * values.
353    *
354    * @return List of parameter values, is null if <code>parameterName</code> is
355    * unknown.
356    */
357
358   public List getParameterValues (final String parameterName)
359   {
360      final List parameterValues = (List) mParameters.get(parameterName);
361      final List rc = (parameterValues == null)
362            ? null : Collections.unmodifiableList(parameterValues);
363      return rc;
364   }
365
366   /**
367    * Gets a possible solution.
368    *
369    * @return The solution for an error message;
370    */
371   public String getSolution ()
372   {
373      return mSolution;
374   }
375
376   /**
377    * Gets the name of the class where the message was logged.
378    *
379    * @return Name of the source class.
380    */
381   public String getSourceClass ()
382   {
383      return mSourceClass;
384   }
385
386   /**
387    * Gets the name of the method where this was logged.
388    *
389    * @return Name of the source method.
390    */
391   public String getSourceMethod ()
392   {
393      return mSourceMethod;
394   }
395
396   /**
397    * Gets the list of stack trace linesstored by this.
398    *
399    * @return List with stack trace lines, might be empty, never null.
400    */
401   public List getStackTraceLines ()
402   {
403      return mStackTraceLines;
404   }
405
406   /**
407    * Gets the message symbol.
408    *
409    * @return The message symbol.
410    */
411   public String getSymbol ()
412   {
413      return mSymbol;
414   }
415
416   /**
417    * Gets the id of the message symbol in hex representation.
418    *
419    * @return The symbol id in hex representation
420    */
421   public String getSymbolId ()
422   {
423      return mSymbolId;
424   }
425
426   /**
427    * Gets the id of the thread which logged this.
428    *
429    * @return Id of thread whcih logged this.
430    */
431   public long getThreadId ()
432   {
433      return mThreadId;
434   }
435
436   /**
437    * Gets the timestamp of when this was logged.
438    *
439    * @return Timestamp when this was logged.
440    */
441   public Date getTimestamp ()
442   {
443      return mTimestamp;
444   }
445
446   /**
447    * Gets the tracking number of this.
448    *
449    * @return Tracking number of this.
450    */
451   public String getTrackingNumber ()
452   {
453      return mTrackingNumber;
454   }
455
456   /**
457    * Returns the type of this, which is retrieved from the log line type.
458    *
459    * @return Type of this.
460    */
461   public String getType ()
462   {
463      return mType;
464   }
465
466   /**
467    * Gets the thread name for this LogItem.
468    *
469    * @return The thread name of this log item.
470    */
471   public String getThreadName ()
472   {
473      return mThreadName;
474   }
475
476   /**
477    * Gets information whether this item stores an exception + stacktrace +
478    * cause description only. Even if this is the case, there might be a nested
479    * item storing a log message.
480    *
481    * @return true, if this item only stores an exception; false, else.
482    */
483   public boolean isExceptionItem ()
484   {
485      // for all types of log messages the mType is set. So if it is not set,
486      // this entry only stores exception values.
487      return (mType == null);
488   }
489
490   /**
491    * Gets the item nested to this.
492    *
493    * @return nested log item, might be null if no such.
494    */
495   public LogItem getNestedItem ()
496   {
497      return mNestedEntry;
498   }
499
500   /**
501    * Sets the supplied item as nested item for this and this as parent for
502    * the supplied item.
503    *
504    * @param nestedItem The LogItem to set as nested item for this.
505    */
506   public void setNestedItem (LogItem nestedItem)
507   {
508      Assert.notNull(nestedItem, "nestedItem");
509      mNestedEntry = nestedItem;
510      nestedItem.setParentItem(this);
511   }
512
513   /**
514    * Sets the log item which is parent to this.
515    *
516    * @param parentEntry The parentEntry to set.
517    */
518   public void setParentItem (LogItem parentEntry)
519   {
520      mParentEntry = parentEntry;
521   }
522
523   /**
524    * Gets the log item which is parent to this.
525    *
526    * @return The parent item of this, might be null.
527    */
528   public LogItem getParentItem ()
529   {
530      return mParentEntry;
531   }
532
533   /**
534    * Adds a parameter with value to the stored parameters.
535    *
536    * @param name The name of the parameter.
537    * @param value The value of the parameter with this name.
538    */
539   public void addToParameters (final String name, final Object value)
540   {
541      mParameters.put(name, value);
542   }
543
544   /**
545    * Used for resetting this to an initial state so that this object could be
546    * reused again.
547    *
548    * @throws LoggingException if an error occurs.
549    */
550   public void reset ()
551         throws LoggingException
552   {
553      mNestedEntry = null;
554      mParentEntry = null;
555      mTimestamp = null;
556      mLogLevel = null;
557      mBusinessImpact = null;
558      mCategory = null;
559      mNodeId = null;
560      mInstanceId = null;
561      mTrackingNumber = null;
562      mSymbol = null;
563      mSymbolId = null;
564      mMessage = null;
565      mSourceClass = null;
566      mSourceMethod = null;
567      mSolution = null;
568      mParameters.clear();
569      mStackTraceLines.clear();
570      mParameters.clear();
571      mType = null;
572   }
573
574}
Note: See TracBrowser for help on using the browser.