In treefmt, there's a custom instantiation of a CustomLog like the below code snippet:
pub struct CustomLog {}
impl log::Log for CustomLog {
fn enabled(&self, _metadata: &Metadata) -> bool {
// The log crate already has log::set_max_level to filter out the logs.
// We don't need more than that.
true
}
fn log(&self, record: &Record) {
match record.level() {
Level::Trace => eprintln!("{}: {}", style("[DEBUG]").bold().dim(), record.args()),
Level::Debug => eprintln!("{}: {}", style("[DEBUG]").bold().dim(), record.args()),
Level::Info => eprintln!("{}: {}", style("[INFO]").bold().dim(), record.args()),
Level::Warn => eprintln!("{}: {}", style("[WARN]").bold().dim(), record.args()),
Level::Error => eprintln!("{}: {}", style("[ERR]").bold().dim(), record.args()),
}
}
// ignore, stderr is already flushed by default
fn flush(&self) {}
}
The library crate should be logger agnostic, delegating the task of instantiating the logger implementation to the binary crate or, if treefmt is used as a library, to the implementor.
Rationale
In
treefmt
, there's a custom instantiation of aCustomLog
like the below code snippet:The library crate should be logger agnostic, delegating the task of instantiating the logger implementation to the binary crate or, if
treefmt
is used as a library, to the implementor.We can achieve the same functionality by instantiating
env_logger
in the main binary crate, which allows easy customization with itsBuilder
class.Related features we can tackle whilst implementing this one are also related to bringing colored logs.