Skip to content
Open
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
4 changes: 4 additions & 0 deletions tracing-appender/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ keywords = ["logging", "tracing", "file-appender", "non-blocking-writer"]
edition = "2018"
rust-version = "1.63.0"

[features]
default = []
local-time = ["time/local-offset"]

[dependencies]
crossbeam-channel = "0.5.6"
time = { version = "0.3.2", default-features = false, features = ["formatting", "parsing"] }
Expand Down
34 changes: 31 additions & 3 deletions tracing-appender/src/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl RollingFileAppender {
ref max_files,
} = builder;
let directory = directory.as_ref().to_path_buf();
let now = OffsetDateTime::now_utc();
let now = Self::now_local_or_utc();
let (state, writer) = Inner::new(
now,
rotation.clone(),
Expand All @@ -215,7 +215,16 @@ impl RollingFileAppender {
return (self.now)();

#[cfg(not(test))]
OffsetDateTime::now_utc()
Self::now_local_or_utc()
}

#[inline]
fn now_local_or_utc() -> OffsetDateTime {
#[cfg(not(feature = "local-time"))]
return OffsetDateTime::now_utc();

#[cfg(feature = "local-time")]
OffsetDateTime::now_local().expect("Invalid localtime; this is a bug in tracing-appender")
}
}

Expand Down Expand Up @@ -554,13 +563,32 @@ impl Rotation {
}

fn date_format(&self) -> Vec<format_description::FormatItem<'static>> {
match *self {
#[cfg(not(feature = "local-time"))]
return match *self {
Rotation::MINUTELY => format_description::parse("[year]-[month]-[day]-[hour]-[minute]"),
Rotation::HOURLY => format_description::parse("[year]-[month]-[day]-[hour]"),
Rotation::DAILY => format_description::parse("[year]-[month]-[day]"),
Rotation::WEEKLY => format_description::parse("[year]-[month]-[day]"),
Rotation::NEVER => format_description::parse("[year]-[month]-[day]"),
}
.expect("Unable to create a formatter; this is a bug in tracing-appender");

#[cfg(feature = "local-time")]
match *self {
Rotation::MINUTELY => format_description::parse(
"[year]-[month]-[day]-[hour]-[minute][offset_hour sign:mandatory][offset_minute]",
),
Rotation::HOURLY => format_description::parse(
"[year]-[month]-[day]-[hour][offset_hour sign:mandatory][offset_minute]",
),
Rotation::DAILY => format_description::parse(
"[year]-[month]-[day][offset_hour sign:mandatory][offset_minute]",
),
Rotation::WEEKLY => format_description::parse(
"[year]-[month]-[day][offset_hour sign:mandatory][offset_minute]",
),
Rotation::NEVER => format_description::parse("[year]-[month]-[day]"),
}
.expect("Unable to create a formatter; this is a bug in tracing-appender")
}
}
Expand Down