Skip to content

Instantly share code, notes, and snippets.

@kwdowicz
Created May 11, 2024 21:18
Show Gist options
  • Save kwdowicz/2317f5939ef9d20eb9008070e45aa9e3 to your computer and use it in GitHub Desktop.
Save kwdowicz/2317f5939ef9d20eb9008070e45aa9e3 to your computer and use it in GitHub Desktop.
Basic but cool Rust logging
[dependencies]
chrono = "0.4.38"
fern = "0.6.2"
log = "0.4.21"
use log::info;
extern crate fern;
extern crate log;
extern crate chrono;
fn main() {
setup_logging().expect("Failed to initialize logging.");
info!("Started DockerFace");
}
fn setup_logging() -> Result<(), fern::InitError> {
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
record.target(),
record.level(),
message
))
})
.level(log::LevelFilter::Info)
.chain(fern::log_file("output.log")?)
.chain(std::io::stdout())
.apply()?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment