From 916c82debc029bd3a8b64555daa30c6213dead3c Mon Sep 17 00:00:00 2001 From: Franklin Date: Mon, 29 Aug 2022 11:40:01 -0400 Subject: [PATCH] Added another init method for logger --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/utils/logger_util.rs | 19 +++++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 838b82b..8e955c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,7 +183,7 @@ dependencies = [ [[package]] name = "actix-web-utils" -version = "0.1.4" +version = "0.1.5" dependencies = [ "actix-web", "log", diff --git a/Cargo.toml b/Cargo.toml index 482ee6e..4c994cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web-utils" -version = "0.1.4" +version = "0.1.5" edition = "2021" authors = ["Franklin E. Blanco"] description = "Just some useful addons for actix web." diff --git a/src/utils/logger_util.rs b/src/utils/logger_util.rs index 6a3f684..1eb8d18 100644 --- a/src/utils/logger_util.rs +++ b/src/utils/logger_util.rs @@ -4,7 +4,22 @@ use crate::extensions::logger::SimpleLogger; static LOGGER: SimpleLogger = SimpleLogger; -pub fn init(max_log_level_filter: LevelFilter) -> Result<(), SetLoggerError> { +pub fn init_logger_default() -> Result<(), SetLoggerError> { log::set_logger(&LOGGER) - .map(|()| log::set_max_level(max_log_level_filter)) + .map(|()| log::set_max_level(LevelFilter::Info)) +} +/// Made this get an integer so that you don't have to import the Log library in your code too, as the LevelFilter enum is inside it. +/// 0 -> Off, 1 -> Trace, 2 -> Debug, 3 -> Info, 4 -> Warn, 5 -> Error +pub fn init_logger_custom(max_log_level_filter: i8) -> Result<(), SetLoggerError> { + let level_filter: LevelFilter = match max_log_level_filter { + 0 => LevelFilter::Off, + 1 => LevelFilter::Trace, + 2 => LevelFilter::Debug, + 3 => LevelFilter::Info, + 4 => LevelFilter::Warn, + 5 => LevelFilter::Error, + _ => panic!("INVALID max_log_level_filter. Only values between 0-5 are allowed."), + }; + log::set_logger(&LOGGER) + .map(|()| log::set_max_level(level_filter)) } \ No newline at end of file