Skip to content
CryoCryo home
Stdliblog

logger

import std::log::logger; · source

Types

Logger

type struct Logger {
    private area: Str;
}

A named area of a program, Logger::new(Str::new("http")), that logs records at a level. A logger is a name and nothing else: copy it, or keep one in a global. It keeps the Str it was given, not a copy, so the text must outlive it, as a literal does.

import std::collections::str::{ Str };
import std::log::logger::{ Logger };

const log: Logger = Logger::new(Str::new("http"));

const path: Str = Str::new("/favicon.ico");
log.warning(f"no route for {path}");

error, warning, information, debug and trace log at their level, and log(level, message) at the level given. Each takes the message as an f-string written in the call, which is formatted only when a sink writes it, so a record the settings refuse costs one atomic load. The record carries the location of the logging call. is_enabled(level) answers whether a record at level would get through, for work worth skipping when it would not.

Constructors

static new(area: Str) -> Logger;

A logger for area, a name like http or http::server; an area below another (http::server below http) takes the level set for it when it has none of its own. The logger keeps the Str, not a copy, so the text must outlive it, as a literal does.

Methods

area(&this) -> Str;

The area this logger was made for, as given to new.

is_enabled(&this, level: LogLevel) -> boolean;

True when the settings let a record at level from this logger's area through.

![caller_location]
log(&this, level: LogLevel, message: Arguments) -> void;

Hand a record to the sink when the settings let it through. The record's location is where this call is written.

![caller_location]
error(&this, message: Arguments) -> void;

log at LogLevel::Error, the least verbose level: something failed. Let through by the default settings.

![caller_location]
warning(&this, message: Arguments) -> void;

log at LogLevel::Warning: something is wrong but the program carries on. Let through by the default settings.

![caller_location]
information(&this, message: Arguments) -> void;

log at LogLevel::Information: what the program is doing. Refused until the settings allow Information for this area.

![caller_location]
debug(&this, message: Arguments) -> void;

log at LogLevel::Debug: detail for finding a fault. Refused until the settings allow Debug for this area.

![caller_location]
trace(&this, message: Arguments) -> void;

log at LogLevel::Trace, the most verbose level: step-by-step detail. Refused until the settings allow Trace for this area.

Logging

type struct Logging {}

The process-wide logging settings. Until a program changes them, records at Warning and above go to standard error, through a StandardErrorSink.

  • set_level(level) lets records at level and above through from every area no area setting names.
  • set_area_level(area, level) sets the level for area and every area below it: http covers http::server, but not https. When several settings name areas a record's area is below, the longest wins.
  • configure(specification) applies comma-separated entries, each a level name (the default level) or area=level: "information,http=debug". Spaces around an entry and its parts are ignored, and so is an empty entry. Nothing is applied when an entry cannot be read; the error names the first such entry.
  • configure_from_environment(variable) is configure with the value of an environment variable, and changes nothing when it is unset.
  • set_sink(&sink) sends every record from then on to a LogSink of the program's own, and reset_sink() sends them to standard error again. 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.
import std::log::record::{ LogLevel };
import std::log::logger::{ Logger, Logging };

Logging::configure(Str::new("warning,http::server=debug")).unwrap();

const log: Logger = Logger::new(Str::new("http"));
const server: Logger = Logger::new(Str::new("http::server"));
server.is_enabled(LogLevel::Debug);   // true
log.is_enabled(LogLevel::Debug);      // false: `http` has the default, `warning`

Static functions

static set_level(level: LogLevel) -> void;

Let records at level and above through, from every area no area setting names.

static set_area_level(area: Str, level: LogLevel) -> void;

Let records at level and above through from area and every area below it, replacing an earlier setting for the same area.

static configure(specification: Str) -> Result<(), LogSpecificationError>;

Apply a specification of comma-separated entries, each a level name (the default level) or area=level: "information,http=debug". Spaces around an entry and its parts are ignored, and so is an empty entry. Nothing is applied when an entry cannot be read; the error names the first such entry.

static configure_from_environment(variable: Str) -> Result<(), LogSpecificationError>;

configure with the value of the environment variable variable; nothing changes when it is unset.

static set_sink<S>(sink: &S) -> void where S: LogSink, S: Sync;

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

static reset_sink() -> void;

Send records to standard error again, as before any set_sink.

LogSpecificationError

type struct LogSpecificationError with [Eq, Clone, Debug] {
    entry: String;
}

A logging specification entry Logging::configure could not read, as written between commas. It prints as unrecognized logging entry 'http=loud'.

Properties

PropertyTypeNotes
entryStringThe entry, as written between commas.

Trait implementations

implement trait Error for struct LogSpecificationError
implement trait Display for struct LogSpecificationError