Logger
Owns an explicitly supplied concrete sink and severity threshold. No global logger, clock sample, allocation or implicit flush is introduced. Sink operations retain their own costs and failures. Synchronize complete logger operations when sharing mutable state; separate loggers writing to the same underlying stream also require record-level synchronization. Destruction releases owned fields without performing hidden output.
Module std.logging
import std.logging;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
Logger
pub final class Logger<S> where S: Movable, LogSinkOwns an explicitly supplied concrete sink and severity threshold. No global logger, clock sample, allocation or implicit flush is introduced. Sink operations retain their own costs and failures. Synchronize complete logger operations when sharing mutable state; separate loggers writing to the same underlying stream also require record-level synchronization. Destruction releases owned fields without performing hidden output. S: Movable structural LogSink, stored without boxing.
Example
module example.logger;
from std.logging import Logger, LogLevel;
from std.io import IoError;
from std.prelude import Result;
struct CaptureSink {
pub Records: usize;
pub Flushes: usize;
pub init() { this.Records = 0; this.Flushes = 0; }
pub fn Write(Level: LogLevel, Message: str) -> Result<void, IoError> {
if (Level is .Off) { return .Err { Error = .OutputFailed }; }
if Message == "fail" { return .Err { Error = .OutputFailed }; }
this.Records += 1; return .Ok;
}
pub fn Flush() -> Result<void, IoError> { this.Flushes += 1; return .Ok; }
}
final class Producer {
priv Calls: mut usize&;
pub init(Calls: mut usize&) { this.Calls = Calls; }
pub readonly fn Invoke() -> str { this.Calls += 1; return "ready"; }
}
fn Main() -> i32 {
var Log = Logger<CaptureSink>(CaptureSink());
if Log.MinimumLevel != .Information || Log.IsEnabled(.Debug) { return 1; }
var Calls: usize = 0;
if Log.WriteLazy(.Debug, Producer(Calls)) is .Err { Error = _ } { return 2; }
if Calls != 0 { return 3; }
Log.ChangeMinimumLevel(.Debug);
if Log.WriteLazy(.Debug, Producer(Calls)) is .Err { Error = _ } { return 4; }
if Calls != 1 || Log.Sink().Records != 1 { return 5; }
let Readonly: readonly Logger<CaptureSink>& = Log;
if !Readonly.IsEnabled(.Critical) || Readonly.Sink().Records != 1 { return 6; }
if Log.Write(.Warning, "ready") is .Err { Error = _ } { return 7; }
if (Log.Write(.Error, "fail") is .Ok) { return 8; }
Log.ChangeMinimumLevel(.Off);
if Log.IsEnabled(.Critical) { return 9; }
if Log.Write(.Critical, "ignored") is .Err { Error = _ } { return 10; }
if Log.Write(.Off, "ignored") is .Err { Error = _ } { return 11; }
if Log.Flush() is .Err { Error = _ } { return 12; }
if Log.Sink().Records != 2 || Log.Sink().Flushes != 1 { return 13; }
Log.Sink().Records = 0;
return 0;
}Logger.init
pub init(mut Sink: S, MinimumLevel: LogLevel)Adopts a concrete sink without implicit I/O. Sink: Owned destination moved into this logger. MinimumLevel: Lowest admitted severity; default Information.
Logger.init
pub init(mut Sink: S)Supplies the omitted arguments to init. Note: Defaults: MinimumLevel = .Information. Sink: Same input, ownership and lifetime contract as the full operation.
Logger.MinimumLevel
pub MinimumLevel: LogLevel;Observes the current threshold without touching the sink. Return: Owner-local threshold, including Off.
Logger.ChangeMinimumLevel
pub fn ChangeMinimumLevel(Level: LogLevel) -> voidChanges only this logger's filtering policy. Level: New minimum severity; Off suppresses all records. Return: No value after updating this logger's threshold; the sink remains untouched.
Logger.IsEnabled
pub readonly fn IsEnabled(Level: LogLevel) -> boolChecks severity admission without producing a message or sink effect. Level: Requested record severity. Return: False for Off or when below the current threshold.
Logger.Sink
pub readonly fn Sink() -> readonly S&Borrows immutable sink state without copying ownership. Return: Readonly sink reference valid until logger movement/destruction.
Logger.Sink
pub fn Sink() -> mut S&Borrows mutable sink state under caller synchronization. Return: Writable sink reference valid until logger movement/destruction.
Logger.Write
pub fn Write(Level: LogLevel, Message: str) -> Result<void, IoError>Submits a record only when its severity passes this logger's filter. Level: Record severity; Off performs no sink operation. Message: View borrowed only through return; argument evaluation is eager. Return: Sink completion/failure, or Ok without sink activity when disabled.
Logger.WriteLazy
pub fn WriteLazy<C>(Level: LogLevel, mut Producer: C) -> Result<void, IoError>
where C: Movable, NullaryCallable<str>Invokes a concrete message producer only for an enabled record. C: Owned Movable safe readonly NullaryCallable yielding a live str. Level: Record severity. Producer: Owned callback; construction is eager, invocation is filtered. Return: Exact sink completion/failure or disabled Ok without invocation.
Logger.Flush
pub fn Flush() -> Result<void, IoError>Explicitly propagates sink output even when filtering is Off. Return: Exact downstream flush completion; no durability promise.