Nexus API Reference

Mdc
in package

FinalYes
Tags
psalm-api

Mapped Diagnostic Context (à la SLF4J / Logback). A two-tier key/value store that the Logger merges into every Record's context:

  • Static tier — process / thread scope. In Swoole SWOOLE_THREAD mode each worker thread has its own PHP heap, so a static field is naturally per-thread isolated. Good for: host, pid, threadId, service name.

  • Coroutine tier — optional, requires ext-swoole. When called inside a Swoole coroutine, put() stores under a dedicated key on Coroutine::getContext() instead of the static tier, so the value lives only as long as the coroutine. Good for: requestId, traceId, userId — values that belong to one in-flight request.

getAll() returns static ⊕ coroutine (coroutine wins on duplicate keys).

Values are log-safe primitives: scalars, Stringable objects, or null — anything the formatters can interpolate or JSON-encode losslessly.

Without ext-swoole loaded the coroutine tier transparently disables and everything falls back to the static tier.

Table of Contents

Methods

clearStatic()  : void
Clear ONLY the static tier. Coroutine contexts are cleared automatically when the coroutine ends.
get()  : string|int|float|bool|Stringable|null
getAll()  : array<string, scalar|Stringable|null>
Merge of static + coroutine tiers. Coroutine values override static.
put()  : void
putStatic()  : void
Always writes to the thread/process-static tier, bypassing any coroutine context. Use for boot-time process info (host, pid, threadId, service) that should survive across all requests on this thread.
remove()  : void
scope()  : T
Scoped MDC override. Sets each key, runs $fn, restores previous values (or removes them) in a finally block.

Methods

clearStatic()

Clear ONLY the static tier. Coroutine contexts are cleared automatically when the coroutine ends.

public static clearStatic() : void

get()

public static get(string $key) : string|int|float|bool|Stringable|null
Parameters
$key : string
Return values
string|int|float|bool|Stringable|null

getAll()

Merge of static + coroutine tiers. Coroutine values override static.

public static getAll() : array<string, scalar|Stringable|null>
Return values
array<string, scalar|Stringable|null>

put()

public static put(string $key, string|int|float|bool|Stringable|null $value) : void
Parameters
$key : string
$value : string|int|float|bool|Stringable|null

putStatic()

Always writes to the thread/process-static tier, bypassing any coroutine context. Use for boot-time process info (host, pid, threadId, service) that should survive across all requests on this thread.

public static putStatic(string $key, string|int|float|bool|Stringable|null $value) : void
Parameters
$key : string
$value : string|int|float|bool|Stringable|null

remove()

public static remove(string $key) : void
Parameters
$key : string

scope()

Scoped MDC override. Sets each key, runs $fn, restores previous values (or removes them) in a finally block.

public static scope(array<string, scalar|Stringable|null> $values, callable(): T $fn) : T
Parameters
$values : array<string, scalar|Stringable|null>
$fn : callable(): T
Tags
template
Return values
T
On this page

Search results