tracing
tracing copied to clipboard
Bubble up error for RollingFileAppender::new
Motivation
RollingAppender::new
uses expect
to handle any InnerAppender
error:
RollingFileAppender {
inner: InnerAppender::new(
directory.as_ref(),
file_name_prefix.as_ref(),
rotation,
Utc::now(),
).expect("Failed to create appender"),
}
I found it may be annoying to get a panic and not being able to handle it for outer error handling systems API users may have.
In specific I have been using it when bootstrapping a server. We have some specific return codes depending on what bootstrapping error happened. For solving it I had to manually check some file-related data (like if the path is malformed) before using any RollingAppender
related call.
Solution
Bubble-up the derivated error and handle in the most outer scope.
pub fn new(
rotation: Rotation,
directory: impl AsRef<Path>,
file_name_prefix: impl AsRef<Path>,
) -> io::Result<RollingFileAppender> {
Ok(RollingFileAppender {
inner: InnerAppender::new(
directory.as_ref(),
file_name_prefix.as_ref(),
rotation,
Utc::now(),
)?,
})
}
I updated the unwraps with a custom expect message. I understand that is a breaking change and that it will have to wait until the next breaking release of tracing-appender.
Maybe with a rebase, this is still something that could be merged.
@bryangarza I rebased and updated the code 😄
FYI @hawkw @davidbarsky this one is rebased now