Class LogRecordContext

java.lang.Object
org.apache.juneau.commons.logging.LogRecordContext

public final class LogRecordContext extends Object
Identity-keyed side table that carries a 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 Details

    • of

      public static Map<String,Object> of(LogRecord rec)
      Returns the context snapshot attached to the specified record.
      Parameters:
      rec - The record. Must not be null.
      Returns:
      The attached snapshot, or the shared empty Map.of() singleton if none is attached. Never null.
    • attachIfAbsent

      public static void attachIfAbsent(LogRecord rec)
      Attaches the current thread's live LogContext snapshot 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 use LogContext pay only a thread-local read and an isEmpty() check — never a lock.

      Parameters:
      rec - The record to attach to. Must not be null.
    • attachIfAbsent

      public static void attachIfAbsent(LogRecord rec, Map<String,Object> ctx)
      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 be null.
      ctx - The context snapshot to attach. Must not be null.