Skip to content

Commit 36f984e

Browse files
committed
Make UTC the default timestamps option, and change the output format to include the timezone
This works around the issue of potentially silently changing the timezone used in logfiles (and not leaving the user any way to notice the change) by changing the timezone format used from one that is timezone agnostic to one that includes the timezone. Since it's alreday changing from one that perfectly matches Supervisord, this uses the well known RFC 3339 format that implements ISO 8601 (and happens to currently be the only format provided by the `time` crate that I don't have to implement myself).
1 parent 90db900 commit 36f984e

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "simple_logger"
3-
version = "1.16.0"
3+
version = "2.0.0"
44
license = "MIT"
55
authors = ["Sam Clements <[email protected]>"]
66
description = "A logger that prints all messages with a readable output format"

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
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](http://supervisord.org/).
5+
The output format is based on the format used by [Supervisord](http://supervisord.org/), with timestamps in [RFC 3339](https://datatracker.ietf.org/doc/html/rfc3339) format.
66

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

11+
Breaking changes
12+
----------------
13+
14+
- **Version 2.0.0 changes the default from displaying timestamps in the local timezone to displaying timestamps in UTC.** See issue [#52](https://github.com/borntyping/rust-simple_logger/issues/52) for more information.
15+
1116
Usage
1217
-----
1318

@@ -24,7 +29,7 @@ fn main() {
2429
This outputs:
2530

2631
```
27-
2015-02-24 01:05:20 WARN [logging_example] This is an example message.
32+
2022-01-19T17:27:07.013874956Z WARN [logging_example] This is an example message.
2833
```
2934

3035
You can run the above example with:

src/lib.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@ use colored::*;
3535
use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};
3636
use std::collections::HashMap;
3737
#[cfg(feature = "timestamps")]
38-
use time::{format_description::FormatItem, OffsetDateTime};
39-
40-
#[cfg(feature = "timestamps")]
41-
const TIMESTAMP_FORMAT: &[FormatItem] = time::macros::format_description!(
42-
"[year]-[month]-[day] [hour]:[minute]:[second],[subsecond digits:3]"
43-
);
38+
use time::{format_description::well_known::Rfc3339, OffsetDateTime, UtcOffset};
4439

4540
#[cfg(feature = "timestamps")]
4641
#[derive(PartialEq)]
4742
enum Timestamps {
4843
None,
4944
Local,
5045
Utc,
46+
UtcOffset(UtcOffset),
5147
}
5248

5349
/// Implements [`Log`] and a set of simple builder methods for configuration.
@@ -105,7 +101,7 @@ impl SimpleLogger {
105101
threads: false,
106102

107103
#[cfg(feature = "timestamps")]
108-
timestamps: Timestamps::Local,
104+
timestamps: Timestamps::Utc,
109105

110106
#[cfg(feature = "colored")]
111107
colors: true,
@@ -292,6 +288,16 @@ impl SimpleLogger {
292288
self
293289
}
294290

291+
/// Display timestamps using a static UTC offset.
292+
///
293+
/// This method is only available if the `timestamps` feature is enabled.
294+
#[must_use = "You must call init() to begin logging"]
295+
#[cfg(feature = "timestamps")]
296+
pub fn with_utc_offset(mut self, offset: UtcOffset) -> SimpleLogger {
297+
self.timestamps = Timestamps::UtcOffset(offset);
298+
self
299+
}
300+
295301
/// Control whether messages are colored or not.
296302
///
297303
/// This method is only available if the `colored` feature is enabled.
@@ -408,13 +414,15 @@ impl Log for SimpleLogger {
408414
Timestamps::None => "".to_string(),
409415
Timestamps::Local => format!("{} ", OffsetDateTime::now_local().expect(concat!(
410416
"Could not determine the UTC offset on this system. ",
417+
"Consider displaying UTC time instead. ",
411418
"Possible causes are that the time crate does not implement \"local_offset_at\" ",
412419
"on your system, or that you are running in a multi-threaded environment and ",
413420
"the time crate is returning \"None\" from \"local_offset_at\" to avoid unsafe ",
414421
"behaviour. See the time crate's documentation for more information. ",
415422
"(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
416-
)).format(&TIMESTAMP_FORMAT).unwrap()),
417-
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&TIMESTAMP_FORMAT).unwrap()),
423+
)).format(&Rfc3339).unwrap()),
424+
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&Rfc3339).unwrap()),
425+
Timestamps::UtcOffset(offset) => format!("{} ", OffsetDateTime::now_utc().to_offset(offset).format(&Rfc3339).unwrap()),
418426
}
419427

420428
#[cfg(not(feature = "timestamps"))]
@@ -478,6 +486,17 @@ pub fn init() -> Result<(), SetLoggerError> {
478486
SimpleLogger::new().init()
479487
}
480488

489+
/// Initialise the logger with it's default configuration.
490+
///
491+
/// Log messages will not be filtered.
492+
/// The `RUST_LOG` environment variable is not used.
493+
///
494+
/// This function is only available if the `timestamps` feature is enabled.
495+
#[cfg(feature = "timestamps")]
496+
pub fn init_utc() -> Result<(), SetLoggerError> {
497+
SimpleLogger::new().with_utc_timestamps().init()
498+
}
499+
481500
/// Initialise the logger with the `RUST_LOG` environment variable.
482501
///
483502
/// Log messages will be filtered based on the `RUST_LOG` environment variable.
@@ -546,7 +565,7 @@ mod test {
546565
#[cfg(feature = "timestamps")]
547566
fn test_timestamps_defaults() {
548567
let builder = SimpleLogger::new();
549-
assert!(builder.timestamps == Timestamps::Local);
568+
assert!(builder.timestamps == Timestamps::Utc);
550569
}
551570

552571
#[test]

0 commit comments

Comments
 (0)