Class LogRecordContext
LogContext snapshot alongside a LogRecord.
The context snapshot is attached in a side table (not a field on any LogRecord type) so it is
visible regardless of whether the record is a base JUL LogRecord (as both debug pipelines
construct) or the commons LogRecord subclass.
Identity-key invariant (load-bearing):
The backing WeakHashMap behaves as an identity map only because neither base JUL
LogRecord nor the commons LogRecord subclass overrides equals/hashCode
(both use Object identity). If a future change gives either value-equality, the side table silently corrupts
(two distinct records comparing equal would share/overwrite one entry). A unit test guards this by asserting both
record types remain on Object equality.
Framework attach points:
The two attachIfAbsent overloads are called by RichLogger.log(java.util.logging.LogRecord) and by the
two REST debug pipelines to pre-seed a record's correlation context. They are not intended as a general
application-facing mutator.
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidattachIfAbsent(LogRecord rec) Attaches the current thread's liveLogContextsnapshot to the specified record, if no snapshot is already attached.static voidattachIfAbsent(LogRecord rec, Map<String, Object> ctx) Attaches the supplied context snapshot to the specified record, if no snapshot is already attached.Returns the context snapshot attached to the specified record.
-
Method Details
-
of
Returns the context snapshot attached to the specified record.- Parameters:
rec- The record. Must not benull .- Returns:
- The attached snapshot, or the shared empty
Map.of()singleton if none is attached. Nevernull .
-
attachIfAbsent
Attaches the current thread's liveLogContextsnapshot to the specified record, if no snapshot is already attached.Framework attach point (called as the first statement of
RichLogger.log(java.util.logging.LogRecord)). When the live context is empty this returns immediately, before touching the synchronized side table, so callers who never useLogContextpay only a thread-local read and anisEmpty()check — never a lock.- Parameters:
rec- The record to attach to. Must not benull .
-
attachIfAbsent
Attaches the supplied context snapshot to the specified record, if no snapshot is already attached.Framework attach point used by the debug pipelines to pre-seed a record from a snapshot carried across an async boundary. When the supplied map is empty this returns immediately, before touching the synchronized side table — symmetric with the one-arg overload — so a later one-arg call from an empty emitting thread cannot clobber a previously pre-seeded snapshot.
- Parameters:
rec- The record to attach to. Must not benull .ctx- The context snapshot to attach. Must not benull .
-