Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/time_format.rs
Original file line number Diff line number Diff line change
@@ -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");
}
11 changes: 10 additions & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Logger {
colorize: bool,
fields: Vec<(String, String)>,
formatter: Box<dyn Formatter>,
time_format: Option<String>,
}

impl Logger {
Expand Down Expand Up @@ -46,6 +47,11 @@ impl Logger {
self
}

pub fn time_format(mut self, time_format: &str) -> Self {
self.time_format = Some(time_format.to_string());
self
}

pub fn with(&self, fields: &[(&str, &dyn std::fmt::Debug)]) -> Self {
let mut new_fields = self.fields.clone();
for (key, value) in fields {
Expand All @@ -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.clone(),
}
}

Expand Down Expand Up @@ -88,7 +95,8 @@ impl Logger {
}

let timestamp = if self.show_timestamp {
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
};
Expand All @@ -115,6 +123,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,
}
}
}
Expand Down