Skip to content

Commit c32992b

Browse files
authored
Merge pull request #79 from ctaoist/main
Customizable timestamps format
2 parents 7446241 + 0939c3d commit c32992b

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A logger that prints all messages with a readable output format.
44

5-
The output format is based on the format used by [Supervisord](https://github.com/Supervisor/supervisor), with timestamps in [RFC 3339](https://datatracker.ietf.org/doc/html/rfc3339) format.
5+
The output format is based on the format used by [Supervisord](https://github.com/Supervisor/supervisor), with timestamps in default [RFC 3339](https://datatracker.ietf.org/doc/html/rfc3339) format and custom format.
66

77
* [Source on GitHub](https://github.com/borntyping/rust-simple_logger)
88
* [Packages on Crates.io](https://crates.io/crates/simple_logger)

examples/timestamps_format.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use simple_logger::SimpleLogger;
2+
use time::macros::format_description;
3+
4+
fn main() {
5+
SimpleLogger::new()
6+
.env()
7+
.with_custom_timestamps(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
8+
.init()
9+
.unwrap();
10+
11+
log::warn!("This is an example message with custom timestamp format.");
12+
}

src/lib.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub struct SimpleLogger {
8181
/// This field is only available if the `timestamps` feature is enabled.
8282
#[cfg(feature = "timestamps")]
8383
timestamps: Timestamps,
84+
#[cfg(feature = "timestamps")]
85+
timeformat: &'static [FormatItem<'static>],
8486

8587
/// Whether to use color output or not.
8688
///
@@ -111,6 +113,8 @@ impl SimpleLogger {
111113

112114
#[cfg(feature = "timestamps")]
113115
timestamps: Timestamps::Utc,
116+
#[cfg(feature = "timestamps")]
117+
timeformat: time::macros::format_description!(""),
114118

115119
#[cfg(feature = "colored")]
116120
colors: true,
@@ -265,6 +269,26 @@ impl SimpleLogger {
265269
self
266270
}
267271

272+
/// Custom timestamps format
273+
///
274+
/// The syntax for the format_description macro can be found in the
275+
/// [`time` crate book](https://time-rs.github.io/book/api/format-description.html).
276+
///
277+
/// ```
278+
/// simple_logger::SimpleLogger::new()
279+
/// .with_level(log::LevelFilter::Debug)
280+
/// .env()
281+
/// .with_custom_timestamps(time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
282+
/// .init()
283+
/// .unwrap();
284+
/// ```
285+
#[must_use = "You must call init() to begin logging"]
286+
#[cfg(feature = "timestamps")]
287+
pub fn with_custom_timestamps(mut self, timeformat: &'static [FormatItem<'static>]) -> SimpleLogger {
288+
self.timeformat = timeformat;
289+
self
290+
}
291+
268292
/// Don't display any timestamps.
269293
///
270294
/// This method is only available if the `timestamps` feature is enabled.
@@ -321,6 +345,17 @@ impl SimpleLogger {
321345
#[cfg(all(windows, feature = "colored"))]
322346
set_up_color_terminal();
323347

348+
// Set default timestamp format
349+
#[cfg(feature = "timestamps")]
350+
if self.timeformat.len() <= 0 {
351+
self.timeformat = match self.timestamps {
352+
Timestamps::Local => TIMESTAMP_FORMAT_OFFSET,
353+
Timestamps::Utc => TIMESTAMP_FORMAT_UTC,
354+
Timestamps::UtcOffset(_) => TIMESTAMP_FORMAT_OFFSET,
355+
_ => self.timeformat,
356+
};
357+
}
358+
324359
/* Sort all module levels from most specific to least specific. The length of the module
325360
* name is used instead of its actual depth to avoid module name parsing.
326361
*/
@@ -426,15 +461,15 @@ impl Log for SimpleLogger {
426461
"behaviour. See the time crate's documentation for more information. ",
427462
"(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
428463
))
429-
.format(&TIMESTAMP_FORMAT_OFFSET)
464+
.format(&self.timeformat)
430465
.unwrap()
431466
),
432-
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&TIMESTAMP_FORMAT_UTC).unwrap()),
467+
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&self.timeformat).unwrap()),
433468
Timestamps::UtcOffset(offset) => format!(
434469
"{} ",
435470
OffsetDateTime::now_utc()
436471
.to_offset(offset)
437-
.format(&TIMESTAMP_FORMAT_OFFSET)
472+
.format(&self.timeformat)
438473
.unwrap()
439474
),
440475
}

0 commit comments

Comments
 (0)