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.List;
21  import java.util.Map;
22  
23  import com.fasterxml.jackson.annotation.JacksonInject;
24  import com.fasterxml.jackson.annotation.JsonAlias;
25  import com.fasterxml.jackson.annotation.JsonIgnore;
26  import com.fasterxml.jackson.databind.annotation.JsonValueInstantiator;
27  import org.slf4j.Marker;
28  import org.slf4j.event.KeyValuePair;
29  import org.slf4j.helpers.MessageFormatter;
30  
31  import ch.qos.logback.classic.Level;
32  
33  /**
34   * A read/write and serializable implementation of {@link ILoggingEvent}.
35   * 
36   * @author Ceki Gülcü
37   */
38  public class PubLoggingEventVO implements ILoggingEvent, Serializable {
39  
40      private static final long serialVersionUID = -3385765861078946218L;
41  
42      private static final int NULL_ARGUMENT_ARRAY = -1;
43      private static final String NULL_ARGUMENT_ARRAY_ELEMENT = "NULL_ARGUMENT_ARRAY_ELEMENT";
44  
45      public String threadName;
46      public String loggerName;
47      public PubLoggerContextVO loggerContextVO;
48  
49      public transient Level level;
50      public String message;
51  
52      private transient String formattedMessage;
53  
54      @JsonAlias
55      public Object[] argumentArray;
56  
57      public IThrowableProxy throwableProxy;
58      public StackTraceElement[] callerDataArray;
59      public List<Marker> markerList;
60      public List<KeyValuePair> kvpList;
61      public Map<String, String> mdcPropertyMap;
62      public long timeStamp;
63      public int nanoseconds;
64      public long sequenceNumber;
65  
66      public String getThreadName() {
67          return threadName;
68      }
69  
70      public LoggerContextVO getLoggerContextVO() {
71          return loggerContextVO;
72      }
73  
74      public String getLoggerName() {
75          return loggerName;
76      }
77  
78  
79      @JsonIgnore
80      public Level getLevel() {
81          return level;
82      }
83  
84      public String getMessage() {
85          return message;
86      }
87  
88      public String getFormattedMessage() {
89          if (formattedMessage != null) {
90              return formattedMessage;
91          }
92  
93          if (argumentArray != null) {
94              formattedMessage = MessageFormatter.arrayFormat(message, argumentArray).getMessage();
95          } else {
96              formattedMessage = message;
97          }
98  
99          return formattedMessage;
100     }
101 
102     public Object[] getArgumentArray() {
103         return argumentArray;
104     }
105 
106     public IThrowableProxy getThrowableProxy() {
107         return throwableProxy;
108     }
109 
110     public StackTraceElement[] getCallerData() {
111         return callerDataArray;
112     }
113 
114     public boolean hasCallerData() {
115         return callerDataArray != null;
116     }
117 
118     public List<Marker> getMarkerList() {
119         return markerList;
120     }
121 
122     public long getTimeStamp() {
123         return timeStamp;
124     }
125 
126     @Override
127     public int getNanoseconds() {
128         return nanoseconds;
129     }
130 
131     public long getSequenceNumber() {
132         return sequenceNumber;
133     }
134 
135     public void setSequenceNumber(long sequenceNumber) {
136         this.sequenceNumber = sequenceNumber;
137     }
138 
139     public long getContextBirthTime() {
140         return loggerContextVO.getBirthTime();
141     }
142 
143     public LoggerContextVO getContextLoggerRemoteView() {
144         return loggerContextVO;
145     }
146 
147     public Map<String, String> getMDCPropertyMap() {
148         return mdcPropertyMap;
149     }
150 
151     public Map<String, String> getMdc() {
152         return mdcPropertyMap;
153     }
154 
155     public void prepareForDeferredProcessing() {
156     }
157 
158     @Override
159     public List<KeyValuePair> getKeyValuePairs() {
160         return kvpList;
161     }
162 
163     private void writeObject(ObjectOutputStream out) throws IOException {
164         out.defaultWriteObject();
165         out.writeInt(level.levelInt);
166         if (argumentArray != null) {
167             int len = argumentArray.length;
168             out.writeInt(len);
169             for (int i = 0; i < argumentArray.length; i++) {
170                 if (argumentArray[i] != null) {
171                     out.writeObject(argumentArray[i].toString());
172                 } else {
173                     out.writeObject(NULL_ARGUMENT_ARRAY_ELEMENT);
174                 }
175             }
176         } else {
177             out.writeInt(NULL_ARGUMENT_ARRAY);
178         }
179 
180     }
181 
182     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
183         in.defaultReadObject();
184         int levelInt = in.readInt();
185         level = Level.toLevel(levelInt);
186 
187         int argArrayLen = in.readInt();
188         if (argArrayLen != NULL_ARGUMENT_ARRAY) {
189             argumentArray = new String[argArrayLen];
190             for (int i = 0; i < argArrayLen; i++) {
191                 Object val = in.readObject();
192                 if (!NULL_ARGUMENT_ARRAY_ELEMENT.equals(val)) {
193                     argumentArray[i] = val;
194                 }
195             }
196         }
197     }
198 
199     @Override
200     public int hashCode() {
201         final int prime = 31;
202         int result = 1;
203         result = prime * result + ((message == null) ? 0 : message.hashCode());
204         result = prime * result + ((threadName == null) ? 0 : threadName.hashCode());
205         result = prime * result + (int) (timeStamp ^ (timeStamp >>> 32));
206         return result;
207     }
208 
209     @Override
210     public boolean equals(Object obj) {
211         if (this == obj)
212             return true;
213         if (obj == null)
214             return false;
215         if (getClass() != obj.getClass())
216             return false;
217         final PubLoggingEventVO other = (PubLoggingEventVO) obj;
218         if (message == null) {
219             if (other.message != null)
220                 return false;
221         } else if (!message.equals(other.message))
222             return false;
223 
224         if (loggerName == null) {
225             if (other.loggerName != null)
226                 return false;
227         } else if (!loggerName.equals(other.loggerName))
228             return false;
229 
230         if (threadName == null) {
231             if (other.threadName != null)
232                 return false;
233         } else if (!threadName.equals(other.threadName))
234             return false;
235         if (timeStamp != other.timeStamp)
236             return false;
237 
238         if (markerList == null) {
239             if (other.markerList != null)
240                 return false;
241         } else if (!markerList.equals(other.markerList))
242             return false;
243 
244         if (mdcPropertyMap == null) {
245             if (other.mdcPropertyMap != null)
246                 return false;
247         } else if (!mdcPropertyMap.equals(other.mdcPropertyMap))
248             return false;
249         return true;
250     }
251 
252     public String toString() {
253         StringBuilder sb = new StringBuilder();
254         sb.append(timeStamp);
255         sb.append(" ");
256         sb.append(level);
257         sb.append(" [");
258         sb.append(threadName);
259         sb.append("] ");
260         sb.append(loggerName);
261         sb.append(" - ");
262         sb.append(getFormattedMessage());
263         return sb.toString();
264     }
265 
266 }