View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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.kryo;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.spi.*;
18  import org.slf4j.Marker;
19  import org.slf4j.event.KeyValuePair;
20  import org.slf4j.helpers.MessageFormatter;
21  
22  import java.util.*;
23  
24  final public class LoggingEventKryoVO implements ILoggingEvent {
25  
26      private static final Map<String, String> EMPTY_MAP = Collections.emptyMap();
27  
28      /**
29       * Runtime class of {@link Collections#unmodifiableMap}. Logback's MDC snapshot
30       * is one of these wrapping a {@code HashMap}.
31       */
32      private static final Class<?> UNMODIFIABLE_MAP_CLASS =
33              Collections.unmodifiableMap(new HashMap<String, String>()).getClass();
34      String threadName;
35      String loggerName;
36      int levelInt;
37      String message;
38      Object[] argumentArray;
39  
40      List<Marker> markerList;
41      List<KeyValuePair> keyValuePairList;
42      Map<String, String> mdcPropertyMap;
43      long timestamp;
44      int nanoseconds;
45      long sequenceNumber;
46      ThrowableProxyVO throwableProxyVO;
47  
48      LoggerContextVO loggerContextVO;
49  
50      StackTraceElement[] callerData;
51  
52      public LoggingEventKryoVO() {
53      }
54  
55      public LoggingEventKryoVO(ILoggingEvent le) {
56          this.loggerName = le.getLoggerName();
57          this.loggerContextVO = le.getLoggerContextVO();
58  
59          this.threadName = le.getThreadName();
60          this.levelInt = le.getLevel().levelInt;
61          this.message = le.getMessage();
62          this.argumentArray = safeObjectArray(le.getArgumentArray());
63          this.markerList = le.getMarkerList();
64          this.keyValuePairList = le.getKeyValuePairs();
65          Map<String, String> mdcMap = le.getMDCPropertyMap();
66          // LoggingEvent substitutes Collections.emptyMap() when the adapter has no
67          // map. Vanilla Kryo has no registration for that class. An empty map is
68          // stored as null, which is what the event codecs already write for absence.
69          this.mdcPropertyMap = (mdcMap == null || mdcMap.isEmpty()) ? null : mdcMap;
70          this.timestamp = le.getTimeStamp();
71          this.nanoseconds = le.getNanoseconds();
72          this.sequenceNumber = le.getSequenceNumber();
73          this.throwableProxyVO = ThrowableProxyVO.build(le.getThrowableProxy());
74          if(le.hasCallerData()) {
75              this.callerData = le.getCallerData();
76          } else {
77               this.callerData = null;
78          }
79      }
80  
81  //    /**
82  //     * Empty maps serialize as {@link #EMPTY_MAP}. Logback's snapshot is a
83  //     * {@link Collections#unmodifiableMap} around a {@code HashMap}, which may
84  //     * still hold null values.
85  //     */
86  //    private static Map<String, String> getSafeMdcPropertyMap(ILoggingEvent le) {
87  //        Map<String, String> mdcMap = le.getMDCPropertyMap();
88  //
89  //        if (mdcMap == null)
90  //            return null;
91  //
92  //        if(mdcMap.isEmpty())
93  //            return EMPTY_MAP;
94  //
95  //        if (UNMODIFIABLE_MAP_CLASS.isInstance(mdcMap)) {
96  //            return mdcMap;
97  //        }
98  //
99  //        if (hasNullKeyOrValue(mdcMap)) {
100 //            return copyOfWithoutNullEntries(mdcMap);
101 //        }
102 //
103 //        return Map.copyOf(mdcMap);
104 //    }
105 
106     private static boolean hasNullKeyOrValue(Map<String, String> map) {
107         for (Map.Entry<String, String> entry : map.entrySet()) {
108             if (entry.getKey() == null || entry.getValue() == null) {
109                 return true;
110             }
111         }
112         return false;
113     }
114 
115     private static Map<String, String> copyOfWithoutNullEntries(Map<String, String> map) {
116         Map<String, String> kept = new HashMap<>();
117         for (Map.Entry<String, String> entry : map.entrySet()) {
118             String key = entry.getKey();
119             String value = entry.getValue();
120             if (key != null && value != null) {
121                 kept.put(key, value);
122             }
123         }
124         if (kept.isEmpty()) {
125             return null;
126         }
127         return Map.copyOf(kept);
128     }
129 
130     /**
131      * Returns {@code objects} as-is when every element is already null or a
132      * {@link SafeObjectTransformer} type. Otherwise a copy with unsafe values
133      * reduced via {@link SafeObjectTransformer#asAllowedObject(Object)}.
134      * {@link SafeObjectCodec} still applies the same reduction on write.
135      */
136     static Object[] safeObjectArray(Object[] objects) {
137         if (objects == null) {
138             return null;
139         }
140         int n = objects.length;
141         for (int i = 0; i < n; i++) {
142             Object obj = objects[i];
143             if (obj != null && !SafeObjectTransformer.isAllowedType(obj)) {
144                 Object[] copy = new Object[n];
145                 System.arraycopy(objects, 0, copy, 0, i);
146                 copy[i] = SafeObjectTransformer.asAllowedObject(obj);
147                 for (int j = i + 1; j < n; j++) {
148                     copy[j] = safeObject(objects[j]);
149                 }
150                 return copy;
151             }
152         }
153         return objects;
154     }
155 
156     static Object safeObject(Object o) {
157         if (o == null)
158             return null;
159 
160         return SafeObjectTransformer.asAllowedObject(o);
161 
162     }
163 
164     @Override
165     public String getThreadName() {
166         return threadName;
167     }
168 
169     @Override
170     public Level getLevel() {
171         return Level.toLevel(levelInt);
172     }
173 
174     @Override
175     public String getMessage() {
176         return message;
177     }
178 
179     @Override
180     public Object[] getArgumentArray() {
181         return argumentArray;
182     }
183 
184     @Override
185     public String getFormattedMessage() {
186         String formattedMessage;
187         if (argumentArray != null) {
188             if (throwableProxyVO == null) {
189                 formattedMessage = MessageFormatter.arrayFormat(message, argumentArray).getMessage();
190             } else {
191                 // very rare case where the argument array ends with two exceptions
192                 // See https://github.com/qos-ch/logback/issues/876
193                 formattedMessage = MessageFormatter.arrayFormat(message, argumentArray, null).getMessage();
194             }
195         } else {
196             formattedMessage = message;
197         }
198         return formattedMessage;
199     }
200 
201 
202     @Override
203     public String getLoggerName() {
204         return loggerName;
205     }
206 
207     @Override
208     public LoggerContextVO getLoggerContextVO() {
209         return loggerContextVO;
210     }
211 
212     @Override
213     public IThrowableProxy getThrowableProxy() {
214         return throwableProxyVO;
215     }
216 
217     @Override
218     public StackTraceElement[] getCallerData() {
219         return callerData;
220     }
221 
222     @Override
223     public boolean hasCallerData() {
224         return callerData != null;
225     }
226 
227     @Override
228     public List<Marker> getMarkerList() {
229         return markerList;
230     }
231 
232     @Override
233     public Map<String, String> getMDCPropertyMap() {
234         return mdcPropertyMap;
235     }
236 
237     @Override
238     public Map<String, String> getMdc() {
239         return Map.of();
240     }
241 
242     @Override
243     public long getTimeStamp() {
244         return timestamp;
245     }
246 
247     @Override
248     public int getNanoseconds() {
249         return nanoseconds;
250     }
251 
252     @Override
253     public long getSequenceNumber() {
254         return sequenceNumber;
255     }
256 
257     @Override
258     public List<KeyValuePair> getKeyValuePairs() {
259         return keyValuePairList;
260     }
261 
262     @Override
263     public void prepareForDeferredProcessing() {
264     }
265 
266     @Override
267     public boolean equals(Object o) {
268         if (o == null || getClass() != o.getClass()) return false;
269         LoggingEventKryoVO that = (LoggingEventKryoVO) o;
270         return levelInt == that.levelInt && timestamp == that.timestamp
271                 && nanoseconds == that.nanoseconds && sequenceNumber == that.sequenceNumber
272                 && loggerContextVO.equals(that.loggerContextVO)
273                 && Objects.equals(threadName, that.threadName)
274                 && Objects.equals(loggerName, that.loggerName)
275                 && Objects.equals(message, that.message) && Objects.deepEquals(argumentArray, that.argumentArray)
276                 && Objects.equals(markerList, that.markerList)
277                 && Objects.equals(keyValuePairList, that.keyValuePairList)
278                 && Objects.equals(mdcPropertyMap, that.mdcPropertyMap)
279                 && Objects.equals(throwableProxyVO, that.throwableProxyVO)
280                 && Arrays.equals(callerData, that.callerData);
281     }
282 
283     @Override
284     public int hashCode() {
285         return Objects.hash(timestamp, nanoseconds, loggerContextVO, threadName, loggerName, levelInt, message, Arrays.hashCode(argumentArray), markerList, keyValuePairList, mdcPropertyMap, Arrays.hashCode(callerData));
286     }
287 
288     @Override
289     public String toString() {
290         return "LoggingEventKryoVO{" +
291                 "threadName='" + threadName + '\'' +
292                 ", loggerName='" + loggerName + '\'' +
293                 ", level=" + getLevel() +
294                 ", message='" + message + '\'' +
295                 ", argumentArray=" + Arrays.toString(argumentArray) +
296                 ", markerList=" + markerList +
297                 ", keyValuePairList=" + keyValuePairList +
298                 ", mdcPropertyMap=" + mdcPropertyMap +
299                 ", timestamp=" + timestamp +
300                 ", nanoseconds=" + nanoseconds +
301                 ", sequenceNumber=" + sequenceNumber +
302                 ", throwableProxyVO=" + throwableProxyVO +
303                 ", callerData=" + Arrays.toString(callerData) +
304                 ", loggerContextVO='" + loggerContextVO + '}';
305     }
306 
307     /**
308      * A builder to create {@link LoggingEventVO}.
309      *
310      * @since 1.5.22
311      */
312     public static class Builder {
313 
314         LoggingEventKryoVO loggingEventKryoVO = new LoggingEventKryoVO();
315 
316         public LoggingEventKryoVO.Builder withThreadName(String threadName) {
317             loggingEventKryoVO.threadName = threadName;
318             return this;
319         }
320 
321         public LoggingEventKryoVO.Builder withLoggerName(String loggerName) {
322             loggingEventKryoVO.loggerName = loggerName;
323             return this;
324         }
325 
326         public LoggingEventKryoVO.Builder withLoggerContextVO(LoggerContextVO loggerContextVO) {
327             loggingEventKryoVO.loggerContextVO = loggerContextVO;
328             return this;
329         }
330 
331         public LoggingEventKryoVO.Builder withLevelInt(int levelInt) {
332             loggingEventKryoVO.levelInt = levelInt;
333             return this;
334         }
335 
336         public LoggingEventKryoVO.Builder withMessage(String message) {
337             loggingEventKryoVO.message = message;
338             return this;
339         }
340 
341         public LoggingEventKryoVO.Builder withArgumentArray(Object[] argumentArray) {
342             loggingEventKryoVO.argumentArray = argumentArray;
343             return this;
344         }
345 
346         public LoggingEventKryoVO.Builder withThrowableProxy(ThrowableProxyVO throwableProxy) {
347             loggingEventKryoVO.throwableProxyVO = throwableProxy;
348             return this;
349         }
350 
351         public LoggingEventKryoVO.Builder withMarkerList(List<Marker> markerList) {
352             loggingEventKryoVO.markerList = markerList;
353             return this;
354         }
355 
356         public LoggingEventKryoVO.Builder withKeyValuePairList(List<KeyValuePair> keyValuePairList) {
357             loggingEventKryoVO.keyValuePairList = keyValuePairList;
358             return this;
359         }
360 
361         public LoggingEventKryoVO.Builder withMdcPropertyMap(Map<String, String> mdcPropertyMap) {
362             loggingEventKryoVO.mdcPropertyMap = mdcPropertyMap;
363             return this;
364         }
365 
366         public LoggingEventKryoVO.Builder withTimestamp(long timestamp) {
367             loggingEventKryoVO.timestamp = timestamp;
368             return this;
369         }
370 
371         public LoggingEventKryoVO.Builder withNanoseconds(int nanoseconds) {
372             loggingEventKryoVO.nanoseconds = nanoseconds;
373             return this;
374         }
375 
376         public LoggingEventKryoVO.Builder withSequenceNumber(long sequenceNumber) {
377             loggingEventKryoVO.sequenceNumber = sequenceNumber;
378             return this;
379         }
380 
381         public LoggingEventKryoVO.Builder withCallerData(StackTraceElement[] callerData) {
382             loggingEventKryoVO.callerData = callerData;
383             return this;
384         }
385 
386         public LoggingEventKryoVO build() {
387             return loggingEventKryoVO;
388         }
389     }
390 
391 }