Log (std::log)
En esta página
std::log emite mensajes a stderr con nivel de severidad y timestamp ISO 8601
por defecto. Al interactuar directamente con el sistema operativo (escritura en
stderr y, opcionalmente, en archivo), está marcado como | norun en el sandbox
WASM. Ejecuta con zolo run para ver la salida real.
Niveles de log
Hay cuatro niveles en orden creciente de severidad: debug, info, warn y
error. El nivel inicial predeterminado es info, por lo que los mensajes
debug se filtran hasta que el nivel se baje explícitamente. Compón los mensajes
con interpolación de cadenas de la forma habitual.
Emisión de mensajes en cada nivel; debug filtrado por el nivel predeterminado info.
// 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}")
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Control de verbosidad
log.set_level(nivel) ajusta el filtro en tiempo de ejecución. El valor "off"
silencia completamente el log — útil en pruebas. log.get_level() devuelve el
nivel actual como cadena.
Cambio dinámico del nivel y uso de "off" para suprimir toda la salida.
// 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")
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Formato y destino
log.set_format(template) redefine la plantilla usando los marcadores {time},
{level} y {msg}. Pasa nil para restaurar el valor predeterminado.
log.to_file(path) redirige la salida a un archivo; nil vuelve a stderr.
Formato compacto sin timestamp y redirección a archivo.
// 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")
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Consulta también