Skip to content

Commit

Permalink
appender: replace chrono with time (#1652)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
davidbarsky authored and hawkw committed Oct 22, 2021
1 parent 60a2644 commit c62dac9
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 210 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,24 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
args: --all
args: --all --exclude=tracing-appender

# TODO: remove this once tracing's MSRV is bumped.
check-msrv-appender:
# Run `cargo check` on our minimum supported Rust version (1.51.0).
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.51.0
profile: minimal
override: true
- name: Check
uses: actions-rs/cargo@v1
with:
command: check
args: --lib=tracing-appender

check:
# Run `cargo check` first to ensure that the pushed code at least compiles.
Expand Down
5 changes: 3 additions & 2 deletions tracing-appender/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ edition = "2018"

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

[dependencies.tracing-subscriber]
path = "../tracing-subscriber"
version = "0.2.7"
default-features = false
features = ["fmt"]
features = ["fmt", "std"]

[dev-dependencies]
tracing = { path = "../tracing", version = "0.1" }
time = { version = "0.3", default-features = false, features = ["formatting", "parsing"] }
tempfile = "3"
18 changes: 9 additions & 9 deletions tracing-appender/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allows events and spans to be recorded in a non-blocking manner through a
dedicated logging thread. It also provides a [`RollingFileAppender`][file_appender]
that can be used with _or_ without the non-blocking writer.

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

[msrv]: #supported-rust-versions

Expand Down Expand Up @@ -145,17 +145,17 @@ fn main() {

## Supported Rust Versions

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

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

## License

Expand Down
20 changes: 12 additions & 8 deletions tracing-appender/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ use std::io::{BufWriter, Write};
use std::{fs, io};

use crate::rolling::Rotation;
use chrono::prelude::*;
use std::fmt::Debug;
use std::fs::{File, OpenOptions};
use std::path::Path;
use time::OffsetDateTime;

#[derive(Debug)]
pub(crate) struct InnerAppender {
log_directory: String,
log_filename_prefix: String,
writer: BufWriter<File>,
next_date: DateTime<Utc>,
next_date: Option<OffsetDateTime>,
rotation: Rotation,
}

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

Expand All @@ -32,7 +32,7 @@ impl InnerAppender {
log_directory: &Path,
log_filename_prefix: &Path,
rotation: Rotation,
now: DateTime<Utc>,
now: OffsetDateTime,
) -> io::Result<Self> {
let log_directory = log_directory.to_str().unwrap();
let log_filename_prefix = log_filename_prefix.to_str().unwrap();
Expand All @@ -49,15 +49,15 @@ impl InnerAppender {
})
}

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

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

Expand All @@ -75,8 +75,12 @@ impl InnerAppender {
}
}

fn should_rollover(&self, date: DateTime<Utc>) -> bool {
date >= self.next_date
fn should_rollover(&self, date: OffsetDateTime) -> bool {
// the `None` case means that the `InnerAppender` *never* rorates log files.
match self.next_date {
None => false,
Some(next_date) => date >= next_date,
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions tracing-appender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! a dedicated logging thread. It also provides a [`RollingFileAppender`][file_appender] that can
//! be used with _or_ without the non-blocking writer.
//!
//! *Compiler support: [requires `rustc` 1.42+][msrv]*
//! *Compiler support: [requires `rustc` 1.51+][msrv]*
//!
//! [msrv]: #supported-rust-versions
//! [file_appender]: ./rolling/struct.RollingFileAppender.html
Expand Down Expand Up @@ -110,8 +110,8 @@
//!
//! ## Supported Rust Versions
//!
//! Tracing is built against the latest stable release. The minimum supported
//! version is 1.42. The current Tracing version is not guaranteed to build on
//! `tracing-appender` is built against the latest stable release. The minimum supported
//! version is 1.51. The current `tracing-appender` version is not guaranteed to build on
//! Rust versions earlier than the minimum supported version.
//!
//! Tracing follows the same compiler support policies as the rest of the Tokio
Expand Down
Loading

0 comments on commit c62dac9

Please sign in to comment.