Durable.LogCapture (Durable v2.0.2)
View SourceCaptures Logger output and IO operations during workflow step execution.
Uses process dictionary key :durable_logs for buffering log entries.
Optionally replaces group_leader for IO.puts/IO.inspect capture.
Lines are written to the step's row when the step ends, so a step that's killed or still running has none saved. The console is the copy of those.
Usage
This module is used internally by Durable.Executor.StepRunner to capture
logs during step execution:
Durable.LogCapture.start_capture()
# Step execution happens here - Logger calls are captured
Logger.info("Processing order")
IO.puts("Debug output")
logs = Durable.LogCapture.stop_capture()
# => [%{timestamp: ..., level: "info", message: "Processing order", ...}, ...]Configuration
config :durable,
log_capture: [
enabled: true,
levels: [:warning, :error],
io_capture: true,
io_passthrough: false,
max_log_entries: 1000,
max_message_length: 10_000,
metadata_filter: :all,
console: true
]:levels- which levels to save; every level by default.:metadata_filter- which Logger metadata keys to save with each line::all(the default) or a list of keys. Anything in Logger metadata is stored until the run is pruned, so narrow this if yours can hold secrets.:console- set tofalseto keep the lines a step saves off the console. Takes effect when Durable starts. Lines are saved when a step ends, so withfalsea step that's killed (a cancel, a dead node, a lost lock) leaves its lines nowhere: keep the console if you need those.
What's saved
Each line is a map with "timestamp", "level", "severity_number"
(OpenTelemetry's), "message", "source", "metadata", "trace_id" (the
top run's id) and "span_id" (from the attempt's row id).
Summary
Functions
Adds an IO output entry to the log buffer.
Adds a log entry to the buffer.
Clears the log buffer.
Returns the current log buffer contents.
Checks if the current process is executing within a workflow context.
Starts log capture for the current process.
Stops log capture and returns the captured logs.
Functions
@spec add_io_log(String.t(), DateTime.t()) :: :ok
Adds an IO output entry to the log buffer.
Called when processing IO log messages from the IO server.
Adds a log entry to the buffer.
Called by the Logger handler when a log event occurs in workflow context.
Respects the max_log_entries configuration limit.
Parameters
level- The log level atom (:debug, :info, :warning, :error)message- The log message stringmetadata- Metadata map from the Logger event
@spec clear_logs() :: :ok
Clears the log buffer.
@spec get_logs() :: [map()]
Returns the current log buffer contents.
Logs are returned in chronological order (oldest first).
@spec in_workflow_context?() :: boolean()
Checks if the current process is executing within a workflow context.
Returns true if :durable_workflow_id is set in the process dictionary,
indicating that log capture should be active.
@spec start_capture(map()) :: :ok
Starts log capture for the current process.
Initializes the log buffer in the process dictionary and optionally starts IO capture by replacing the group_leader.
This should be called before step execution begins. ids holds the
:trace_id and :span_id saved with each line.
@spec stop_capture() :: [map()]
Stops log capture and returns the captured logs.
Restores the original group_leader if IO capture was active, stops the IO server process, and returns the formatted logs.
Returns an empty list if capture was not started or is disabled.