Skip to content

Commit 6b3bb6d

Browse files
authored
Merge pull request #53 from borntyping/feature/2.0.0
Version 2.0.0
2 parents 90db900 + ee4c93d commit 6b3bb6d

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

Cargo.toml

Lines changed: 10 additions & 2 deletions
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"
@@ -32,10 +32,18 @@ required-features = ["colors"]
3232
name = "threads"
3333
required-features = ["threads"]
3434

35+
[[example]]
36+
name = "timestamps_local"
37+
required-features = ["timestamps"]
38+
39+
[[example]]
40+
name = "timestamps_none"
41+
required-features = ["timestamps"]
42+
3543
[[example]]
3644
name = "timestamps_utc"
3745
required-features = ["timestamps"]
3846

3947
[[example]]
40-
name = "timestamps_local"
48+
name = "timestamps_utc_offset"
4149
required-features = ["timestamps"]

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:

examples/timestamps_none.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use simple_logger::SimpleLogger;
2+
3+
fn main() {
4+
SimpleLogger::new().without_timestamps().init().unwrap();
5+
6+
log::warn!("This is an example message.");
7+
}

examples/timestamps_utc_offset.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::UtcOffset;
3+
4+
fn main() {
5+
SimpleLogger::new()
6+
.with_utc_offset(UtcOffset::from_hms(14, 0, 0).unwrap())
7+
.init()
8+
.unwrap();
9+
10+
log::warn!("This is an example message using a static UTC offset.");
11+
log::info!("Daylight savings or other timezone changes will not be respected.");
12+
}

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)