From 3a860aa1d74e5644915ea5096d2ffaec07b9fd08 Mon Sep 17 00:00:00 2001 From: Joao Rafael Date: Mon, 16 Feb 2026 18:41:14 -0300 Subject: [PATCH 1/2] feat: Add support for custom timestamp format with time_format() --- examples/time_format.rs | 15 +++++++++++++++ src/logger.rs | 13 ++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 examples/time_format.rs diff --git a/examples/time_format.rs b/examples/time_format.rs new file mode 100644 index 0000000..db9b7db --- /dev/null +++ b/examples/time_format.rs @@ -0,0 +1,15 @@ +use sheen::{Level, Logger}; + +fn main() { + sheen::init_with( + Logger::new() + .level(Level::Trace) + .time_format("%Y-%m-%d %H:%M:%S"), + ); + + sheen::trace!("starting up"); + sheen::debug!("loading configuration"); + sheen::info!("server listening on port 3000"); + sheen::warn!("cache is nearly full"); + sheen::error!("failed to connect to database"); +} diff --git a/src/logger.rs b/src/logger.rs index e8cb925..ea53e5c 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -10,6 +10,7 @@ pub struct Logger { colorize: bool, fields: Vec<(String, String)>, formatter: Box, + time_format: Option<&'static str>, } impl Logger { @@ -46,6 +47,11 @@ impl Logger { self } + pub fn time_format(mut self, time_format: &'static str) -> Self { + self.time_format = Some(time_format); + self + } + pub fn with(&self, fields: &[(&str, &dyn std::fmt::Debug)]) -> Self { let mut new_fields = self.fields.clone(); for (key, value) in fields { @@ -60,6 +66,7 @@ impl Logger { colorize: std::io::stderr().is_terminal(), fields: new_fields, formatter: Box::new(TextFormatter::new(self.colorize)), + time_format: self.time_format, } } @@ -88,7 +95,10 @@ impl Logger { } let timestamp = if self.show_timestamp { - Some(Local::now().format("%H:%M:%S").to_string()) + match &self.time_format { + Some(fmt) => Some(Local::now().format(fmt).to_string()), + None => Some(Local::now().format("%H:%M:%S").to_string()), + } } else { None }; @@ -115,6 +125,7 @@ impl Default for Logger { colorize: std::io::stderr().is_terminal(), // auto-detect (TTY) fields: Vec::new(), formatter: Box::new(TextFormatter::new(std::io::stderr().is_terminal())), + time_format: None, } } } From 2cf76b2757dca24fe65814c3e6f1ec504192556b Mon Sep 17 00:00:00 2001 From: Joao Rafael Date: Wed, 18 Feb 2026 18:06:06 -0300 Subject: [PATCH 2/2] feat: Make time_format consistent: store as String, accept &str --- src/logger.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/logger.rs b/src/logger.rs index ea53e5c..054fa13 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -10,7 +10,7 @@ pub struct Logger { colorize: bool, fields: Vec<(String, String)>, formatter: Box, - time_format: Option<&'static str>, + time_format: Option, } impl Logger { @@ -47,8 +47,8 @@ impl Logger { self } - pub fn time_format(mut self, time_format: &'static str) -> Self { - self.time_format = Some(time_format); + pub fn time_format(mut self, time_format: &str) -> Self { + self.time_format = Some(time_format.to_string()); self } @@ -66,7 +66,7 @@ impl Logger { colorize: std::io::stderr().is_terminal(), fields: new_fields, formatter: Box::new(TextFormatter::new(self.colorize)), - time_format: self.time_format, + time_format: self.time_format.clone(), } } @@ -95,10 +95,8 @@ impl Logger { } let timestamp = if self.show_timestamp { - match &self.time_format { - Some(fmt) => Some(Local::now().format(fmt).to_string()), - None => Some(Local::now().format("%H:%M:%S").to_string()), - } + let fmt = self.time_format.as_deref().unwrap_or("%H:%M:%S"); + Some(Local::now().format(fmt).to_string()) } else { None };