@std/log.ms#
Import each required symbol with import Name from "@std/log.ms".
LogLevel Enum#
Severity levels accepted by a logger, ordered from Trace through Fatal.
enum LogLevel {
Trace
Debug
Info
Warn
Error
Fatal
Silent
}LogLevel.Debug#
Development diagnostics, encoded with numeric level 20.
DebugLogLevel.Error#
Failure records, encoded with numeric level 50.
ErrorLogLevel.Fatal#
Critical records at numeric level 60; logging does not terminate the process.
FatalLogLevel.Info#
Ordinary operational records, encoded with numeric level 30; the default threshold.
InfoLogLevel.Silent#
Filtering threshold that disables all records; passing it to log returns
EINVAL.
SilentLogLevel.Trace#
Most detailed records, encoded with numeric level 10.
TraceLogLevel.Warn#
Warning records, encoded with numeric level 40.
WarnLogger Type#
A structured logger: level filtering, bound fields, and a pluggable Transport. The
level methods take optional fields of any Data type and encode them with
stringify. A child logger owns a duplicate of the transport (its own handle on the
same destination), so loggers can write from several threads and closing one leaves
the others open.
opaque type Logger {
}Logger.child#
Returns a logger that writes fields under context in each record. A child of a child merges
object members, and the newer value wins on a clash; if either side is not an object, the newer
value replaces the older one. A value that cannot be encoded returns EBADMSG.
fn child[F Data](fields F) Result[Logger, Error]Logger.close#
Closes this logger’s transport and consumes the logger. A scope that owns a logger should close it on every exit. Children and the parent are not affected.
move fn close() Result[Void, Error]Logger.debug#
Logs at Debug when enabled, for development diagnostics. Fields are encoded as in
log.
fn debug[F Data = Void](message String, fields F? = null) Result[Void, Error]Logger.error#
Logs at Error when enabled. This records a failure for diagnosis; it does not
raise a fault. Fields are encoded as in log.
fn error[F Data = Void](message String, fields F? = null) Result[Void, Error]Logger.fatal#
Logs at Fatal when enabled. Logging does not stop the process; the caller must
do that. Fields are encoded as in log.
fn fatal[F Data = Void](message String, fields F? = null) Result[Void, Error]Logger.flush#
Flushes the logger’s transport.
fn flush() Result[Void, Error]Logger.info#
Logs at Info when enabled. Ordinary operational records. Fields are encoded as in
log.
fn info[F Data = Void](message String, fields F? = null) Result[Void, Error]Logger.is_enabled#
Tests whether records at level pass the threshold; Silent is never enabled.
fn is_enabled(level LogLevel) BoolLogger.level#
Returns this logger’s current threshold, independent of its parent or children.
fn level() LogLevelLogger.log#
Writes a record at the selected level. fields is any Data value stringify accepts.
The value is written as is under the record’s data key, so a record, map, list, or number
keeps its shape. No fields, or null, write the message alone. A value that cannot be encoded,
such as a cyclic one, returns EBADMSG; encoding happens only when the level is enabled.
The fields of child loggers are written under context. Silent is a threshold, not a
record level, and returns EINVAL.
fn log[F Data = Void](level LogLevel, message String, fields F? = null) Result[Void, Error]Logger.set_level#
Changes only this logger’s threshold; existing child loggers keep the threshold they copied.
var fn set_level(level LogLevel) VoidLogger.trace#
Logs at Trace, the most detailed level, when enabled. A filtered call returns ok
without encoding the fields or touching the transport. Fields are encoded as in
log.
fn trace[F Data = Void](message String, fields F? = null) Result[Void, Error]Logger.warn#
Logs at Warn when enabled. Returns encoding, size-limit and transport errors
unchanged. Fields are encoded as in log.
fn warn[F Data = Void](message String, fields F? = null) Result[Void, Error]LoggerOptions Type#
Settings for create: the minimum level (default Info), whether records carry a
timestamp (default true), and an optional per-record byte limit.
type LoggerOptions {
level LogLevel = LogLevel.Info
timestamp Bool = true
max_bytes Int? = null
}LoggerOptions.level#
Records below this level are dropped.
level LogLevel = LogLevel.InfoLoggerOptions.max_bytes#
Maximum encoded bytes of one record; null adds no limit.
max_bytes Int? = nullLoggerOptions.timestamp#
True adds a timestamp to every record.
timestamp Bool = trueTransport Interface#
A destination that accepts encoded log records and can be flushed and closed.
interface Transport {
fn write(data String) Result[Void, Error]
fn write_bytes(data Bytes) Result[Void, Error]
fn flush() Result[Void, Error]
fn duplicate() Result[Transport, Error]
move fn close() Void
}Transport.close#
Releases owned resources. Later writes return EBADF.
move fn close() VoidTransport.duplicate#
Returns a new transport onto the same destination with its own handle. Each child logger writes through one.
fn duplicate() Result[Transport, Error]Transport.flush#
Flushes the destination; what that guarantees depends on the destination.
fn flush() Result[Void, Error]Transport.write#
Accepts one encoded record as text and may return the original I/O cause. An
error can follow a partial write to the destination, which is not rolled back.
Each logger owns its transport (see duplicate), so an implementation needs no
locking.
fn write(data String) Result[Void, Error]Transport.write_bytes#
Accepts one encoded record as bytes, with the same rules as write.
fn write_bytes(data Bytes) Result[Void, Error]create Function#
Creates a logger that owns transport, with the level, timestamp and record-size
settings in options.
fn create(move transport Transport, options LoggerOptions = LoggerOptions()) Result[Logger, Error] { ... }file_transport Function#
Appends log records to a file at path.
fn file_transport(path String) Result[Transport, Error] { ... }handle_transport Function#
Appends log records to an open file. The transport owns file; closing the transport
closes it.
fn handle_transport(move file FileHandle) Transport { ... }rotating_file_transport Function#
Appends log records to the file at path and rotates it by size.
When a record would take the file past max_bytes, the file is renamed to path.1, an older path.1 to path.2, and so on up to path.<keep>; the oldest is deleted.
max_bytes must be positive, and a single record larger than it is still written whole, to a fresh file.
keep is the number of rotated files to retain, zero or more; zero discards the old contents.
A non-positive max_bytes or a negative keep returns EINVAL.
An existing file is appended to, and its current size counts toward max_bytes.
Example: create(rotating_file_transport("app.log", max_bytes: 1048576, keep: 5)).
fn rotating_file_transport(path String, max_bytes Int, keep Int) Result[Transport, Error] { ... }stdout_transport Function#
Writes log records to the process standard-output stream.
fn stdout_transport() Transport { ... }