View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.spi;
25  
26  import java.util.Hashtable;
27  
28  import org.apache.log4j.spi.LoggingEvent;
29  import org.apache.log4j.spi.ThrowableInformation;
30  import org.apache.log4j.Logger;
31  import org.apache.log4j.Level;
32  
33  /**
34  *
35  * The <code>LogEvent</code> class is a derivative of the Apache log4j LoggingEvent class.
36  * Provides constructors to support creating LoggingEvent instances from log4j-like
37  * log files.
38  *
39  * @author  Regunath B
40  * @version 1.0, 04/11/26
41  * @see     org.apache.log4j.spi.LoggingEvent
42  */
43  public class LogEvent extends LoggingEvent {
44  	
45  	/**
46  	 * Used for object serialization
47  	 */
48  	private static final long serialVersionUID = -7395745277683854531L;
49  	
50  	/*
51  	 * Useful constants for various default values for LoggingEvent creation
52  	 */
53  	private static final String DEFAULT_STRING = "<no info available>";
54  	private static final String DEFAULT_CATEGORY_CLASS_NAME = DEFAULT_STRING;
55  	private static final ThrowableInformation DEFAULT_THROWABLE_INFO = 
56  		new ThrowableInformation( new String[] {}
57  			);
58  	private static final Logger DEFAULT_LOGGER = Logger.getLogger(DEFAULT_STRING);
59  	private static final Level DEFAULT_LEVEL = Level.INFO;
60  	
61  	/*
62  	 * Variable that stores namespace information for this LogEvent. 
63  	 */
64  	private LogNamespace namespace;
65  	
66  	/*
67  	 * Variable that stores the Exception name, if any, for this LogEvent 
68  	 */
69  	private String exceptionName;
70  	
71  	/*
72  	 * Variable that holds the relative time
73  	 */
74  	private long relativeTime;	
75  	
76  	/**
77  	 * No args constructor for this class
78  	 */
79  	public LogEvent() {
80  		super();
81  	}
82  	
83  	/**
84  	 * Constructor for this class.
85  	 * @param message non-null message string for this LogEvent
86  	 */
87  	public LogEvent(Object message) {
88  		super(DEFAULT_CATEGORY_CLASS_NAME,DEFAULT_LOGGER,DEFAULT_LEVEL,message,null);
89  		super.setThrowableInformation(DEFAULT_THROWABLE_INFO);
90  		super.setThreadName(DEFAULT_STRING);
91  	}
92  	
93  	/**
94  	 * Constructor for this class. Creates an instance of this class from a Apache log4j LoggingEvent instance
95  	 * @param event the LoggingEvent instance to use for creating an instance of this class
96  	 */
97  	public LogEvent(LoggingEvent event) {
98  		super(event.getLoggerName(),event.getLogger(),event.getLevel(),event.getMessage(),null);
99  	    super.setTimeStamp(event.getTimeStamp());
100 	    super.setThreadName(event.getThreadName());
101 	    super.setLocationInformation(event.getLocationInformation());
102 	    super.setNDC(event.getNDC());
103 	    super.setProperties(new Hashtable(event.getProperties()));		
104 		super.setThrowableInformation(event.getThrowableInformation());
105 	}
106 	
107 	/**
108 	 * Returns the Throwable information contained in this LogEvent as a String array
109 	 * @return String[] that contains the stack trace of the Throwable
110 	 */
111 	public String[] getThrowableStrRepresentation() {
112 		return super.getThrowableInformation().getThrowableStrRep();
113 	}
114 	
115 	/**
116 	 * @return Returns the namespace.
117 	 */
118 	public LogNamespace getNamespace() {
119 		return this.namespace;
120 	}
121 	/**
122 	 * Sets the namespace information for this LogEvent
123 	 * @param namespace The namespace to set.
124 	 */
125 	public void setNamespace(LogNamespace namespace) {
126 		this.namespace = namespace;
127 	}
128 	/**
129 	 * @return Returns the exceptionName.
130 	 */
131 	public String getExceptionName() {
132 		return exceptionName;
133 	}
134 	/**
135 	 * @param exceptionName The exceptionName to set.
136 	 */
137 	public void setExceptionName(String exceptionName) {
138 		this.exceptionName = exceptionName;
139 	}
140 	/**
141 	 * @return Returns the relativeTime.
142 	 */
143 	public long getRelativeTime() {
144 		return this.relativeTime;
145 	}
146 	/**
147 	 * @param relativeTime The relativeTime to set.
148 	 */
149 	public void setRelativeTime(long relativeTime) {
150 		this.relativeTime = relativeTime;
151 	}
152 }