Skip to content
CryoCryo home
Stdliblog

sink

import std::log::sink; · source

Types

LogSink

type trait LogSink {
    write(&this, record: &LogRecord) -> void;
}

A destination for log records. Logging calls write for every record that passes the level settings, one call at a time: no two writes to a sink overlap, so a sink needs no lock of its own for its output to come out whole. The record's message borrows the logging call's values, so a sink formats it during the call, not later. A sink cannot report a failure; a write that fails is the sink's to handle.

import std::fmt;
import std::fmt::error::{ FormatError };
import std::log::logger::{ Logging };
import std::log::record::{ LogRecord };
import std::log::sink::{ LogSink };

type struct LineSink { }

implement trait LogSink for struct LineSink {
    write(&this, record: &LogRecord) -> void {
        const area: Str = record.area;
        mut line: String = String::new();
        match (line.write(f"[{record.level}] {area}: ")) {
            Result::Ok(_)  => { const _: Result<(), FormatError> = line.write(record.message); }
            Result::Err(_) => { }
        }
        fmt::println(f"{line}");
    }
}

const sink: LineSink = LineSink { };

// in main:
Logging::set_sink(&sink);

Methods

write(&this, record: &LogRecord) -> void;

Write out record. The record's message borrows the logging call's values, so format it during this call, not later; a sink cannot report a failure, so a write that fails is the sink's to handle.

Implementors

implement trait LogSink for struct StandardErrorSink

StandardErrorSink

type struct StandardErrorSink {
    private error_style:       Style;
    private warning_style:     Style;
    private information_style: Style;
    private debug_style:       Style;
    private trace_style:       Style;
}

Writes each record to standard error as one line: its time in RFC 3339 (UTC, to the second), its level, its area and its message.

2026-11-09T14:03:07Z warning http: no route for /favicon.ico

The level is coloured — error red and bold, warning yellow, information green, debug blue, trace dim — when standard error takes colour (Terminal::supports_color), which new decides once.

Constructors

static new() -> StandardErrorSink;

A sink for standard error, its levels coloured (error red and bold, warning yellow, information green, debug blue, trace dim) when standard error takes colour now, and plain otherwise.