Emitting to the console
You can use emit_term
to write diagnostic events to the console in a human-readable format:
[dependencies.emit_term]
version = "0.11.0-alpha.21"
extern crate emit; extern crate emit_term; fn main() { let rt = emit::setup().emit_to(emit_term::stdout()).init(); // Your app code goes here rt.blocking_flush(std::time::Duration::from_secs(5)); }
See the crate docs for more details.
Format
Events are written with a header containing the timestamp, level, and emitting package name, followed by the rendered message template:
#![allow(unused)] fn main() { extern crate emit; emit::info!("Hello, {user}", user: "Rust"); }
If the event contains an error (the well-known err
property), then it will be formatted as a cause chain after the message:
#![allow(unused)] fn main() { extern crate emit; let err = ""; emit::warn!("writing to {path} failed", path: "./file.txt", err); }
If the event is part of a trace, the trace and span ids will be written in the header with corresponding colored boxes derived from their values:
extern crate emit; fn main() -> Result<(), Box<dyn std::error::Error>> { #[emit::info_span(err_lvl: "warn", "write to {path}")] fn write_to_file(path: &str, data: &[u8]) -> std::io::Result<()> { /* .. */ emit::debug!("wrote {bytes} bytes to the file", bytes: data.len()); Ok(()) } write_to_file("./file.txt", b"Hello")?; Ok(()) }
Writing your own console emitter
The emit_term
source code is written to be hackable. You can take and adapt its source to your needs, or write your own emitter that formats events the way you'd like. See Writing an emitter for details.