-
Notifications
You must be signed in to change notification settings - Fork 742
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
Feature: Options to log line number and filename on tracing-subscriber #1773
Feature: Options to log line number and filename on tracing-subscriber #1773
Conversation
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.
overall, this looks like a good start. however, i noticed the tests here don't pass on Windows, as the Windows CI build is failing. we'll want to fix that.
.with_ansi(false) | ||
.with_timer(MockTime); | ||
|
||
let expected = Regex::new("^fake time tracing_subscriber::fmt::format::test: tracing-subscriber/src/fmt/format/mod.rs: [0-9]+: hello\n$").unwrap(); |
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.
it looks like these tests fail on Windows, because Windows formats paths using \
rather than /
. see the CI failure: https://github.com/tokio-rs/tracing/runs/4477194846?check_suite_focus=true
perhaps what we should do instead is build the regex using the actual path to this file, rather than hard-coding it. that way, these tests would pass regardless of the operating system's path separator character. also, we wouldn't have to update it if we moved the test into a different file. we could write something like this:
let expected = Regex::new("^fake time tracing_subscriber::fmt::format::test: tracing-subscriber/src/fmt/format/mod.rs: [0-9]+: hello\n$").unwrap(); | |
let expected = Regex::new(concat!( | |
"^fake time ", | |
module_path!(), | |
" :", | |
file!(), | |
": [0-9]+: hello\n$" | |
)).unwrap(); |
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.
actually, hmm, this won't quite work, since we'd also need to escape any backslash characters in the path on windows, but we could do that by using format!
rather than concat!
, and using str::replace
to replace any \
s in the file path with \\
@@ -679,6 +693,24 @@ impl<F, T> Format<F, T> { | |||
} | |||
} | |||
|
|||
/// Sets whether or not the [filename] of the tracing logs source code is displayed |
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 looks like it's supposed to be a link, but it doesn't link to anything?
} | ||
} | ||
|
||
/// Sets whether or not the [line_number] of the tracing logs is source codedisplayed |
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.
similarly, this looks like it's supposed to be a link to something?
...oh, I see that you also fixed the Windows path separator issue while i was writing the review, thanks! |
if let Some(line_number) = line_number { | ||
write!( | ||
writer, | ||
"{}{} ", | ||
dimmed.paint(line_number.to_string()), | ||
dimmed.paint(":") | ||
)?; | ||
} |
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.
As I mentioned here, I'm still unconvinced that we should display line numbers if both targets and file names are disabled...a line number like ':156' doesn't make a lot of sense if we don't know what file or module it's in. But, trying to handle that case nicely will complicate the implementation significantly.
@davidbarsky what do you think? Is it worthwhile to make sure we do the "right thing" if a user calls with_line_number(true)
but never enables file names? What if they've also called .with_target(false)
?
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.
@davidbarsky what do you think? Is it worthwhile to make sure we do the "right thing" if a user calls
.with_line_number(true)
but never enables file names?
Just saw this. While I think that it's worth doing the right thing, I'd like to consider redesigning the configuration interface of fmt
holistically. To that end, I'd rather accept this PR-as-is designed, and fix it later.
At the very least, the lone line number will confusing, but it won't cause runtime panics like other misconfigurations can.
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.
To be clear, I'm not entirely sure what the holistic redesign should look like, but naively, I might want to try bundling lines and file formatting information into a single struct where a missing target/file is very obvious to the end user.
write!( | ||
writer, | ||
"{}{} ", | ||
dimmed.paint(line_number.to_string()), | ||
dimmed.paint(":") | ||
)?; |
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.
I don't think we should be calling to_string
here; that will allocate a new String
, which could have a performance impact that it would be nice to avoid. Nothing else in the formatting code currently allocates strings, so I'd really like to avoid this.
I think the to_string()
call here is necessary to use Style::paint
, which takes a Cow<'a, str>
rather than a fmt::Debug
implementation? We can avoid this by using ansi_term
's prefix
and suffix
methods, changing this code to something like this:
write!( | |
writer, | |
"{}{} ", | |
dimmed.paint(line_number.to_string()), | |
dimmed.paint(":") | |
)?; | |
write!( | |
writer, | |
"{}{}:{} ", | |
dimmed.prefix(), | |
line_number, | |
dimmed.suffix(), | |
)?; |
If we do that, we'll also need to add prefix()
and suffix()
methods to the no-op version of Style
that's used when ANSI support is disabled, here:
tracing/tracing-subscriber/src/fmt/format/mod.rs
Lines 1190 to 1198 in 989e07f
#[cfg(not(feature = "ansi"))] | |
impl Style { | |
fn new() -> Self { | |
Style | |
} | |
fn paint(&self, d: impl fmt::Display) -> impl fmt::Display { | |
d | |
} | |
} |
This would look something like the following:
#[cfg(not(feature = "ansi"))]
impl Style {
// ...
fn prefix(&self) -> &'static str {
""
}
fn suffix(&self) -> &'static str {
""
}
}
write!( | ||
writer, | ||
"{}{}", | ||
writer.bold().paint(line_number.to_string()), |
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.
as i mentioned above, i think the use of to_string
should be avoided here.
ping @renecouto, I'd really like to get this PR merged. I think https://github.com/tokio-rs/tracing/pull/1773/files#r774769644 is the last blocking issue we need to address before this is ready to merge. Do you have the time to continue working on this? If not, I'd be happy to make those changes to your branch so that we can get this released. |
another quick ping on this — @renecouto, are you still planning to work on this? otherwise, i'm happy to wrap it up so we can merge it. thanks again! |
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
e663405
to
218afdd
Compare
okay, i went ahead and made the remaining required changes for this, I'll go ahead and merge it pending CI |
## Motivation Logging line numbers and file names can be useful for debugging. This feature was suggested by #1326 ## Solution As per @hawkw's suggestions, fields were added on `Format`, along with builder methods. Filename and line number information was gathered from the `meta` variable. The `Pretty` formatter already supports printing source locations, but this is configured separately on the `Pretty` formatter rather than on the `Format` type. This branch also changes `Pretty` to honor the `Format`-level configurations and deprecates the `Pretty`-specific method. Fixes #1326 Closes #1804 Co-authored-by: David Barsky <[email protected]> Co-authored-by: Eliza Weisman <[email protected]>
## Motivation Logging line numbers and file names can be useful for debugging. This feature was suggested by #1326 ## Solution As per @hawkw's suggestions, fields were added on `Format`, along with builder methods. Filename and line number information was gathered from the `meta` variable. The `Pretty` formatter already supports printing source locations, but this is configured separately on the `Pretty` formatter rather than on the `Format` type. This branch also changes `Pretty` to honor the `Format`-level configurations and deprecates the `Pretty`-specific method. Fixes #1326 Closes #1804 Co-authored-by: David Barsky <[email protected]> Co-authored-by: Eliza Weisman <[email protected]>
# 0.3.6 (Jan 14, 2022) This release adds configuration options to `tracing_subscriber::fmt` to log source code locations for events. ### Added - **fmt**: Added `with_file` and `with_line_number` configuration methods to `fmt::Format`, `fmt::SubscriberBuilder`, and `fmt::Layer` ([#1773]) ### Fixed - **fmt**: Removed incorrect leading comma from span fields with the `Pretty` formatter ([#1833]) ### Deprecated - **fmt**: Deprecated `Pretty::with_source_location`, as it can now be replaced by the more general `Format`, `SubscriberBuilder`, and `Layer` methods ([#1773]) Thanks to new contributor @renecouto for contributing to this release! [#1773]: #1773 [#1833]: #1833
# 0.3.6 (Jan 14, 2022) This release adds configuration options to `tracing_subscriber::fmt` to log source code locations for events. ### Added - **fmt**: Added `with_file` and `with_line_number` configuration methods to `fmt::Format`, `fmt::SubscriberBuilder`, and `fmt::Layer` ([#1773]) ### Fixed - **fmt**: Removed incorrect leading comma from span fields with the `Pretty` formatter ([#1833]) ### Deprecated - **fmt**: Deprecated `Pretty::with_source_location`, as it can now be replaced by the more general `Format`, `SubscriberBuilder`, and `Layer` methods ([#1773]) Thanks to new contributor @renecouto for contributing to this release! [#1773]: #1773 [#1833]: #1833
This was just added to tracing (tokio-rs/tracing#1773) and the wrapper can be updated to leverage the same.
…1773) ## Motivation Logging line numbers and file names can be useful for debugging. This feature was suggested by tokio-rs#1326 ## Solution As per @hawkw's suggestions, fields were added on `Format`, along with builder methods. Filename and line number information was gathered from the `meta` variable. The `Pretty` formatter already supports printing source locations, but this is configured separately on the `Pretty` formatter rather than on the `Format` type. This branch also changes `Pretty` to honor the `Format`-level configurations and deprecates the `Pretty`-specific method. Fixes tokio-rs#1326 Closes tokio-rs#1804 Co-authored-by: David Barsky <[email protected]> Co-authored-by: Eliza Weisman <[email protected]>
# 0.3.6 (Jan 14, 2022) This release adds configuration options to `tracing_subscriber::fmt` to log source code locations for events. ### Added - **fmt**: Added `with_file` and `with_line_number` configuration methods to `fmt::Format`, `fmt::SubscriberBuilder`, and `fmt::Layer` ([tokio-rs#1773]) ### Fixed - **fmt**: Removed incorrect leading comma from span fields with the `Pretty` formatter ([tokio-rs#1833]) ### Deprecated - **fmt**: Deprecated `Pretty::with_source_location`, as it can now be replaced by the more general `Format`, `SubscriberBuilder`, and `Layer` methods ([tokio-rs#1773]) Thanks to new contributor @renecouto for contributing to this release! [tokio-rs#1773]: tokio-rs#1773 [tokio-rs#1833]: tokio-rs#1833
Motivation
Logging line numbers and file names can ease debugging. This feature was suggested by #1326
Solution
As per @hawkw 's suggestions, fields were added on Format, along with builder methods. filename and line number information was gathered from the
meta
variable.