-
Notifications
You must be signed in to change notification settings - Fork 734
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
Adds a check in tracing_appender
#2826
base: master
Are you sure you want to change the base?
Conversation
Checks that the input `Builder::max_log_files` is positive, or it would cause a crash in `src/rolling.rs` at line 626.
Ohh, So if NonZeroUsize is introduced the user will have to change .max_log_files(5) to .max_log_files(5.try_into().unwrap()) which isn't that beautiful, but I think its slightly better (at least better than panicking? [0] ). I guess this change is still fine for [0]: In terms that the library is panicking, in the second example above the application code is panicking at the |
It didn't occur to me I could use an |
Should be fixed now, let me know if you have any other observations or suggestions😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks good to me, thanks for the PR! I suggested some very small style changes I'd like to make, and then I'm going to go ahead and merge this.
I agree that NonZeroUsize
could be worth considering for a breaking change release in the future, but the benefit of enforcing non-zero at the type level ought to be weighed against the cost of introducing more complexity for the caller, as @kaffarell pointed out...
PR #2323 added a feature that allows the creation of a
RollingFileAppender
that keeps only then
most recent log files on each rotation.I find this feature to be very useful and have been using it in my code every since
tracing_appender
0.2.3 came out. I opened this PR with the intention of introducing a couple of minor improvements on top of the changes brought by PR #2323.Motivation
The current version of the code leaves open the possibility for
n
to be 0, a value that makes no sense and that causes a crash inprune_old_logs
at line 626 ofsrc/rolling.rs
, when an attempt at computingmax_files - 1
is made. It seems to me that this is not a major pitfall for the user, as it is easy to avoid passing 0 toBuilder::max_log_files
, but I believe that the code should still perform some basic check.Moreover, when I first read the documentation for this method I was confused by the fact that
n
was modelled with anusize
and that no mention of the special value of 0 was made. In particular, I did not understand if 0 was just a forbidden value, a special value with an ad-hoc meaning, or if I was misinterpreting the meaning ofn
altogether.Solution
This PR adds checks for the positiveness of
n
in the public-facing parts of the API, i.e. inBuilder::max_log_files
. This method panics if the check fails, thus anticipating the error that would occur during rotation at the source of the error, making the error easier to debug.It also adds a line in the documentation that makes it clear that 0 is a forbidden value.
Future Improvements
Making
n
aNonZeroUsize
instead of anusize
would make its semantics clearer and reduce the likelihood of an error, both for the users and in the internal parts of the code. It would, however, constitute a breaking change, so it could be reserved e.g. for v0.3 oftracing_appender
. If you are favourable to this change, I could work on it and open a PR to have it fixed.