1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2024-11-24 02:44:17 +00:00
UltimateTrainingModpack/src/logging.rs
asimon-1 77f439a6eb
Improve Error Handling (#459)
* Add error messages by changing unwrap() calls to expect()

* Improve error messages by changing println! to info!/warn!/error!

* Address nits

* Change panic message

* details in panic
2023-01-27 11:19:18 -08:00

50 lines
1.5 KiB
Rust

pub use log::{error, info, warn};
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
use owo_colors::OwoColorize;
struct TrainingModpackLogger;
impl log::Log for TrainingModpackLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Info
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
match record.level() {
Level::Error => {
println!(
"[TrainingModpack] [{}] {}",
record.level().red(),
record.args()
);
}
Level::Warn => {
println!(
"[TrainingModpack] [{}] {}",
record.level().yellow(),
record.args()
);
}
Level::Info => {
println!(
"[TrainingModpack] [{}] {}",
record.level().cyan(),
record.args()
);
}
_ => {
println!("[TrainingModpack] [{}] {}", record.level(), record.args());
}
};
}
}
fn flush(&self) {}
}
static LOGGER: TrainingModpackLogger = TrainingModpackLogger;
pub fn init_logger() -> Result<(), SetLoggerError> {
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Info))
}