-
Notifications
You must be signed in to change notification settings - Fork 25
feat: support customizable timestamp format function #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| pub extern crate colored; | ||
| pub extern crate jiff; | ||
|
|
||
| use std::fmt::Write; | ||
|
|
||
| use colored::Color; | ||
| use colored::ColoredString; | ||
| use colored::Colorize; | ||
|
|
@@ -61,11 +63,23 @@ use logforth_core::record::Record; | |
| /// | ||
| /// let layout = TextLayout::default(); | ||
| /// ``` | ||
| #[derive(Debug, Clone, Default)] | ||
| #[derive(Debug, Clone)] | ||
| pub struct TextLayout { | ||
| colors: LevelColor, | ||
| no_color: bool, | ||
| tz: Option<TimeZone>, | ||
| timezone: TimeZone, | ||
| timestamp_format: Option<fn(Timestamp, &TimeZone) -> String>, | ||
| } | ||
|
|
||
| impl Default for TextLayout { | ||
| fn default() -> Self { | ||
| Self { | ||
| colors: LevelColor::default(), | ||
| no_color: false, | ||
| timezone: TimeZone::system(), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope avoiding calling |
||
| timestamp_format: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TextLayout { | ||
|
|
@@ -125,6 +139,8 @@ impl TextLayout { | |
|
|
||
| /// Set the timezone for timestamps. | ||
| /// | ||
| /// Defaults to the system timezone if not set. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
|
|
@@ -134,7 +150,29 @@ impl TextLayout { | |
| /// let layout = TextLayout::default().timezone(TimeZone::UTC); | ||
| /// ``` | ||
| pub fn timezone(mut self, tz: TimeZone) -> Self { | ||
| self.tz = Some(tz); | ||
| self.timezone = tz; | ||
| self | ||
| } | ||
|
|
||
| /// Set a user-defined timestamp format function. | ||
| /// | ||
| /// Default to formatting the timestamp with offset as ISO 8601. See the example below. | ||
tisonkun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// For other formatting options, refer to the [jiff::fmt::strtime] documentation. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use jiff::Timestamp; | ||
| /// use jiff::tz::TimeZone; | ||
| /// use logforth_layout_text::TextLayout; | ||
| /// | ||
| /// // This is equivalent to the default timestamp format. | ||
| /// let layout = TextLayout::default() | ||
| /// .timestamp_format(|ts, tz| format!("{:.6}", ts.display_with_offset(tz.to_offset(ts)))); | ||
| /// ``` | ||
| pub fn timestamp_format(mut self, format: fn(Timestamp, &TimeZone) -> String) -> Self { | ||
| self.timestamp_format = Some(format); | ||
| self | ||
| } | ||
|
|
||
|
|
@@ -157,24 +195,34 @@ impl Visitor for KvWriter { | |
| } | ||
| } | ||
|
|
||
| fn default_timestamp_format(ts: Timestamp, tz: &TimeZone) -> String { | ||
| let offset = tz.to_offset(ts); | ||
| format!("{:.6}", ts.display_with_offset(offset)) | ||
| } | ||
|
|
||
| impl Layout for TextLayout { | ||
| fn format(&self, record: &Record, diags: &[Box<dyn Diagnostic>]) -> Result<Vec<u8>, Error> { | ||
| // SAFETY: jiff::Timestamp::try_from only fails if the time is out of range, which is | ||
| // very unlikely if the system clock is correct. | ||
| let ts = Timestamp::try_from(record.time()).unwrap(); | ||
| let tz = self.tz.clone().unwrap_or_else(TimeZone::system); | ||
| let offset = tz.to_offset(ts); | ||
| let time = ts.display_with_offset(offset); | ||
| let time = if let Some(format) = self.timestamp_format { | ||
| format(ts, &self.timezone) | ||
| } else { | ||
| default_timestamp_format(ts, &self.timezone) | ||
| }; | ||
|
|
||
| let level = self.format_record_level(record.level()); | ||
| let target = record.target(); | ||
| let file = record.filename(); | ||
| let line = record.line().unwrap_or_default(); | ||
| let message = record.payload(); | ||
|
|
||
| let mut visitor = KvWriter { | ||
| text: format!("{time:.6} {level:>6} {target}: {file}:{line} {message}"), | ||
| }; | ||
| let mut visitor = KvWriter { text: time }; | ||
| write!( | ||
| &mut visitor.text, | ||
| " {level:>6} {target}: {file}:{line} {message}" | ||
| ) | ||
| .unwrap(); | ||
| record.key_values().visit(&mut visitor)?; | ||
| for d in diags { | ||
| d.visit(&mut visitor)?; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.