Skip to content

std::log::testing

Records what a body logs, as values a test asserts on. testing.run(fn logger -> body) hands the body a Logger that writes nowhere, and returns the body’s value with every record it logged through that logger, in order. A body that logs through an app field gives it the recorder with with App.logger = logger.

Import with import std::log::testing.

struct Record
    at: Instant
    level: Level
    message: String
    fields: List<Field>
end

One record a logger wrote.

impl Debug for Record

fn inspect(value: Record) -> String

impl Debug.inspect

impl Equatable for Record

fn equal(a: Record, b: Record) -> Bool

impl Equatable.equal

struct Logged<T>
    value: T
    records: List<Record>
end

What run returns: the body’s value and the records it logged.

impl Debug for Logged<T>

fn inspect<T>(value: Logged<T>) -> String where T: Debug

impl Debug.inspect

fn run<T>(min: Level = Level.Debug, body: (Logger) -> T) -> Logged<T>

Runs body with a logger that records each record at min or above, Debug unless set, and returns its value and the records, in order.

import std::log

got = run(fn logger -> log.warn(logger, "slow", [log.int("ms", 900)]))

assert Iter.count(got.records) == 1
only = List.first(got.records)!
assert only.level == log.Level.Warn
assert only.message == "slow"
assert only.fields == [log.int("ms", 900)]
fn messages(records: List<Record>) -> List<String>

The messages of records, for a test that checks only what was said.