001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package org.jivesoftware.util.log;
010:
011: import java.io.ObjectStreamException;
012: import java.io.Serializable;
013:
014: /**
015: * This class encapsulates each individual log event.
016: * LogEvents usually originate at a Logger and are routed
017: * to LogTargets.
018: *
019: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
020: */
021: public final class LogEvent implements Serializable {
022: //A Constant used when retrieving time relative to start of applicaiton start
023: private final static long START_TIME = System.currentTimeMillis();
024:
025: ///The category that this LogEvent concerns. (Must not be null)
026: private String m_category;
027:
028: ///The message to be logged. (Must not be null)
029: private String m_message;
030:
031: ///The exception that caused LogEvent if any. (May be null)
032: private Throwable m_throwable;
033:
034: ///The time in millis that LogEvent occurred
035: private long m_time;
036:
037: ///The priority of LogEvent. (Must not be null)
038: private Priority m_priority;
039:
040: ///The context map associated with LogEvent. (May be null).
041: private ContextMap m_contextMap;
042:
043: /**
044: * Get Priority for LogEvent.
045: *
046: * @return the LogEvent Priority
047: */
048: public final Priority getPriority() {
049: return m_priority;
050: }
051:
052: /**
053: * Set the priority of LogEvent.
054: *
055: * @param priority the new LogEvent priority
056: */
057: public final void setPriority(final Priority priority) {
058: m_priority = priority;
059: }
060:
061: /**
062: * Get ContextMap associated with LogEvent
063: *
064: * @return the ContextMap
065: */
066: public final ContextMap getContextMap() {
067: return m_contextMap;
068: }
069:
070: /**
071: * Set the ContextMap for this LogEvent.
072: *
073: * @param contextMap the context map
074: */
075: public final void setContextMap(final ContextMap contextMap) {
076: m_contextMap = contextMap;
077: }
078:
079: // /**
080: // * Get ContextStack associated with LogEvent
081: // *
082: // * @return the ContextStack
083: // * @deprecated ContextStack has been deprecated and thus so has this method
084: // */
085: // public final ContextStack getContextStack()
086: // {
087: // return m_contextStack;
088: // }
089:
090: // /**
091: // * Set the ContextStack for this LogEvent.
092: // * Note that if this LogEvent ever changes threads, the
093: // * ContextStack must be cloned.
094: // *
095: // * @param contextStack the context stack
096: // * @deprecated ContextStack has been deprecated and thus so has this method
097: // */
098: // public final void setContextStack( final ContextStack contextStack )
099: // {
100: // m_contextStack = contextStack;
101: // }
102:
103: /**
104: * Get the category that LogEvent relates to.
105: *
106: * @return the name of category
107: */
108: public final String getCategory() {
109: return m_category;
110: }
111:
112: /**
113: * Get the message associated with event.
114: *
115: * @return the message
116: */
117: public final String getMessage() {
118: return m_message;
119: }
120:
121: /**
122: * Get throwabe instance associated with event.
123: *
124: * @return the Throwable
125: */
126: public final Throwable getThrowable() {
127: return m_throwable;
128: }
129:
130: /**
131: * Get the absolute time of the log event.
132: *
133: * @return the absolute time
134: */
135: public final long getTime() {
136: return m_time;
137: }
138:
139: /**
140: * Get the time of the log event relative to start of application.
141: *
142: * @return the time
143: */
144: public final long getRelativeTime() {
145: return m_time - START_TIME;
146: }
147:
148: /**
149: * Set the LogEvent category.
150: *
151: * @param category the category
152: */
153: public final void setCategory(final String category) {
154: m_category = category;
155: }
156:
157: /**
158: * Set the message for LogEvent.
159: *
160: * @param message the message
161: */
162: public final void setMessage(final String message) {
163: m_message = message;
164: }
165:
166: /**
167: * Set the throwable for LogEvent.
168: *
169: * @param throwable the instance of Throwable
170: */
171: public final void setThrowable(final Throwable throwable) {
172: m_throwable = throwable;
173: }
174:
175: /**
176: * Set the absolute time of LogEvent.
177: *
178: * @param time the time
179: */
180: public final void setTime(final long time) {
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: * @throws ObjectStreamException if an error occurs
189: */
190: private Object readResolve() throws ObjectStreamException {
191: if (null == m_category)
192: m_category = "";
193: if (null == m_message)
194: m_message = "";
195:
196: String priorityName = "";
197: if (null != m_priority) {
198: priorityName = m_priority.getName();
199: }
200:
201: m_priority = Priority.getPriorityForName(priorityName);
202:
203: return this;
204: }
205: }
|