kubi/kubi-logging/src/lib.rs

54 lines
1.4 KiB
Rust
Raw Normal View History

2023-02-10 13:44:34 -06:00
/// Custom env_logger options and styling
2023-01-14 18:41:29 -06:00
use env_logger::{fmt::Color, Builder, Env};
use log::Level;
use std::io::Write;
2023-02-10 13:44:34 -06:00
pub use log;
pub use env_logger;
#[inline]
2023-01-14 18:41:29 -06:00
pub fn init() {
let env = Env::default()
.filter_or("RUST_LOG", "trace,gilrs=warn,rusty_xinput=warn");
2023-01-15 11:11:49 -06:00
Builder::from_env(env)
.format(|buf, record| {
let mut level_style = buf.style();
level_style.set_color(match record.level() {
Level::Error => Color::Red,
Level::Warn => Color::Yellow,
_ => Color::Blue
}).set_bold(true);
2023-01-14 18:41:29 -06:00
2023-02-12 14:31:21 -06:00
let mut bold_style = buf.style();
bold_style.set_bold(true);
2023-01-15 11:11:49 -06:00
let mut location_style = buf.style();
location_style.set_bold(true);
location_style.set_dimmed(true);
2023-01-14 18:41:29 -06:00
2023-01-15 11:11:49 -06:00
let mut location_line_style = buf.style();
location_line_style.set_dimmed(true);
2023-02-12 14:31:21 -06:00
let text = format!("{}", record.args());
2023-01-15 11:11:49 -06:00
writeln!(
buf,
2023-02-12 14:31:21 -06:00
"{} {:<50}\t{}{}{}{}",
2023-01-15 11:11:49 -06:00
level_style.value(match record.level() {
Level::Error => "[e]",
Level::Warn => "[w]",
Level::Info => "[i]",
Level::Debug => "[d]",
Level::Trace => "[t]",
}),
2023-02-12 14:31:21 -06:00
text,
bold_style.value((text.len() > 50).then_some("\n ╰─ ").unwrap_or_default()),
2023-01-15 11:11:49 -06:00
location_style.value(record.target()),
location_line_style.value(" :"),
location_line_style.value(record.line().unwrap_or(0))
)
})
.init();
2023-01-14 18:41:29 -06:00
}