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: Add offset time setting #2221

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 28 additions & 7 deletions tracing-appender/src/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::{
path::Path,
sync::atomic::{AtomicUsize, Ordering},
};
use time::{format_description, Duration, OffsetDateTime, Time};
use time::{format_description, Duration, OffsetDateTime, Time, UtcOffset};

/// A file appender with the ability to rotate log files at a fixed schedule.
///
Expand Down Expand Up @@ -85,6 +85,7 @@ pub struct RollingFileAppender {
writer: RwLock<File>,
#[cfg(test)]
now: Box<dyn Fn() -> OffsetDateTime + Send + Sync>,
offset_time: Option<UtcOffset>,
}

/// A [writer] that writes to a rolling log file.
Expand Down Expand Up @@ -142,16 +143,35 @@ impl RollingFileAppender {
writer,
#[cfg(test)]
now: Box::new(OffsetDateTime::now_utc),
offset_time: None,
}
}

pub fn set_time_offset(&mut self, offset: UtcOffset) {
self.offset_time = Some(offset);
let now = OffsetDateTime::now_utc().to_offset(offset);
let log_directory = self.state.log_directory.clone();
let log_filename_prefix = self.state.log_filename_prefix.clone();

let filename = self.state.rotation.join_date(log_filename_prefix.as_str(), &now);
self.writer = RwLock::new(
create_writer(log_directory.as_str(), &filename).expect("failed to create appender"),
);
}

#[inline]
fn now(&self) -> OffsetDateTime {
#[cfg(test)]
return (self.now)();
match self.offset_time {
None => { (self.now)() }
Some(offset_time) => { (self.now)().to_offset(offset_time) }
}

#[cfg(not(test))]
OffsetDateTime::now_utc()
match self.offset_time {
None => { OffsetDateTime::now_utc() }
Some(offset_time) => { OffsetDateTime::now_utc().to_offset(offset_time) }
}
}
}

Expand Down Expand Up @@ -655,7 +675,7 @@ mod test {

#[test]
#[should_panic(
expected = "internal error: entered unreachable code: Rotation::NEVER is impossible to round."
expected = "internal error: entered unreachable code: Rotation::NEVER is impossible to round."
)]
fn test_never_date_rounding() {
let now = OffsetDateTime::now_utc();
Expand All @@ -668,7 +688,7 @@ mod test {
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
sign:mandatory]:[offset_minute]:[offset_second]",
)
.unwrap();
.unwrap();

let now = OffsetDateTime::parse("2020-02-01 10:01:00 +00:00:00", &format).unwrap();

Expand Down Expand Up @@ -698,7 +718,7 @@ mod test {
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
sign:mandatory]:[offset_minute]:[offset_second]",
)
.unwrap();
.unwrap();

let now = OffsetDateTime::parse("2020-02-01 10:01:00 +00:00:00", &format).unwrap();
let directory = tempfile::tempdir().expect("failed to create tempdir");
Expand All @@ -710,7 +730,8 @@ mod test {
let clock = clock.clone();
Box::new(move || *clock.lock().unwrap())
};
let appender = RollingFileAppender { state, writer, now };
let mut appender = RollingFileAppender { state, writer, now, offset_time: None };
appender.set_time_offset(UtcOffset::from_hms(8, 0, 0).unwrap());
let default = tracing_subscriber::fmt()
.without_time()
.with_level(false)
Expand Down