Async-logger-state-to-json
Convert AsyncLoggerState into a JsonValue. This helper is the structured export path for async logger runtime snapshots or manually constructed state values when callers want machine-readable diagnostics instead of a plain string.
Interface
pub fn async_logger_state_to_json(state : AsyncLoggerState) -> @json_parser.JsonValue {}input
state : AsyncLoggerState- Snapshot produced byAsyncLogger::state()or any manually constructedAsyncLoggerStatevalue.
output
JsonValue- Structured JSON representation of the async logger snapshot.
Explanation
Detailed rules explaining key parameters and behaviors
- The JSON includes runtime mode, worker support, queue counters, lifecycle flags, last error, and flush policy.
- The top-level fields are
runtime,pending_count,dropped_count,is_closed,is_running,has_failed,last_error, andflush_policy. - The nested
runtimefield reusesasync_runtime_state_to_json(...), andflush_policyis serialized with the canonical labelsNever,Batch, orShutdown. - The public helper returns the same internal JSON snapshot shape used by
stringify_async_logger_state(...), so both export paths stay aligned without duplicate field assembly logic. - This helper is suitable for health endpoints, diagnostics payloads, and custom serialization flows.
- It shares the same stable field names used by
stringify_async_logger_state(...). - The state must already have been captured or constructed before serialization.
- Serialization preserves whatever snapshot combination it receives, including failure flags together with remaining backlog counts.
- This helper never rereads a logger instance by itself. If callers pass an older or manually constructed
AsyncLoggerState, the JSON reflects that provided value exactly rather than refreshing fields from live runtime state. - It also does not normalize mixed diagnostic combinations. If the provided snapshot says
is_closed=truetogether with retainedhas_failed=true,last_error, or non-zero backlog counters, those exact combinations are emitted unchanged.
How to Use
Here are some specific examples provided.
When Need Machine-readable Diagnostics
When the snapshot should be embedded into a JSON payload:
let state_json = async_logger_state_to_json(logger.state())In this example, callers receive a structured value that can be composed into larger JSON objects.
When Need A Snapshot Before Custom Stringify
When another serializer or pipeline expects a JSON value:
let payload = async_logger_state_to_json(logger.state())
println(@json_parser.stringify(payload))In this example, the helper stays useful even outside the built-in stringify wrapper.
Error Case
e.g.:
If the snapshot contains no error,
last_erroris serialized as an empty string.If the queue is empty,
pending_countanddropped_countare still serialized normally as numeric values.If
has_failedistrue, serialization does not forcepending_countto0or clearlast_error; it reports the snapshot exactly as provided.If callers need newer logger data, they must capture a fresh
AsyncLogger::state()first instead of expecting JSON conversion itself to refresh stale fields.
Notes
This helper preserves the nested runtime snapshot instead of flattening
modeandbackground_workeronto the top level.The resulting object matches the compact string form produced by
stringify_async_logger_state(...)after JSON stringification.Use this API when downstream code wants a JSON value rather than a ready-made string.
Pair it with
AsyncLogger::state()when you want current logger data, or withAsyncLoggerState::new(...)when tests or adapters are exporting a synthetic snapshot.