data log
Namespace of the logging functions: log.info("…").
Example:
use std.obs { log }
fn main() io {
let id = 42
log.info("order {id} paid")
print("logged")
}Вывод
logged
Примеры#
Рецепты логирования: уровни, метрики и спаны.
Уровни, метрики, спаны#
Записи log.* идут в stderr; metric.counter/gauge/histogram и span.with дополняют логи.
use std.obs { log, Level, metric, span }
fn handle(id: i64) -> i64 io {
log.info("handling request {id}") // записи идут в stderr, не в stdout
id * 2
}
fn main() io {
log.setLevel(Level.Info)
let served = metric.counter("requests_total", "requests served")
let inflight = metric.gauge("inflight", "requests in flight")
var total = 0
for id in 1..=3 {
inflight.inc()
total += span.with("handle", () => handle(id)) // span измеряет участок и попадает в трассу
served.inc()
inflight.dec()
}
log.debug("not printed: level is Info")
print("total {total}, served {served.value()}, in flight {inflight.value()}")
print("debug enabled: {log.enabled(Level.Debug)}")
}Вывод
total 12, served 3.0, in flight 0.0 debug enabled: false
pub(api) data logПотокобезопасность. safe from any task; records of different tasks may interleave, each one is written whole.
С версии. 1.0.0-beta
Методы
trace | (msg: str) ioVery detailed diagnostics. |
debug | (msg: str) ioDiagnostics for developers. |
info | (msg: str) ioNormal events worth recording. |
warn | (msg: str) ioSomething unexpected that the program handled. |
error | (msg: str) ioAn operation failed. |
at | (level: Level, msg: str) ioLogs at a level chosen at run time (log.at(Level.Warn, "…")). |
level | () -> Level ioCurrent threshold |
setLevel | (level: Level) ioChanges the threshold of this process (the start-up value comes from --log-level / OXL_LOG). |
enabled | (level: Level) -> bool ioTrue when a record at level would be written; guard expensive field computation with it. |
setJson | (on: bool) ioSwitches between text (false) and JSON-lines (true) output. |
isJson | () -> bool ioTrue when the output is JSON lines. |
Определено в: std/obs/obs.oxl:101