Pular para o conteúdo

Log (std::log)

Nesta página

std::log emite mensagens para stderr com nível de severidade e timestamp ISO 8601 por padrão. Por interagir diretamente com o sistema operacional (escrita em stderr e, opcionalmente, em arquivo), é marcado como | norun no sandbox WASM. Execute com zolo run para ver a saída real.


Níveis de log

Há quatro níveis em ordem crescente de severidade: debug, info, warn e error. O nível inicial padrão é info, portanto mensagens debug são filtradas até que o nível seja explicitamente abaixado. Compose mensagens com interpolação de strings normalmente.

Emissão de mensagens em cada nível; debug filtrado pelo nível padrão info.

01-levels.zolo
Playground
// 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}")

Requer a CLI/host do Zolo — abra no playground ou rode localmente.


Controle de verbosidade

log.set_level(nivel) ajusta o filtro em tempo de execução. O valor "off" silencia completamente o log — útil em testes. log.get_level() retorna o nível atual como string.

Alteração dinâmica do nível e uso de "off" para suprimir toda a saída.

02-set-level.zolo
Playground
// 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")

Requer a CLI/host do Zolo — abra no playground ou rode localmente.


Formato e destino

log.set_format(template) redefine o template usando os placeholders {time}, {level} e {msg}. Passe nil para restaurar o padrão. log.to_file(path) redireciona a saída para um arquivo; nil volta ao stderr.

Formato compacto sem timestamp e redirecionamento para arquivo.

03-format.zolo
Playground
// 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")

Requer a CLI/host do Zolo — abra no playground ou rode localmente.

Buscar no Zolo

9 resultados

enespt-br