Skip to content

Commit 336f8ca

Browse files
authored
appender: replace chrono with time (#1652)
## Motivation This PR continues the work started in #1646 to replace `chrono` with `time`. I'll refer to @hawkw's motivation: > Currently, `tracing-subscriber` supports the `chrono` crate for > timestamp formatting, via a default-on feature flag. When this code > was initially added to `tracing-subscriber`, the `time` crate did not > have support for the timestamp formatting options we needed. > > Unfortunately, the `chrono` crate's maintenance status is now in > question (see #1598). Furthermore, `chrono` depends on version 0.1 of > the `time` crate, which contains a security vulnerability > (https://rustsec.org/advisories/RUSTSEC-2020-0071.html). This > vulnerability is fixed in more recent releases of `time`, but `chrono` > still uses v0.1. ## Solution I've replaced chrono with time 0.3. Unfortunately, some of chrono's builders for DateTimes are not present in `time`, which required the usage of `macro_rules!` macros to construct some of the hard-coded times. I also took the opportunity to tidy some of the tests and change the internal representation of `Rotation::NEVER` from year 9,999 to an `Option::None`. This branch changes `tracing-appender`'s MSRV from Rust 1.42 to Rust 1.51, the MSRV for the `time` crate when certain required feature flags are enabled. This does *not* effect the MSRV for other crates in this repository.
1 parent 6aafe52 commit 336f8ca

File tree

6 files changed

+147
-210
lines changed

6 files changed

+147
-210
lines changed

.github/workflows/CI.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,24 @@ jobs:
4242
uses: actions-rs/cargo@v1
4343
with:
4444
command: check
45-
args: --all
45+
args: --all --exclude=tracing-appender
46+
47+
# TODO: remove this once tracing's MSRV is bumped.
48+
check-msrv-appender:
49+
# Run `cargo check` on our minimum supported Rust version (1.51.0).
50+
runs-on: ubuntu-latest
51+
steps:
52+
- uses: actions/checkout@main
53+
- uses: actions-rs/toolchain@v1
54+
with:
55+
toolchain: 1.51.0
56+
profile: minimal
57+
override: true
58+
- name: Check
59+
uses: actions-rs/cargo@v1
60+
with:
61+
command: check
62+
args: --lib=tracing-appender
4663

4764
check:
4865
# Run `cargo check` first to ensure that the pushed code at least compiles.

tracing-appender/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ edition = "2018"
2121

2222
[dependencies]
2323
crossbeam-channel = "0.5.0"
24-
chrono = { version = "0.4.16", default-features = false, features = ["clock", "std"] }
24+
time = { version = "0.3", default-features = false, features = ["formatting"] }
2525

2626
[dependencies.tracing-subscriber]
2727
path = "../tracing-subscriber"
2828
version = "0.3"
2929
default-features = false
30-
features = ["fmt"]
30+
features = ["fmt", "std"]
3131

3232
[dev-dependencies]
3333
tracing = { path = "../tracing", version = "0.2" }
34+
time = { version = "0.3", default-features = false, features = ["formatting", "parsing"] }
3435
tempfile = "3"

tracing-appender/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ allows events and spans to be recorded in a non-blocking manner through a
3636
dedicated logging thread. It also provides a [`RollingFileAppender`][file_appender]
3737
that can be used with _or_ without the non-blocking writer.
3838

39-
*Compiler support: [requires `rustc` 1.42+][msrv]*
39+
*Compiler support: [requires `rustc` 1.51+][msrv]*
4040

4141
[msrv]: #supported-rust-versions
4242

@@ -145,17 +145,17 @@ fn main() {
145145

146146
## Supported Rust Versions
147147

148-
Tracing is built against the latest stable release. The minimum supported
149-
version is 1.42. The current Tracing version is not guaranteed to build on Rust
150-
versions earlier than the minimum supported version.
148+
`tracing-appender` is built against the latest stable release. The minimum supported
149+
version is 1.51. The current `tracing-appender` version is not guaranteed to build on
150+
Rust versions earlier than the minimum supported version.
151151

152152
Tracing follows the same compiler support policies as the rest of the Tokio
153153
project. The current stable Rust compiler and the three most recent minor
154-
versions before it will always be supported. For example, if the current stable
155-
compiler version is 1.45, the minimum supported version will not be increased
156-
past 1.42, three minor versions prior. Increasing the minimum supported compiler
157-
version is not considered a semver breaking change as long as doing so complies
158-
with this policy.
154+
versions before it will always be supported. For example, if the current
155+
stable compiler version is 1.45, the minimum supported version will not be
156+
increased past 1.42, three minor versions prior. Increasing the minimum
157+
supported compiler version is not considered a semver breaking change as
158+
long as doing so complies with this policy.
159159

160160
## License
161161

tracing-appender/src/inner.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ use std::io::{BufWriter, Write};
22
use std::{fs, io};
33

44
use crate::rolling::Rotation;
5-
use chrono::prelude::*;
65
use std::fmt::Debug;
76
use std::fs::{File, OpenOptions};
87
use std::path::Path;
8+
use time::OffsetDateTime;
99

1010
#[derive(Debug)]
1111
pub(crate) struct InnerAppender {
1212
log_directory: String,
1313
log_filename_prefix: String,
1414
writer: BufWriter<File>,
15-
next_date: DateTime<Utc>,
15+
next_date: Option<OffsetDateTime>,
1616
rotation: Rotation,
1717
}
1818

1919
impl io::Write for InnerAppender {
2020
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
21-
let now = Utc::now();
21+
let now = OffsetDateTime::now_utc();
2222
self.write_timestamped(buf, now)
2323
}
2424

@@ -32,7 +32,7 @@ impl InnerAppender {
3232
log_directory: &Path,
3333
log_filename_prefix: &Path,
3434
rotation: Rotation,
35-
now: DateTime<Utc>,
35+
now: OffsetDateTime,
3636
) -> io::Result<Self> {
3737
let log_directory = log_directory.to_str().unwrap();
3838
let log_filename_prefix = log_filename_prefix.to_str().unwrap();
@@ -49,15 +49,15 @@ impl InnerAppender {
4949
})
5050
}
5151

52-
fn write_timestamped(&mut self, buf: &[u8], date: DateTime<Utc>) -> io::Result<usize> {
52+
fn write_timestamped(&mut self, buf: &[u8], date: OffsetDateTime) -> io::Result<usize> {
5353
// Even if refresh_writer fails, we still have the original writer. Ignore errors
5454
// and proceed with the write.
5555
let buf_len = buf.len();
5656
self.refresh_writer(date);
5757
self.writer.write_all(buf).map(|_| buf_len)
5858
}
5959

60-
fn refresh_writer(&mut self, now: DateTime<Utc>) {
60+
fn refresh_writer(&mut self, now: OffsetDateTime) {
6161
if self.should_rollover(now) {
6262
let filename = self.rotation.join_date(&self.log_filename_prefix, &now);
6363

@@ -75,8 +75,12 @@ impl InnerAppender {
7575
}
7676
}
7777

78-
fn should_rollover(&self, date: DateTime<Utc>) -> bool {
79-
date >= self.next_date
78+
fn should_rollover(&self, date: OffsetDateTime) -> bool {
79+
// the `None` case means that the `InnerAppender` *never* rorates log files.
80+
match self.next_date {
81+
None => false,
82+
Some(next_date) => date >= next_date,
83+
}
8084
}
8185
}
8286

tracing-appender/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! a dedicated logging thread. It also provides a [`RollingFileAppender`][file_appender] that can
88
//! be used with _or_ without the non-blocking writer.
99
//!
10-
//! *Compiler support: [requires `rustc` 1.42+][msrv]*
10+
//! *Compiler support: [requires `rustc` 1.51+][msrv]*
1111
//!
1212
//! [msrv]: #supported-rust-versions
1313
//! [file_appender]: rolling::RollingFileAppender
@@ -109,8 +109,8 @@
109109
//!
110110
//! ## Supported Rust Versions
111111
//!
112-
//! Tracing is built against the latest stable release. The minimum supported
113-
//! version is 1.42. The current Tracing version is not guaranteed to build on
112+
//! `tracing-appender` is built against the latest stable release. The minimum supported
113+
//! version is 1.51. The current `tracing-appender` version is not guaranteed to build on
114114
//! Rust versions earlier than the minimum supported version.
115115
//!
116116
//! Tracing follows the same compiler support policies as the rest of the Tokio

0 commit comments

Comments
 (0)