-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
LoggingEvent.java
executable file
·505 lines (431 loc) · 15.3 KB
/
LoggingEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
* Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
* reserved.
*
* This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
* v1.0 as published by the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.spi;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.util.EnvUtil;
import org.slf4j.Marker;
import org.slf4j.event.KeyValuePair;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.spi.MDCAdapter;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.LogbackMDCAdapter;
import ch.qos.logback.core.spi.SequenceNumberGenerator;
/**
* The internal representation of logging events. When an affirmative decision is made to log then a
* <code>LoggingEvent</code> instance is created. This instance is passed around to the different logback-classic
* components.
* <p/>
* <p>
* Writers of logback-classic components such as appenders should be aware of that some of the LoggingEvent fields are
* initialized lazily. Therefore, an appender wishing to output data to be later correctly read by a receiver, must
* initialize "lazy" fields prior to writing them out. See the {@link #prepareForDeferredProcessing()} method for the
* exact list.
* </p>
*
* @author Ceki Gülcü
* @author Sébastien Pennec
*/
public class LoggingEvent implements ILoggingEvent {
public static final String VIRTUAL_THREAD_NAME_PREFIX = "virtual-";
public static final String REGULAR_UNNAMED_THREAD_PREFIX = "unnamed-";
/**
* Fully qualified name of the calling Logger class. This field does not survive serialization.
* <p/>
* <p/>
* Note that the getCallerInformation() method relies on this fact.
*/
transient String fqnOfLoggerClass;
/**
* The name of thread in which this logging event was generated.
*/
private String threadName;
private String loggerName;
private LoggerContext loggerContext;
private LoggerContextVO loggerContextVO;
/**
* Level of logging event.
* <p/>
* <p>
* This field should not be accessed directly. You should use the {@link #getLevel} method instead.
* </p>
*/
private transient Level level;
private String message;
// we gain significant space at serialization time by marking
// formattedMessage as transient and constructing it lazily in
// getFormattedMessage()
transient String formattedMessage;
private transient Object[] argumentArray;
private ThrowableProxy throwableProxy;
private StackTraceElement[] callerDataArray;
private List<Marker> markerList;
private Map<String, String> mdcPropertyMap;
/**
* @since 1.3.0
*/
List<KeyValuePair> keyValuePairs;
/**
* The number of milliseconds elapsed from 1/1/1970 until logging event was created.
*/
private Instant instant;
private long timeStamp;
private int nanoseconds;
private long sequenceNumber;
public LoggingEvent() {
}
public LoggingEvent(String fqcn, Logger logger, Level level, String message, Throwable throwable,
Object[] argArray) {
this.fqnOfLoggerClass = fqcn;
this.loggerName = logger.getName();
this.loggerContext = logger.getLoggerContext();
this.loggerContextVO = loggerContext.getLoggerContextRemoteView();
this.level = level;
this.message = message;
this.argumentArray = argArray;
Instant instant = Clock.systemUTC().instant();
initTmestampFields(instant);
if (loggerContext != null) {
SequenceNumberGenerator sequenceNumberGenerator = loggerContext.getSequenceNumberGenerator();
if (sequenceNumberGenerator != null)
sequenceNumber = sequenceNumberGenerator.nextSequenceNumber();
}
if (throwable == null) {
throwable = extractThrowableAnRearrangeArguments(argArray);
}
if (throwable != null) {
this.throwableProxy = new ThrowableProxy(throwable);
if (loggerContext != null && loggerContext.isPackagingDataEnabled()) {
this.throwableProxy.calculatePackagingData();
}
}
}
void initTmestampFields(Instant instant) {
this.instant = instant;
long epochSecond = instant.getEpochSecond();
this.nanoseconds = instant.getNano();
long milliseconds = nanoseconds / 1000_000;
this.timeStamp = (epochSecond * 1000) + (milliseconds);
}
private Throwable extractThrowableAnRearrangeArguments(Object[] argArray) {
Throwable extractedThrowable = EventArgUtil.extractThrowable(argArray);
if (EventArgUtil.successfulExtraction(extractedThrowable)) {
this.argumentArray = EventArgUtil.trimmedCopy(argArray);
}
return extractedThrowable;
}
public void setArgumentArray(Object[] argArray) {
if (this.argumentArray != null) {
throw new IllegalStateException("argArray has been already set");
}
this.argumentArray = argArray;
}
public Object[] getArgumentArray() {
return this.argumentArray;
}
public void addKeyValuePair(KeyValuePair kvp) {
if (keyValuePairs == null) {
keyValuePairs = new ArrayList<>(4);
}
keyValuePairs.add(kvp);
}
public void setKeyValuePairs(List<KeyValuePair> kvpList) {
this.keyValuePairs = kvpList;
}
@Override
public List<KeyValuePair> getKeyValuePairs() {
return this.keyValuePairs;
}
public Level getLevel() {
return level;
}
public String getLoggerName() {
return loggerName;
}
public void setLoggerName(String loggerName) {
this.loggerName = loggerName;
}
public String getThreadName() {
if (threadName == null) {
threadName = extractThreadName(Thread.currentThread());
}
return threadName;
}
/**
* Extracts the name of aThread by calling {@link Thread#getName()}. If the value is null, then use the value
* returned by {@link Thread#getId()} prefixing with {@link #VIRTUAL_THREAD_NAME_PREFIX} if thread is virtual or
* with {@link #REGULAR_UNNAMED_THREAD_PREFIX} if regular.
*
* @param aThread
* @return
* @since 1.5.0
*/
private String extractThreadName(Thread aThread) {
if (aThread == null) {
return CoreConstants.NA;
}
String threadName = aThread.getName();
if (threadName != null && !threadName.isEmpty())
return threadName;
Long virtualThreadId = getVirtualThreadId(aThread);
if (virtualThreadId != null) {
return VIRTUAL_THREAD_NAME_PREFIX + virtualThreadId;
} else {
return REGULAR_UNNAMED_THREAD_PREFIX + aThread.getId();
}
}
// +
/**
* Return the threadId if running under JDK 21+ and the thread is a virtual thread, return null otherwise.
*
* @param aThread
* @return Return the threadId if the thread is a virtual thread, return null otherwise.
*/
Long getVirtualThreadId(Thread aThread) {
if (EnvUtil.isJDK21OrHigher()) {
try {
Method isVirtualMethod = Thread.class.getMethod("isVirtual");
boolean isVirtual = (boolean) isVirtualMethod.invoke(aThread);
if (isVirtual)
return aThread.getId();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
return null;
}
}
return null;
}
/**
* @param threadName The threadName to set.
* @throws IllegalStateException If threadName has been already set.
*/
public void setThreadName(String threadName) throws IllegalStateException {
if (this.threadName != null) {
throw new IllegalStateException("threadName has been already set");
}
this.threadName = threadName;
}
/**
* Returns the throwable information contained within this event. May be
* <code>null</code> if there is no such information.
*/
public IThrowableProxy getThrowableProxy() {
return throwableProxy;
}
/**
* Set this event's throwable information.
*/
public void setThrowableProxy(ThrowableProxy tp) {
if (throwableProxy != null) {
throw new IllegalStateException("ThrowableProxy has been already set.");
} else {
throwableProxy = tp;
}
}
/**
* This method should be called prior to serializing an event. It should also be called when using asynchronous or
* deferred logging.
* <p/>
* <p/>
* Note that due to performance concerns, this method does NOT extract caller data. It is the responsibility of the
* caller to extract caller information.
*/
public void prepareForDeferredProcessing() {
this.getFormattedMessage();
this.getThreadName();
// fixes http://jira.qos.ch/browse/LBCLASSIC-104
this.getMDCPropertyMap();
}
public void setLoggerContext(LoggerContext lc) {
this.loggerContext = lc;
}
public LoggerContextVO getLoggerContextVO() {
return loggerContextVO;
}
public void setLoggerContextRemoteView(LoggerContextVO loggerContextVO) {
this.loggerContextVO = loggerContextVO;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
if (this.message != null) {
throw new IllegalStateException("The message for this event has been set already.");
}
this.message = message;
}
/**
* Return the {@link Instant} corresponding to the creation of this event.
*
* @see {@link #getTimeStamp()}
* @since 1.3
*/
public Instant getInstant() {
return instant;
}
/**
* Set {@link Instant} corresponding to the creation of this event.
*
* The value of {@link #getTimeStamp()} will be overridden as well.
*/
public void setInstant(Instant instant) {
initTmestampFields(instant);
}
/**
* Return the number of elapsed milliseconds since epoch in UTC.
*/
public long getTimeStamp() {
return timeStamp;
}
/**
* Return the number of nanoseconds past the {@link #getTimeStamp() timestamp in seconds}.
*
* @since 1.3.0
*/
@Override
public int getNanoseconds() {
return nanoseconds;
}
/**
* Set the number of elapsed milliseconds since epoch in UTC.
*
* Setting the timestamp will override the value contained in {@link #getInstant}. Nanoseconds value will be
* computed form the provided millisecond value.
*/
public void setTimeStamp(long timeStamp) {
Instant instant = Instant.ofEpochMilli(timeStamp);
setInstant(instant);
}
@Override
public long getSequenceNumber() {
return sequenceNumber;
}
public void setSequenceNumber(long sn) {
sequenceNumber = sn;
}
public void setLevel(Level level) {
if (this.level != null) {
throw new IllegalStateException("The level has been already set for this event.");
}
this.level = level;
}
/**
* Get the caller information for this logging event. If caller information is null at the time of its invocation,
* this method extracts location information. The collected information is cached for future use.
* <p/>
* <p>
* Note that after serialization it is impossible to correctly extract caller information.
* </p>
*/
public StackTraceElement[] getCallerData() {
if (callerDataArray == null) {
callerDataArray = CallerData.extract(new Throwable(), fqnOfLoggerClass,
loggerContext.getMaxCallerDataDepth(), loggerContext.getFrameworkPackages());
}
return callerDataArray;
}
public boolean hasCallerData() {
return (callerDataArray != null);
}
public void setCallerData(StackTraceElement[] callerDataArray) {
this.callerDataArray = callerDataArray;
}
public List<Marker> getMarkerList() {
return markerList;
}
public void addMarker(Marker marker) {
if (marker == null) {
return;
}
if (markerList == null) {
markerList = new ArrayList<>(4);
}
markerList.add(marker);
}
public long getContextBirthTime() {
return loggerContextVO.getBirthTime();
}
// lazy computation as suggested in LOGBACK-495
public String getFormattedMessage() {
if (formattedMessage != null) {
return formattedMessage;
}
if (argumentArray != null) {
formattedMessage = MessageFormatter.arrayFormat(message, argumentArray).getMessage();
} else {
formattedMessage = message;
}
return formattedMessage;
}
public Map<String, String> getMDCPropertyMap() {
// populate mdcPropertyMap if null
if (mdcPropertyMap == null) {
MDCAdapter mdcAdapter = loggerContext.getMDCAdapter();
if (mdcAdapter instanceof LogbackMDCAdapter)
mdcPropertyMap = ((LogbackMDCAdapter) mdcAdapter).getPropertyMap();
else
mdcPropertyMap = mdcAdapter.getCopyOfContextMap();
}
// mdcPropertyMap still null, use emptyMap()
if (mdcPropertyMap == null)
mdcPropertyMap = Collections.emptyMap();
return mdcPropertyMap;
}
/**
* Set the MDC map for this event.
*
* @param map
* @since 1.0.8
*/
public void setMDCPropertyMap(Map<String, String> map) {
if (mdcPropertyMap != null) {
throw new IllegalStateException("The MDCPropertyMap has been already set for this event.");
}
this.mdcPropertyMap = map;
}
/**
* Synonym for [@link #getMDCPropertyMap}.
*
* @deprecated Replaced by [@link #getMDCPropertyMap}
*/
public Map<String, String> getMdc() {
return getMDCPropertyMap();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
sb.append(level).append("] ");
sb.append(getFormattedMessage());
return sb.toString();
}
/**
* LoggerEventVO instances should be used for serialization. Use {@link LoggingEventVO#build(ILoggingEvent) build}
* method to create the LoggerEventVO instance.
*
* @since 1.0.11
*/
private void writeObject(ObjectOutputStream out) throws IOException {
throw new UnsupportedOperationException(this.getClass() + " does not support serialization. "
+ "Use LoggerEventVO instance instead. See also LoggerEventVO.build method.");
}
}