feat(logger): publish log crate utilities

This commit is contained in:
Lucas Colombo 2024-03-31 11:23:37 -03:00
parent 8e10cebaca
commit baef445169
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
3 changed files with 29 additions and 13 deletions

View File

@ -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");
}

View File

@ -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();

View File

@ -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);