Skip to content

Log (std::log)

std::log emits messages to stderr with a severity level and an ISO 8601 timestamp by default. Because it interacts directly with the operating system (writing to stderr and optionally to a file), it is marked as | norun in the WASM sandbox. Run with zolo run to see the actual output.


Log levels

There are four levels in ascending order of severity: debug, info, warn and error. The default initial level is info, so debug messages are filtered out until the level is explicitly lowered. Compose messages with string interpolation as usual.

Emitting messages at each level; debug filtered by the default info level.

01-levels.zolo
// Feature: log.debug/info/warn/error — structured logging to stderr
// When to use: trace execution in production. Output formatted as
// `[ISO8601] LEVEL message` by default.

use std::log

// Each level prints to stderr (only if >= current level).
log.debug("internal detail")  // (filtered by default — initial level: info)
log.info("application started")  // expected: [...] INFO application started
log.warn("config without default value")  // expected: [...] WARN ...
log.error("failed to connect to database")  // expected: [...] ERROR ...

// Messages are usually strings — compose with interpolation.
let user_id = 42
log.info("user logged in: id={user_id}")

Requires the Zolo CLI/host — open in the playground or run locally.


Verbosity control

log.set_level(level) adjusts the filter at runtime. The value "off" silences the log entirely — useful in tests. log.get_level() returns the current level as a string.

Dynamic level change and using "off" to suppress all output.

02-set-level.zolo
// Feature: log.set_level / log.get_level — control verbosity
// When to use: silence messages in production, see everything in development.
// Levels (ascending): debug < info < warn < error < off.

use std::log

print(log.get_level())  // expected: info (default)

// Raise the level to 'warn' — debug and info are now ignored.
log.set_level("warn")
log.debug("does not appear")
log.info("also does not appear")
log.warn("this one appears")  // expected: WARN
log.error("and this one too")  // expected: ERROR

// Back to everything.
log.set_level("debug")
log.debug("now it appears")  // expected: DEBUG

// 'off' silences completely.
log.set_level("off")
log.error("ignored")

Requires the Zolo CLI/host — open in the playground or run locally.


Format and destination

log.set_format(template) redefines the template using the placeholders {time}, {level} and {msg}. Pass nil to restore the default. log.to_file(path) redirects output to a file; nil returns to stderr.

Compact format without timestamp and redirection to a file.

03-format.zolo
// Feature: log.set_format / log.to_file — customize output
// When to use: integrate with aggregators (Elastic, Datadog), redirect to file.

use std::log

// Custom format uses placeholders: \{time\}, \{level\}, \{msg\}.
// (The \{ and \} here are just string escapes; the log module receives `{level}: {msg}`.)
log.set_format("\{level\}: \{msg\}")
log.info("compact mode")  // expected: INFO: compact mode

// Back to the default format by passing nil.
log.set_format(nil)
log.info("default is back")  // expected: [ISO8601] INFO default is back

// log.to_file(path) redirects to file (nil = back to stderr).
// Useful in headless servers.
//   log.to_file("/var/log/app.log")
//   log.to_file(nil)
print("log.to_file available")

Requires the Zolo CLI/host — open in the playground or run locally.

enespt-br