Ver código fonte

Constant width timestamps

Shows a fixed number of digits in the subsecond fraction in timestamps.

This reverts to using `format_description!`, and will always show UTC
time with an offset of `Z` and local time with an offset of `+/-HH:MM`.
This should still comply with RFC 3339, which specifically calls out
number of digits when talking about ordering timestamp strings.

Closes #55
Sam Clements 3 anos atrás
pai
commit
496d76a5de
2 arquivos alterados com 15 adições e 5 exclusões
  1. 1 1
      Cargo.toml
  2. 14 4
      src/lib.rs

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "simple_logger"
-version = "2.0.0"
+version = "2.1.0"
 license = "MIT"
 authors = ["Sam Clements <sam@borntyping.co.uk>"]
 description = "A logger that prints all messages with a readable output format"

+ 14 - 4
src/lib.rs

@@ -35,7 +35,17 @@ use colored::*;
 use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};
 use std::collections::HashMap;
 #[cfg(feature = "timestamps")]
-use time::{format_description::well_known::Rfc3339, OffsetDateTime, UtcOffset};
+use time::{format_description::FormatItem, OffsetDateTime, UtcOffset};
+
+#[cfg(feature = "timestamps")]
+const TIMESTAMP_FORMAT_OFFSET: &[FormatItem] = time::macros::format_description!(
+    "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3][offset_hour sign:mandatory]:[offset_minute]"
+);
+
+#[cfg(feature = "timestamps")]
+const TIMESTAMP_FORMAT_UTC: &[FormatItem] = time::macros::format_description!(
+    "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"
+);
 
 #[cfg(feature = "timestamps")]
 #[derive(PartialEq)]
@@ -420,9 +430,9 @@ impl Log for SimpleLogger {
                             "the time crate is returning \"None\" from \"local_offset_at\" to avoid unsafe ",
                             "behaviour. See the time crate's documentation for more information. ",
                             "(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
-                        )).format(&Rfc3339).unwrap()),
-                    Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&Rfc3339).unwrap()),
-                    Timestamps::UtcOffset(offset) => format!("{} ", OffsetDateTime::now_utc().to_offset(offset).format(&Rfc3339).unwrap()),
+                        )).format(&TIMESTAMP_FORMAT_OFFSET).unwrap()),
+                    Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&TIMESTAMP_FORMAT_UTC).unwrap()),
+                    Timestamps::UtcOffset(offset) => format!("{} ", OffsetDateTime::now_utc().to_offset(offset).format(&TIMESTAMP_FORMAT_UTC).unwrap()),
                 }
 
                 #[cfg(not(feature = "timestamps"))]