diff --git a/examples/logger.rs b/examples/logger.rs index f3c9728..4c7d278 100644 --- a/examples/logger.rs +++ b/examples/logger.rs @@ -1,10 +1,10 @@ -use {log::Level, lool::logger::ConsoleLogger}; +use lool::logger::{debug, error, info, trace, warn, ConsoleLogger, Level}; fn main() { ConsoleLogger::default_setup(Level::Trace, "test").unwrap(); - log::info!("log line"); - log::warn!("log line"); - log::error!("log line"); - log::debug!("log line"); - log::trace!("log line"); + info!("log line"); + warn!("log line"); + error!("log line"); + debug!("log line"); + trace!("log line"); } diff --git a/lib/logger/README.md b/lib/logger/README.md index c5e26f6..582fd81 100644 --- a/lib/logger/README.md +++ b/lib/logger/README.md @@ -24,7 +24,7 @@ cargo add lool --registry=lugit The logger must be initialized before it can be used. The following code snippet shows how to initialize the logger with the default settings: ```rs -use {log::Level, lool::logger::ConsoleLogger}; +use lool::logger::{ConsoleLogger, Level}; fn main() { ConsoleLogger::default_setup(Level::Trace, "my-app").unwrap(); @@ -42,7 +42,7 @@ The `default_setup` function takes two arguments: The logger can be used with the `log` crate. The following code snippet shows how to use the logger: ```rs -use log::{info, warn, error, trace, debug}; +use lool::logger::{info, warn, error, debug, trace}; fn main() { info!("This is an info message"); @@ -77,7 +77,7 @@ function. The custom function should receive no arguments and return a string wi datetime. ```rs -use {log::Level, lool::logger::ConsoleLogger}; +use lool::logger::{ConsoleLogger, Level}; use custom_implementation::custom_datetime_fn; fn main() { @@ -88,7 +88,7 @@ fn main() { The library also provides a convenient function in case we just don't want to display the datetime: ```rs -use {log::Level, lool::logger::{ConsoleLogger, datetime::noop_datetime}}; +use lool::logger::{ConsoleLogger, Level, datetime::noop_datetime}; fn main() { ConsoleLogger::custom_setup(Level::Trace, "my-app", noop_datetime).unwrap(); diff --git a/lib/logger/mod.rs b/lib/logger/mod.rs index 4bf5451..31d1f80 100644 --- a/lib/logger/mod.rs +++ b/lib/logger/mod.rs @@ -1,8 +1,8 @@ pub mod datetime; -use { - eyre::{eyre, Result}, - log::{Level, Metadata, Record}, +use eyre::{eyre, Result}; +pub use log::{ + debug, error, info, set_max_level, trace, warn, Level, LevelFilter, Log, Metadata, Record, }; const RESET: &str = "\x1b[0m"; @@ -48,12 +48,20 @@ impl StyledRecord { } } +/// 🧉 » simple console logger implementation +/// -- +/// +/// this is a simple logger implementation (mounted on top of the `log` crate) that logs to stdout +/// with ANSI colors and datetime stamps. pub struct ConsoleLogger<'a> { context: &'a str, time_fn: fn() -> String, } impl<'a> ConsoleLogger<'a> { + /// **🧉 » sets up the logger with the default settings** + /// + /// sets the logger to use the `datetime::utc_current_time` function to get the current time. pub fn default_setup(max_level: Level, context: &'static str) -> Result<()> { let logger = Box::new(ConsoleLogger { context, @@ -64,6 +72,12 @@ impl<'a> ConsoleLogger<'a> { .map_err(|err| eyre!("failed to set logger: {}", err)) } + /// **🧉 » sets up the logger with the given settings** + /// + /// the `time_fn` parameter should be a function that returns a string representation of the + /// current time. + /// + /// the default `time_fn` is `datetime::utc_current_time` and it doesn't take TZ into account. pub fn setup(max_level: Level, context: &'static str, time_fn: fn() -> String) -> Result<()> { let logger = Box::new(ConsoleLogger { context, time_fn }); log::set_logger(Box::leak(logger) as &'static ConsoleLogger) @@ -71,6 +85,7 @@ impl<'a> ConsoleLogger<'a> { .map_err(|err| eyre!("failed to set logger: {}", err)) } + /// 🧉 » sets the logger's context pub fn set_context(&mut self, context: &'a str) { self.context = context; } @@ -82,6 +97,7 @@ impl<'a> log::Log for ConsoleLogger<'a> { metadata.level() <= Level::Info } + /// log the record fn log(&self, record: &Record) { let styled_record = StyledRecord::from(record, self.time_fn);