Browse Source

1. added examples/timestamps_format.rs
2. could customize format for utc timestamps

ctaoist 2 years ago
parent
commit
5998a66006
2 changed files with 24 additions and 2 deletions
  1. 12 0
      examples/timestamps_format.rs
  2. 12 2
      src/lib.rs

+ 12 - 0
examples/timestamps_format.rs

@@ -0,0 +1,12 @@
+use simple_logger::SimpleLogger;
+use time::macros::format_description;
+
+fn main() {
+    SimpleLogger::new()
+        .env()
+        .with_custom_timestamps(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
+        .init()
+        .unwrap();
+
+    log::warn!("This is an example message with custom timestamp format.");
+}

+ 12 - 2
src/lib.rs

@@ -114,7 +114,7 @@ impl SimpleLogger {
             #[cfg(feature = "timestamps")]
             timestamps: Timestamps::Utc,
             #[cfg(feature = "timestamps")]
-            timeformat: TIMESTAMP_FORMAT_OFFSET,
+            timeformat: time::macros::format_description!(""),
 
             #[cfg(feature = "colored")]
             colors: true,
@@ -345,6 +345,16 @@ impl SimpleLogger {
         #[cfg(all(windows, feature = "colored"))]
         set_up_color_terminal();
 
+        // Set default timestamp format
+        if self.timeformat.len() <= 0 {
+            self.timeformat = match self.timestamps {
+                Timestamps::Local => TIMESTAMP_FORMAT_OFFSET,
+                Timestamps::Utc => TIMESTAMP_FORMAT_UTC,
+                Timestamps::UtcOffset(_) => TIMESTAMP_FORMAT_OFFSET,
+                _ => self.timeformat,
+            };
+        }
+
         /* Sort all module levels from most specific to least specific. The length of the module
          * name is used instead of its actual depth to avoid module name parsing.
          */
@@ -453,7 +463,7 @@ impl Log for SimpleLogger {
                             .format(&self.timeformat)
                             .unwrap()
                     ),
-                    Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&TIMESTAMP_FORMAT_UTC).unwrap()),
+                    Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&self.timeformat).unwrap()),
                     Timestamps::UtcOffset(offset) => format!(
                         "{} ",
                         OffsetDateTime::now_utc()