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

Feature: Options to log line number and filename on tracing-subscriber #1773

Merged
merged 20 commits into from
Jan 14, 2022

Conversation

renecouto
Copy link
Contributor

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.

@renecouto renecouto requested review from davidbarsky, hawkw and a team as code owners December 9, 2021 22:41
Copy link
Member

@hawkw hawkw left a 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();
Copy link
Member

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:

Suggested change
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();

Copy link
Member

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
Copy link
Member

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
Copy link
Member

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?

@hawkw
Copy link
Member

hawkw commented Dec 9, 2021

...oh, I see that you also fixed the Windows path separator issue while i was writing the review, thanks!

Comment on lines 911 to 918
if let Some(line_number) = line_number {
write!(
writer,
"{}{} ",
dimmed.paint(line_number.to_string()),
dimmed.paint(":")
)?;
}
Copy link
Member

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)?

Copy link
Member

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.

Copy link
Member

@davidbarsky davidbarsky Dec 29, 2021

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.

Comment on lines 912 to 917
write!(
writer,
"{}{} ",
dimmed.paint(line_number.to_string()),
dimmed.paint(":")
)?;
Copy link
Member

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:

Suggested change
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:

#[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()),
Copy link
Member

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.

@hawkw
Copy link
Member

hawkw commented Dec 28, 2021

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.

@hawkw
Copy link
Member

hawkw commented Jan 11, 2022

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 <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw force-pushed the feature/log-line-number-and-filename branch from e663405 to 218afdd Compare January 13, 2022 19:12
@hawkw
Copy link
Member

hawkw commented Jan 13, 2022

okay, i went ahead and made the remaining required changes for this, I'll go ahead and merge it pending CI

@hawkw hawkw self-requested a review January 14, 2022 17:29
@hawkw hawkw merged commit 6f23c12 into tokio-rs:master Jan 14, 2022
hawkw added a commit that referenced this pull request Jan 14, 2022
## 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 <me@davidbarsky.com>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Jan 14, 2022
## 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 <me@davidbarsky.com>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Jan 14, 2022
# 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
hawkw added a commit that referenced this pull request Jan 14, 2022
# 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
bsodmike added a commit to bsodmike/axum-rest-starter-example that referenced this pull request Jan 17, 2022
This was just added to tracing
(tokio-rs/tracing#1773) and the wrapper can be
updated to leverage the same.
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
…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 <me@davidbarsky.com>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants