Statifier.Logging.TestAdapter (statifier v1.9.0)

View Source

Logging adapter that stores log entries in the StateChart for testing.

This adapter is designed for use in test environments where you want to:

  • Keep test output clean (no log pollution)
  • Inspect log messages in tests
  • Verify that correct logging occurs

The adapter stores log entries in the StateChart's logs field as a list, with optional circular buffer behavior to prevent memory growth.

Configuration

# Unlimited log storage
adapter = %Statifier.Logging.TestAdapter{}

# Circular buffer with max 100 entries
adapter = %Statifier.Logging.TestAdapter{max_entries: 100}

Log Entry Format

Each log entry is a map with the following structure:

%{
  timestamp: ~U[2023-01-01 12:00:00.000000Z],
  level: :info,
  message: "Processing started",
  metadata: %{action_type: "initialization"}
}

Usage in Tests

# Create state chart with TestAdapter
{:ok, state_chart} = Interpreter.initialize(document, [
  log_adapter: {Statifier.Logging.TestAdapter, [max_entries: 50]}
])

# ... perform actions that log ...

# Inspect captured logs
assert [%{level: :info, message: "Processing started"}] = state_chart.logs

Summary

Functions

Clears all captured log entries from a StateChart.

Returns all captured log entries from a StateChart.

Returns log entries filtered by level.

Types

t()

@type t() :: %Statifier.Logging.TestAdapter{max_entries: pos_integer() | nil}

Functions

clear_logs(state_chart)

Clears all captured log entries from a StateChart.

Returns an updated StateChart with empty logs.

Examples

state_chart = Statifier.Logging.TestAdapter.clear_logs(state_chart)
assert state_chart.logs == []

get_logs(state_chart)

@spec get_logs(Statifier.StateChart.t()) :: [map()]

Returns all captured log entries from a StateChart.

Entries are returned in chronological order (oldest first).

Examples

logs = Statifier.Logging.TestAdapter.get_logs(state_chart)
assert length(logs) == 3

get_logs(state_chart, level)

@spec get_logs(Statifier.StateChart.t(), atom()) :: [map()]

Returns log entries filtered by level.

Examples

info_logs = Statifier.Logging.TestAdapter.get_logs(state_chart, :info)
error_logs = Statifier.Logging.TestAdapter.get_logs(state_chart, :error)