Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appender: replace chrono with time #1652

Merged
merged 14 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also update the MSRV in tracing-appender's documentation & readme?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup!


# 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.3"
default-features = false
features = ["fmt"]
features = ["fmt", "std"]

[dev-dependencies]
tracing = { path = "../tracing", version = "0.2" }
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]*
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved

[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();
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved
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,
}
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved
}
}

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::RollingFileAppender
Expand Down Expand Up @@ -109,8 +109,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