numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
602 stars 37 forks source link

Removal of custom log and use env_logger (or similar) instead #196

Closed aldoborrero closed 1 year ago

aldoborrero commented 1 year ago

Rationale

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.

We can achieve the same functionality by instantiating env_logger in the main binary crate, which allows easy customization with its Builder class.

Related features we can tackle whilst implementing this one are also related to bringing colored logs.

brianmcgee commented 1 year ago

Makes sense to me. I don't mind having a look at it, since #191 is a bit blocked at the moment.