-
Notifications
You must be signed in to change notification settings - Fork 887
Feature: Options to log line number and filename on tracing-subscriber #1773
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
Changes from 6 commits
91b860e
ddf4f0c
2c95286
c3b8c72
7afbe52
989e07f
4a8f07a
317ee10
662bdee
6a6ff49
7e6a77c
c2a2f04
a2bb9f3
f37d5e1
333991b
333e8ed
3791f7a
6535633
218afdd
bda7dad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -328,6 +328,8 @@ pub struct Format<F = Full, T = SystemTime> { | |||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) display_level: bool, | ||||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) display_thread_id: bool, | ||||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) display_thread_name: bool, | ||||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) display_filename: bool, | ||||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) display_line_number: bool, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // === impl Writer === | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -504,6 +506,8 @@ impl Default for Format<Full, SystemTime> { | |||||||||||||||||||||||||||||||||||||||||||||
| display_level: true, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_id: false, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_name: false, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_filename: false, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_line_number: false, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -522,6 +526,8 @@ impl<F, T> Format<F, T> { | |||||||||||||||||||||||||||||||||||||||||||||
| display_level: self.display_level, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_id: self.display_thread_id, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_name: self.display_thread_name, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_filename: self.display_filename, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_line_number: self.display_line_number, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -559,6 +565,8 @@ impl<F, T> Format<F, T> { | |||||||||||||||||||||||||||||||||||||||||||||
| display_level: self.display_level, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_id: self.display_thread_id, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_name: self.display_thread_name, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_filename: self.display_filename, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_line_number: self.display_line_number, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -589,6 +597,8 @@ impl<F, T> Format<F, T> { | |||||||||||||||||||||||||||||||||||||||||||||
| display_level: self.display_level, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_id: self.display_thread_id, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_name: self.display_thread_name, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_filename: self.display_filename, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_line_number: self.display_line_number, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -616,6 +626,8 @@ impl<F, T> Format<F, T> { | |||||||||||||||||||||||||||||||||||||||||||||
| display_level: self.display_level, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_id: self.display_thread_id, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_name: self.display_thread_name, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_filename: self.display_filename, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_line_number: self.display_line_number, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -630,6 +642,8 @@ impl<F, T> Format<F, T> { | |||||||||||||||||||||||||||||||||||||||||||||
| display_level: self.display_level, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_id: self.display_thread_id, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_thread_name: self.display_thread_name, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_filename: self.display_filename, | ||||||||||||||||||||||||||||||||||||||||||||||
| display_line_number: self.display_line_number, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -679,6 +693,24 @@ impl<F, T> Format<F, T> { | |||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| /// Sets whether or not the [filename] of the tracing logs source code is displayed | ||||||||||||||||||||||||||||||||||||||||||||||
| /// when formatting events | ||||||||||||||||||||||||||||||||||||||||||||||
| pub fn with_filename(self, display_filename: bool) -> Format<F, T> { | ||||||||||||||||||||||||||||||||||||||||||||||
| Format { | ||||||||||||||||||||||||||||||||||||||||||||||
| display_filename, | ||||||||||||||||||||||||||||||||||||||||||||||
| ..self | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| /// Sets whether or not the [line_number] of the tracing logs is source codedisplayed | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| /// when formatting events | ||||||||||||||||||||||||||||||||||||||||||||||
| pub fn with_line_number(self, display_line_number: bool) -> Format<F, T> { | ||||||||||||||||||||||||||||||||||||||||||||||
| Format { | ||||||||||||||||||||||||||||||||||||||||||||||
| display_line_number, | ||||||||||||||||||||||||||||||||||||||||||||||
| ..self | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| fn format_level(&self, level: Level, writer: &mut Writer<'_>) -> fmt::Result | ||||||||||||||||||||||||||||||||||||||||||||||
| where | ||||||||||||||||||||||||||||||||||||||||||||||
| F: LevelNames, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -862,6 +894,29 @@ where | |||||||||||||||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| let line_number = if self.display_line_number { meta.line() } else { None }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if self.display_filename { | ||||||||||||||||||||||||||||||||||||||||||||||
| if let Some(filename) = meta.file() { | ||||||||||||||||||||||||||||||||||||||||||||||
| write!( | ||||||||||||||||||||||||||||||||||||||||||||||
| writer, | ||||||||||||||||||||||||||||||||||||||||||||||
| "{}{}{}", | ||||||||||||||||||||||||||||||||||||||||||||||
| dimmed.paint(filename), | ||||||||||||||||||||||||||||||||||||||||||||||
| dimmed.paint(":"), | ||||||||||||||||||||||||||||||||||||||||||||||
| if line_number.is_some() { "" } else { " " } | ||||||||||||||||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if let Some(line_number) = line_number { | ||||||||||||||||||||||||||||||||||||||||||||||
| write!( | ||||||||||||||||||||||||||||||||||||||||||||||
| writer, | ||||||||||||||||||||||||||||||||||||||||||||||
| "{}{} ", | ||||||||||||||||||||||||||||||||||||||||||||||
| dimmed.paint(line_number.to_string()), | ||||||||||||||||||||||||||||||||||||||||||||||
| dimmed.paint(":") | ||||||||||||||||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should be calling I think the
Suggested change
If we do that, we'll also need to add tracing/tracing-subscriber/src/fmt/format/mod.rs Lines 1190 to 1198 in 989e07f
This would look something like the following: #[cfg(not(feature = "ansi"))]
impl Style {
// ...
fn prefix(&self) -> &'static str {
""
}
fn suffix(&self) -> &'static str {
""
}
} |
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just saw this. While I think that it's worth doing the right thing, I'd like to consider redesigning the configuration interface of At the very least, the lone line number will confusing, but it won't cause runtime panics like other misconfigurations can.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ctx.format_fields(writer.by_ref(), event)?; | ||||||||||||||||||||||||||||||||||||||||||||||
| writeln!(writer) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -916,6 +971,28 @@ where | |||||||||||||||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if self.display_filename { | ||||||||||||||||||||||||||||||||||||||||||||||
| if let Some(filename) = meta.file() { | ||||||||||||||||||||||||||||||||||||||||||||||
| write!( | ||||||||||||||||||||||||||||||||||||||||||||||
| writer, | ||||||||||||||||||||||||||||||||||||||||||||||
| "{}{}", | ||||||||||||||||||||||||||||||||||||||||||||||
| writer.bold().paint(filename), | ||||||||||||||||||||||||||||||||||||||||||||||
| writer.dimmed().paint(":") | ||||||||||||||||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if self.display_line_number { | ||||||||||||||||||||||||||||||||||||||||||||||
| if let Some(line_number) = meta.line() { | ||||||||||||||||||||||||||||||||||||||||||||||
| write!( | ||||||||||||||||||||||||||||||||||||||||||||||
| writer, | ||||||||||||||||||||||||||||||||||||||||||||||
| "{}{}", | ||||||||||||||||||||||||||||||||||||||||||||||
| writer.bold().paint(line_number.to_string()), | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| writer.dimmed().paint(":") | ||||||||||||||||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ctx.format_fields(writer.by_ref(), event)?; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| let dimmed = writer.dimmed(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1467,6 +1544,8 @@ pub(super) mod test { | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| use super::{FmtSpan, TimingDisplay, Writer}; | ||||||||||||||||||||||||||||||||||||||||||||||
| use std::fmt; | ||||||||||||||||||||||||||||||||||||||||||||||
| use std::path::Path; | ||||||||||||||||||||||||||||||||||||||||||||||
| use regex::Regex; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) struct MockTime; | ||||||||||||||||||||||||||||||||||||||||||||||
| impl FormatTime for MockTime { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1529,6 +1608,56 @@ pub(super) mod test { | |||||||||||||||||||||||||||||||||||||||||||||
| run_test(subscriber, make_writer, expected); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||
| fn with_line_number_and_file_name() { | ||||||||||||||||||||||||||||||||||||||||||||||
| let make_writer = MockMakeWriter::default(); | ||||||||||||||||||||||||||||||||||||||||||||||
| let subscriber = crate::fmt::Collector::builder() | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_writer(make_writer.clone()) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_filename(true) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_line_number(true) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_level(false) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_ansi(false) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_timer(MockTime); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| let current_path = Path::new("tracing-subscriber").join("src").join("fmt").join("format").join("mod.rs"); | ||||||||||||||||||||||||||||||||||||||||||||||
| let expected = Regex::new(&format!("^fake time tracing_subscriber::fmt::format::test: {}:[0-9]+: hello\n$", current_path.to_str().unwrap())).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||
| let _default = set_default(&subscriber.into()); | ||||||||||||||||||||||||||||||||||||||||||||||
| tracing::info!("hello"); | ||||||||||||||||||||||||||||||||||||||||||||||
| let res = make_writer.get_string(); | ||||||||||||||||||||||||||||||||||||||||||||||
| assert!(expected.is_match(&res)); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||
| fn with_line_number() { | ||||||||||||||||||||||||||||||||||||||||||||||
| let make_writer = MockMakeWriter::default(); | ||||||||||||||||||||||||||||||||||||||||||||||
| let subscriber = crate::fmt::Collector::builder() | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_writer(make_writer.clone()) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_line_number(true) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_level(false) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_ansi(false) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_timer(MockTime); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| let expected = Regex::new("^fake time tracing_subscriber::fmt::format::test: [0-9]+: hello\n$").unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||
| let _default = set_default(&subscriber.into()); | ||||||||||||||||||||||||||||||||||||||||||||||
| tracing::info!("hello"); | ||||||||||||||||||||||||||||||||||||||||||||||
| let res = make_writer.get_string(); | ||||||||||||||||||||||||||||||||||||||||||||||
| assert!(expected.is_match(&res)); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||
| fn with_filename() { | ||||||||||||||||||||||||||||||||||||||||||||||
| let make_writer = MockMakeWriter::default(); | ||||||||||||||||||||||||||||||||||||||||||||||
| let subscriber = crate::fmt::Collector::builder() | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_writer(make_writer.clone()) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_filename(true) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_level(false) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_ansi(false) | ||||||||||||||||||||||||||||||||||||||||||||||
| .with_timer(MockTime); | ||||||||||||||||||||||||||||||||||||||||||||||
| let current_path = Path::new("tracing-subscriber").join("src").join("fmt").join("format").join("mod.rs"); | ||||||||||||||||||||||||||||||||||||||||||||||
| let expected = &format!("fake time tracing_subscriber::fmt::format::test: {}: hello\n", current_path.to_str().unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||
| run_test(subscriber, make_writer, expected); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(feature = "ansi")] | ||||||||||||||||||||||||||||||||||||||||||||||
| fn test_ansi(is_ansi: bool, expected: &str) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let make_writer = MockMakeWriter::default(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
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?