001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.kryo; 015 016import ch.qos.logback.classic.Level; 017import ch.qos.logback.classic.spi.*; 018import org.slf4j.Marker; 019import org.slf4j.event.KeyValuePair; 020import org.slf4j.helpers.MessageFormatter; 021 022import java.util.*; 023 024final public class LoggingEventKryoVO implements ILoggingEvent { 025 026 private static final Map<String, String> EMPTY_MAP = Collections.emptyMap(); 027 028 /** 029 * Runtime class of {@link Collections#unmodifiableMap}. Logback's MDC snapshot 030 * is one of these wrapping a {@code HashMap}. 031 */ 032 private static final Class<?> UNMODIFIABLE_MAP_CLASS = 033 Collections.unmodifiableMap(new HashMap<String, String>()).getClass(); 034 String threadName; 035 String loggerName; 036 int levelInt; 037 String message; 038 Object[] argumentArray; 039 040 List<Marker> markerList; 041 List<KeyValuePair> keyValuePairList; 042 Map<String, String> mdcPropertyMap; 043 long timestamp; 044 int nanoseconds; 045 long sequenceNumber; 046 ThrowableProxyVO throwableProxyVO; 047 048 LoggerContextVO loggerContextVO; 049 050 StackTraceElement[] callerData; 051 052 public LoggingEventKryoVO() { 053 } 054 055 public LoggingEventKryoVO(ILoggingEvent le) { 056 this.loggerName = le.getLoggerName(); 057 this.loggerContextVO = le.getLoggerContextVO(); 058 059 this.threadName = le.getThreadName(); 060 this.levelInt = le.getLevel().levelInt; 061 this.message = le.getMessage(); 062 this.argumentArray = safeObjectArray(le.getArgumentArray()); 063 this.markerList = le.getMarkerList(); 064 this.keyValuePairList = le.getKeyValuePairs(); 065 Map<String, String> mdcMap = le.getMDCPropertyMap(); 066 // LoggingEvent substitutes Collections.emptyMap() when the adapter has no 067 // map. Vanilla Kryo has no registration for that class. An empty map is 068 // stored as null, which is what the event codecs already write for absence. 069 this.mdcPropertyMap = (mdcMap == null || mdcMap.isEmpty()) ? null : mdcMap; 070 this.timestamp = le.getTimeStamp(); 071 this.nanoseconds = le.getNanoseconds(); 072 this.sequenceNumber = le.getSequenceNumber(); 073 this.throwableProxyVO = ThrowableProxyVO.build(le.getThrowableProxy()); 074 if(le.hasCallerData()) { 075 this.callerData = le.getCallerData(); 076 } else { 077 this.callerData = null; 078 } 079 } 080 081// /** 082// * Empty maps serialize as {@link #EMPTY_MAP}. Logback's snapshot is a 083// * {@link Collections#unmodifiableMap} around a {@code HashMap}, which may 084// * still hold null values. 085// */ 086// private static Map<String, String> getSafeMdcPropertyMap(ILoggingEvent le) { 087// Map<String, String> mdcMap = le.getMDCPropertyMap(); 088// 089// if (mdcMap == null) 090// return null; 091// 092// if(mdcMap.isEmpty()) 093// return EMPTY_MAP; 094// 095// if (UNMODIFIABLE_MAP_CLASS.isInstance(mdcMap)) { 096// return mdcMap; 097// } 098// 099// 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}