32 lines
919 B
Rust
32 lines
919 B
Rust
|
|
use anyhow::Result;
|
||
|
|
use fern::colors::{Color, ColoredLevelConfig};
|
||
|
|
|
||
|
|
pub fn setup_logger() -> Result<()> {
|
||
|
|
let colors = ColoredLevelConfig::new()
|
||
|
|
.info(Color::Green)
|
||
|
|
.error(Color::Red)
|
||
|
|
.warn(Color::Yellow)
|
||
|
|
.debug(Color::Blue);
|
||
|
|
|
||
|
|
fern::Dispatch::new()
|
||
|
|
// Perform allocation-free log formatting
|
||
|
|
.format(move |out, message, record| {
|
||
|
|
out.finish(format_args!(
|
||
|
|
"[{} {} {}] {}",
|
||
|
|
humantime::format_rfc3339_millis(std::time::SystemTime::now()),
|
||
|
|
colors.color(record.level()),
|
||
|
|
record.target(),
|
||
|
|
message
|
||
|
|
))
|
||
|
|
})
|
||
|
|
// Add blanket level filter -
|
||
|
|
.level(log::LevelFilter::Debug)
|
||
|
|
// - and per-module overrides
|
||
|
|
.chain(std::io::stdout())
|
||
|
|
.chain(fern::log_file("output.log")?)
|
||
|
|
// Apply globally
|
||
|
|
.apply()?;
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|