View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.spi;
15  
16  import java.io.IOException;
17  import java.io.ObjectInputStream;
18  import java.io.ObjectOutputStream;
19  import java.io.Serializable;
20  import java.util.Map;
21  
22  import org.slf4j.Marker;
23  import org.slf4j.helpers.MessageFormatter;
24  
25  import ch.qos.logback.classic.Level;
26  
27  /**
28   * A read/write and serializable implementation of {@link ILoggingEvent}.
29   * 
30   * @author Ceki Gülcü
31   */
32  public class PubLoggingEventVO implements ILoggingEvent, Serializable {
33  
34      private static final long serialVersionUID = -3385765861078946218L;
35  
36      private static final int NULL_ARGUMENT_ARRAY = -1;
37      private static final String NULL_ARGUMENT_ARRAY_ELEMENT = "NULL_ARGUMENT_ARRAY_ELEMENT";
38  
39      public String threadName;
40      public String loggerName;
41      public LoggerContextVO loggerContextVO;
42  
43      public transient Level level;
44      public String message;
45  
46      private transient String formattedMessage;
47  
48      public Object[] argumentArray;
49  
50      public IThrowableProxy throwableProxy;
51      public StackTraceElement[] callerDataArray;
52      public Marker marker;
53      public Map<String, String> mdcPropertyMap;
54      public long timeStamp;
55      public long sequenceNumber;
56  
57      public String getThreadName() {
58          return threadName;
59      }
60  
61      public LoggerContextVO getLoggerContextVO() {
62          return loggerContextVO;
63      }
64  
65      public String getLoggerName() {
66          return loggerName;
67      }
68  
69      public Level getLevel() {
70          return level;
71      }
72  
73      public String getMessage() {
74          return message;
75      }
76  
77      public String getFormattedMessage() {
78          if (formattedMessage != null) {
79              return formattedMessage;
80          }
81  
82          if (argumentArray != null) {
83              formattedMessage = MessageFormatter.arrayFormat(message, argumentArray).getMessage();
84          } else {
85              formattedMessage = message;
86          }
87  
88          return formattedMessage;
89      }
90  
91      public Object[] getArgumentArray() {
92          return argumentArray;
93      }
94  
95      public IThrowableProxy getThrowableProxy() {
96          return throwableProxy;
97      }
98  
99      public StackTraceElement[] getCallerData() {
100         return callerDataArray;
101     }
102 
103     public boolean hasCallerData() {
104         return callerDataArray != null;
105     }
106 
107     public Marker getMarker() {
108         return marker;
109     }
110 
111     public long getTimeStamp() {
112         return timeStamp;
113     }
114 
115     public long getSequenceNumber() {
116         return sequenceNumber;
117     }
118 
119     public void setSequenceNumber(long sequenceNumber) {
120         this.sequenceNumber = sequenceNumber;
121     }
122     
123     public long getContextBirthTime() {
124         return loggerContextVO.getBirthTime();
125     }
126 
127     public LoggerContextVO getContextLoggerRemoteView() {
128         return loggerContextVO;
129     }
130 
131     public Map<String, String> getMDCPropertyMap() {
132         return mdcPropertyMap;
133     }
134 
135     public Map<String, String> getMdc() {
136         return mdcPropertyMap;
137     }
138 
139     public void prepareForDeferredProcessing() {
140     }
141 
142     private void writeObject(ObjectOutputStream out) throws IOException {
143         out.defaultWriteObject();
144         out.writeInt(level.levelInt);
145         if (argumentArray != null) {
146             int len = argumentArray.length;
147             out.writeInt(len);
148             for (int i = 0; i < argumentArray.length; i++) {
149                 if (argumentArray[i] != null) {
150                     out.writeObject(argumentArray[i].toString());
151                 } else {
152                     out.writeObject(NULL_ARGUMENT_ARRAY_ELEMENT);
153                 }
154             }
155         } else {
156             out.writeInt(NULL_ARGUMENT_ARRAY);
157         }
158 
159     }
160 
161     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
162         in.defaultReadObject();
163         int levelInt = in.readInt();
164         level = Level.toLevel(levelInt);
165 
166         int argArrayLen = in.readInt();
167         if (argArrayLen != NULL_ARGUMENT_ARRAY) {
168             argumentArray = new String[argArrayLen];
169             for (int i = 0; i < argArrayLen; i++) {
170                 Object val = in.readObject();
171                 if (!NULL_ARGUMENT_ARRAY_ELEMENT.equals(val)) {
172                     argumentArray[i] = val;
173                 }
174             }
175         }
176     }
177 
178     @Override
179     public int hashCode() {
180         final int prime = 31;
181         int result = 1;
182         result = prime * result + ((message == null) ? 0 : message.hashCode());
183         result = prime * result + ((threadName == null) ? 0 : threadName.hashCode());
184         result = prime * result + (int) (timeStamp ^ (timeStamp >>> 32));
185         return result;
186     }
187 
188     @Override
189     public boolean equals(Object obj) {
190         if (this == obj)
191             return true;
192         if (obj == null)
193             return false;
194         if (getClass() != obj.getClass())
195             return false;
196         final PubLoggingEventVO other = (PubLoggingEventVO) obj;
197         if (message == null) {
198             if (other.message != null)
199                 return false;
200         } else if (!message.equals(other.message))
201             return false;
202 
203         if (loggerName == null) {
204             if (other.loggerName != null)
205                 return false;
206         } else if (!loggerName.equals(other.loggerName))
207             return false;
208 
209         if (threadName == null) {
210             if (other.threadName != null)
211                 return false;
212         } else if (!threadName.equals(other.threadName))
213             return false;
214         if (timeStamp != other.timeStamp)
215             return false;
216 
217         if (marker == null) {
218             if (other.marker != null)
219                 return false;
220         } else if (!marker.equals(other.marker))
221             return false;
222 
223         if (mdcPropertyMap == null) {
224             if (other.mdcPropertyMap != null)
225                 return false;
226         } else if (!mdcPropertyMap.equals(other.mdcPropertyMap))
227             return false;
228         return true;
229     }
230 
231     public String toString() {
232         StringBuilder sb = new StringBuilder();
233         sb.append(timeStamp);
234         sb.append(" ");
235         sb.append(level);
236         sb.append(" [");
237         sb.append(threadName);
238         sb.append("] ");
239         sb.append(loggerName);
240         sb.append(" - ");
241         sb.append(getFormattedMessage());
242         return sb.toString();
243     }
244 
245 }