feat(logger): ✨ publish log crate utilities
This commit is contained in:
parent
8e10cebaca
commit
baef445169
@ -1,10 +1,10 @@
|
|||||||
use {log::Level, lool::logger::ConsoleLogger};
|
use lool::logger::{debug, error, info, trace, warn, ConsoleLogger, Level};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
ConsoleLogger::default_setup(Level::Trace, "test").unwrap();
|
ConsoleLogger::default_setup(Level::Trace, "test").unwrap();
|
||||||
log::info!("log line");
|
info!("log line");
|
||||||
log::warn!("log line");
|
warn!("log line");
|
||||||
log::error!("log line");
|
error!("log line");
|
||||||
log::debug!("log line");
|
debug!("log line");
|
||||||
log::trace!("log line");
|
trace!("log line");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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:
|
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
|
```rs
|
||||||
use {log::Level, lool::logger::ConsoleLogger};
|
use lool::logger::{ConsoleLogger, Level};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
ConsoleLogger::default_setup(Level::Trace, "my-app").unwrap();
|
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:
|
The logger can be used with the `log` crate. The following code snippet shows how to use the logger:
|
||||||
|
|
||||||
```rs
|
```rs
|
||||||
use log::{info, warn, error, trace, debug};
|
use lool::logger::{info, warn, error, debug, trace};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
info!("This is an info message");
|
info!("This is an info message");
|
||||||
@ -77,7 +77,7 @@ function. The custom function should receive no arguments and return a string wi
|
|||||||
datetime.
|
datetime.
|
||||||
|
|
||||||
```rs
|
```rs
|
||||||
use {log::Level, lool::logger::ConsoleLogger};
|
use lool::logger::{ConsoleLogger, Level};
|
||||||
use custom_implementation::custom_datetime_fn;
|
use custom_implementation::custom_datetime_fn;
|
||||||
|
|
||||||
fn main() {
|
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:
|
The library also provides a convenient function in case we just don't want to display the datetime:
|
||||||
|
|
||||||
```rs
|
```rs
|
||||||
use {log::Level, lool::logger::{ConsoleLogger, datetime::noop_datetime}};
|
use lool::logger::{ConsoleLogger, Level, datetime::noop_datetime};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
ConsoleLogger::custom_setup(Level::Trace, "my-app", noop_datetime).unwrap();
|
ConsoleLogger::custom_setup(Level::Trace, "my-app", noop_datetime).unwrap();
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
pub mod datetime;
|
pub mod datetime;
|
||||||
|
|
||||||
use {
|
use eyre::{eyre, Result};
|
||||||
eyre::{eyre, Result},
|
pub use log::{
|
||||||
log::{Level, Metadata, Record},
|
debug, error, info, set_max_level, trace, warn, Level, LevelFilter, Log, Metadata, Record,
|
||||||
};
|
};
|
||||||
|
|
||||||
const RESET: &str = "\x1b[0m";
|
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> {
|
pub struct ConsoleLogger<'a> {
|
||||||
context: &'a str,
|
context: &'a str,
|
||||||
time_fn: fn() -> String,
|
time_fn: fn() -> String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ConsoleLogger<'a> {
|
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<()> {
|
pub fn default_setup(max_level: Level, context: &'static str) -> Result<()> {
|
||||||
let logger = Box::new(ConsoleLogger {
|
let logger = Box::new(ConsoleLogger {
|
||||||
context,
|
context,
|
||||||
@ -64,6 +72,12 @@ impl<'a> ConsoleLogger<'a> {
|
|||||||
.map_err(|err| eyre!("failed to set logger: {}", err))
|
.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<()> {
|
pub fn setup(max_level: Level, context: &'static str, time_fn: fn() -> String) -> Result<()> {
|
||||||
let logger = Box::new(ConsoleLogger { context, time_fn });
|
let logger = Box::new(ConsoleLogger { context, time_fn });
|
||||||
log::set_logger(Box::leak(logger) as &'static ConsoleLogger)
|
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))
|
.map_err(|err| eyre!("failed to set logger: {}", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 🧉 » sets the logger's context
|
||||||
pub fn set_context(&mut self, context: &'a str) {
|
pub fn set_context(&mut self, context: &'a str) {
|
||||||
self.context = context;
|
self.context = context;
|
||||||
}
|
}
|
||||||
@ -82,6 +97,7 @@ impl<'a> log::Log for ConsoleLogger<'a> {
|
|||||||
metadata.level() <= Level::Info
|
metadata.level() <= Level::Info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// log the record
|
||||||
fn log(&self, record: &Record) {
|
fn log(&self, record: &Record) {
|
||||||
let styled_record = StyledRecord::from(record, self.time_fn);
|
let styled_record = StyledRecord::from(record, self.time_fn);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user