Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
Development · Evolving reference

AsyncLogger

Owns an explicitly supplied concrete sink and severity threshold. No global logger, clock sample, boxed sink or implicit flush is introduced. Managed task/frame allocation follows the existing task-runtime contract. Writable sink and message backing remain borrowed through terminal completion. 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.

Sn is under heavy development. This reference includes the intended 1.0 design; features may be incomplete in the current compiler. Read the status →

Module std.logging

import std.logging;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

AsyncLogger

pub final class AsyncLogger<S> where S: Movable, AsyncLogSink

Owns an explicitly supplied concrete sink and severity threshold. No global logger, clock sample, boxed sink or implicit flush is introduced. Managed task/frame allocation follows the existing task-runtime contract. Writable sink and message backing remain borrowed through terminal completion. 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 AsyncLogSink, stored without boxing.

Example

module example.async_logger;
from std.logging import AsyncLogger, LogLevel;
from std.io import IoError;
from std.prelude import Result;
from std.task import Task, CancellationToken;
struct CaptureSink {
    pub Records: usize;
    pub Flushes: usize;
    pub init() { this.Records = 0; this.Flushes = 0; }
    pub async fn WriteAsync(Level: LogLevel, Message: str, mut Token: CancellationToken) -> Task<Result<void, IoError>> {
        if (Level is .Off) { return .Err { Error = .OutputFailed }; }
        if Message == "fail" { return .Err { Error = .OutputFailed }; }
        this.Records += 1; return .Ok;
    }
    pub async fn FlushAsync(mut Token: CancellationToken) -> Task<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"; }
}
async fn Main() -> Task<i32> {
    var Log = AsyncLogger<CaptureSink>(CaptureSink());
    if Log.MinimumLevel != .Information || Log.IsEnabled(.Debug) { return 1; }
    var Calls: usize = 0;
    if await Log.WriteLazyAsync(.Debug, Producer(Calls)) is .Err { Error = _ } { return 2; }
    if Calls != 0 { return 3; }
    Log.ChangeMinimumLevel(.Debug);
    if await Log.WriteLazyAsync(.Debug, Producer(Calls)) is .Err { Error = _ } { return 4; }
    if Calls != 1 || Log.Sink().Records != 1 { return 5; }
    let Readonly: readonly AsyncLogger<CaptureSink>& = Log;
    if !Readonly.IsEnabled(.Critical) || Readonly.Sink().Records != 1 { return 6; }
    if await Log.WriteAsync(.Warning, "ready") is .Err { Error = _ } { return 7; }
    if (await Log.WriteAsync(.Error, "fail") is .Ok) { return 8; }
    Log.ChangeMinimumLevel(.Off);
    if Log.IsEnabled(.Critical) { return 9; }
    if await Log.WriteAsync(.Critical, "ignored") is .Err { Error = _ } { return 10; }
    if await Log.WriteAsync(.Off, "ignored") is .Err { Error = _ } { return 11; }
    if await Log.FlushAsync() is .Err { Error = _ } { return 12; }
    if Log.Sink().Records != 2 || Log.Sink().Flushes != 1 { return 13; }
    Log.Sink().Records = 0;
    return 0;
}

AsyncLogger.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.

AsyncLogger.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.

AsyncLogger.MinimumLevel

pub MinimumLevel: LogLevel;

Observes the current threshold without touching the sink. Return: Owner-local threshold, including Off.

AsyncLogger.ChangeMinimumLevel

pub fn ChangeMinimumLevel(Level: LogLevel) -> void

Changes 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.

AsyncLogger.IsEnabled

pub readonly fn IsEnabled(Level: LogLevel) -> bool

Checks severity admission without producing a message or sink effect. Level: Requested record severity. Return: False for Off or when below the current threshold.

AsyncLogger.Sink

pub readonly fn Sink() -> readonly S&

Borrows immutable sink state without copying ownership. Return: Readonly sink reference valid until logger movement/destruction.

AsyncLogger.Sink

pub fn Sink() -> mut S&

Borrows mutable sink state under caller synchronization. Return: Writable sink reference valid until logger movement/destruction.

AsyncLogger.WriteAsync

pub async fn WriteAsync(Level: LogLevel, Message: str, mut Token: CancellationToken) -> Task<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 retained through terminal completion; argument evaluation is eager. Token: Owned cooperative cancellation observation, inert by default. Return: Task yielding sink completion/failure, or Ok without sink activity when disabled.

AsyncLogger.WriteAsync

pub fn WriteAsync(mut Level: LogLevel, Message: str) -> Task<Result<void, IoError>>

Supplies the omitted arguments to WriteAsync. Note: Defaults: Token = CancellationToken(). Level: Same input, ownership and lifetime contract as the full operation. Message: Same input, ownership and lifetime contract as the full operation. Return: Exact full-operation result; no additional async frame is created.

AsyncLogger.WriteLazyAsync

pub async fn WriteLazyAsync<C>(Level: LogLevel, mut Producer: C, mut Token: CancellationToken) -> Task<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 synchronous callback; construction is eager, invocation is filtered. Token: Owned cooperative cancellation observation, inert by default. Return: Task yielding exact sink completion/failure or disabled Ok without invocation.

AsyncLogger.WriteLazyAsync

pub fn WriteLazyAsync<C>(mut Level: LogLevel, mut Producer: C) -> Task<Result<void, IoError>>
        where C: Movable, NullaryCallable<str>

Supplies the omitted arguments to WriteLazyAsync. Note: Defaults: Token = CancellationToken(). C: Same generic argument and constraints as the full operation. Level: Same input, ownership and lifetime contract as the full operation. Producer: Same input, ownership and lifetime contract as the full operation. Return: Exact full-operation result; no additional async frame is created.

AsyncLogger.FlushAsync

pub async fn FlushAsync(mut Token: CancellationToken) -> Task<Result<void, IoError>>

Explicitly propagates sink output even when filtering is Off. Token: Owned cooperative cancellation observation, inert by default. Return: Task yielding exact downstream flush completion; no durability promise.

AsyncLogger.FlushAsync

pub fn FlushAsync() -> Task<Result<void, IoError>>

Supplies the omitted arguments to FlushAsync. Note: Defaults: Token = CancellationToken(). Return: Exact full-operation result; no additional async frame is created.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library