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(&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;
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.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
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.
LogSink::write(&this, record: &LogRecord) -> voidWrite 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.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) record: &LogRecordtype struct LogRecord {
level: LogLevel;
area: Str;
message: Arguments;
location: Location;
time: SystemTime;
}One thing logged. The message is formatted only when a sink writes it, and only during the LogSink.write call the record is passed to: it borrows the values the logging call was given.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type enum FormatError {
Io(IoError);
Allocation(AllocationError);
}Why formatting a value failed: the sink refused the bytes, or a sink that grows (a String) could not.
namespace std::log;log: records a program writes about what it is doing, by area and level, sent to one process-wide sink.
module pagenamespace std::log::logger;Logger, a named area a program logs through, and Logging, the process-wide settings every logger reads: the level for each area and the sink records go to.
type struct Logging {}The process-wide logging settings. Records at Warning and above go to standard error until a program says otherwise.
namespace std::log::sink;Where log records go: the LogSink trait, and StandardErrorSink, the sink logging writes to until a program sets its own.
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.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct LineSinkimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype struct LineSinkLineSink::write(&this, record: &LogRecord) -> voidconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const area: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
(parameter) record: &LogRecordmut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut line: Stringtype struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
static String::new() -> StringEmpty string. No heap storage until the first append, push or reserve.
match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, constants, and tuple and destructuring patterns, such as (0, 1..8) or Option::Some((count, _)), and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
mut line: StringString::write(mut &this, arguments: Arguments) -> Result<(), FormatError>via trait FormatWrite
Write an f-string, sink.write(f"x = {x}"), formatting each hole straight into this sink: an f-string written here is the Arguments of its pieces, not a String (fmt::Arguments).
LogRecord.level: LogLevelconst area: Strtype enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
Result::Ok(T)pattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
const _: Result<(), FormatError>primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
LogRecord.message: ArgumentsResult::Err(E)const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const sink: LineSinkstatic Logging::set_sink<S>(sink: &S) -> void
where S: LogSink, S: SyncSend every record from now on to sink, in place of standard error. The sink is borrowed, not kept: it must live until the program stops logging, as a global or a value main holds does. Any thread may log, so the sink's type must be Sync.
const sink: LineSinktype struct StandardErrorSink {}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:
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
StandardErrorSink.error_style: Styletype struct Style with [Eq, Clone, Debug] {}A foreground and background colour and a set of attributes, built up from Style::new(), which sets nothing.
StandardErrorSink.warning_style: StyleStandardErrorSink.information_style: StyleStandardErrorSink.debug_style: StyleStandardErrorSink.trace_style: StyleLogSink::write(&this, record: &LogRecord) -> voidWrite 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.
type struct StandardErrorSink {}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:
std::log::sinknew T { field: value }
new Path(args)
new E::Variant
new T[n]Heap allocation. new T { .. } places a struct literal on the heap and yields a T*, and new E::V does the same for a unit variant. new Path(args) places the value a call returns, whatever its type: new Table::new(8), new Option<i64>::Some(41), new make_pair(3, 4). new T[n] reserves n uninitialised T - the typed malloc. The storage comes from the value type's Allocate implementation, or else the global allocator. A type is not a function, so new Point(1, 2) is refused (E0231).
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static StandardErrorSink::new() -> StandardErrorSinkA 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.
std::log::sink