zlog: Add env var to enable line number logging (#41905)

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-11-06 15:31:26 +00:00
committed by GitHub
parent 9c8e37a156
commit 6f6f652cf2
4 changed files with 30 additions and 10 deletions
+1 -1
View File
@@ -281,7 +281,7 @@ impl BackgroundExecutor {
}); });
let mut cx = std::task::Context::from_waker(&waker); let mut cx = std::task::Context::from_waker(&waker);
let duration = Duration::from_secs(500); let duration = Duration::from_secs(180);
let mut test_should_end_by = Instant::now() + duration; let mut test_should_end_by = Instant::now() + duration;
loop { loop {
+10 -6
View File
@@ -611,17 +611,21 @@ where
let file = caller.file().replace('\\', "/"); let file = caller.file().replace('\\', "/");
// In this codebase all crates reside in a `crates` directory, // In this codebase all crates reside in a `crates` directory,
// so discard the prefix up to that segment to find the crate name // so discard the prefix up to that segment to find the crate name
let target = file let file = file.split_once("crates/");
.split_once("crates/") let target = file.as_ref().and_then(|(_, s)| s.split_once("/src/"));
.and_then(|(_, s)| s.split_once("/src/"));
let module_path = target.map(|(krate, module)| { let module_path = target.map(|(krate, module)| {
krate.to_owned() + "::" + &module.trim_end_matches(".rs").replace('/', "::") if module.starts_with(krate) {
module.trim_end_matches(".rs").replace('/', "::")
} else {
krate.to_owned() + "::" + &module.trim_end_matches(".rs").replace('/', "::")
}
}); });
let file = file.map(|(_, file)| format!("crates/{file}"));
log::logger().log( log::logger().log(
&log::Record::builder() &log::Record::builder()
.target(target.map_or("", |(krate, _)| krate)) .target(module_path.as_deref().unwrap_or(""))
.module_path(module_path.as_deref()) .module_path(file.as_deref())
.args(format_args!("{:?}", error)) .args(format_args!("{:?}", error))
.file(Some(caller.file())) .file(Some(caller.file()))
.line(Some(caller.line())) .line(Some(caller.line()))
+14 -1
View File
@@ -39,6 +39,7 @@ pub struct Record<'a> {
pub level: log::Level, pub level: log::Level,
pub message: &'a std::fmt::Arguments<'a>, pub message: &'a std::fmt::Arguments<'a>,
pub module_path: Option<&'a str>, pub module_path: Option<&'a str>,
pub line: Option<u32>,
} }
pub fn init_output_stdout() { pub fn init_output_stdout() {
@@ -105,7 +106,11 @@ static LEVEL_ANSI_COLORS: [&str; 6] = [
]; ];
// PERF: batching // PERF: batching
pub fn submit(record: Record) { pub fn submit(mut record: Record) {
if record.module_path.is_none_or(|p| !p.ends_with(".rs")) {
// Only render line numbers for actual rust files emitted by `log_err` and friends
record.line.take();
}
if ENABLED_SINKS_STDOUT.load(Ordering::Acquire) { if ENABLED_SINKS_STDOUT.load(Ordering::Acquire) {
let mut stdout = std::io::stdout().lock(); let mut stdout = std::io::stdout().lock();
_ = writeln!( _ = writeln!(
@@ -117,6 +122,7 @@ pub fn submit(record: Record) {
SourceFmt { SourceFmt {
scope: record.scope, scope: record.scope,
module_path: record.module_path, module_path: record.module_path,
line: record.line,
ansi: true, ansi: true,
}, },
record.message record.message
@@ -132,6 +138,7 @@ pub fn submit(record: Record) {
SourceFmt { SourceFmt {
scope: record.scope, scope: record.scope,
module_path: record.module_path, module_path: record.module_path,
line: record.line,
ansi: true, ansi: true,
}, },
record.message record.message
@@ -167,6 +174,7 @@ pub fn submit(record: Record) {
SourceFmt { SourceFmt {
scope: record.scope, scope: record.scope,
module_path: record.module_path, module_path: record.module_path,
line: record.line,
ansi: false, ansi: false,
}, },
record.message record.message
@@ -202,6 +210,7 @@ pub fn flush() {
struct SourceFmt<'a> { struct SourceFmt<'a> {
scope: Scope, scope: Scope,
module_path: Option<&'a str>, module_path: Option<&'a str>,
line: Option<u32>,
ansi: bool, ansi: bool,
} }
@@ -225,6 +234,10 @@ impl std::fmt::Display for SourceFmt<'_> {
f.write_str(subscope)?; f.write_str(subscope)?;
} }
} }
if let Some(line) = self.line {
f.write_char(':')?;
line.fmt(f)?;
}
if self.ansi { if self.ansi {
f.write_str(ANSI_RESET)?; f.write_str(ANSI_RESET)?;
} }
+5 -2
View File
@@ -80,7 +80,7 @@ impl log::Log for Zlog {
None => (private::scope_new(&[]), private::scope_new(&["*unknown*"])), None => (private::scope_new(&[]), private::scope_new(&["*unknown*"])),
}; };
let level = record.metadata().level(); let level = record.metadata().level();
if !filter::is_scope_enabled(&crate_name_scope, record.module_path(), level) { if !filter::is_scope_enabled(&crate_name_scope, Some(record.target()), level) {
return; return;
} }
sink::submit(sink::Record { sink::submit(sink::Record {
@@ -89,6 +89,7 @@ impl log::Log for Zlog {
message: record.args(), message: record.args(),
// PERF(batching): store non-static paths in a cache + leak them and pass static str here // PERF(batching): store non-static paths in a cache + leak them and pass static str here
module_path: record.module_path().or(record.file()), module_path: record.module_path().or(record.file()),
line: record.line(),
}); });
} }
@@ -109,6 +110,7 @@ macro_rules! log {
level, level,
message: &format_args!($($arg)+), message: &format_args!($($arg)+),
module_path: Some(module_path!()), module_path: Some(module_path!()),
line: Some(line!()),
}); });
} }
} }
@@ -291,7 +293,7 @@ impl log::Log for Logger {
return; return;
} }
let level = record.metadata().level(); let level = record.metadata().level();
if !filter::is_scope_enabled(&self.scope, record.module_path(), level) { if !filter::is_scope_enabled(&self.scope, Some(record.target()), level) {
return; return;
} }
sink::submit(sink::Record { sink::submit(sink::Record {
@@ -299,6 +301,7 @@ impl log::Log for Logger {
level, level,
message: record.args(), message: record.args(),
module_path: record.module_path(), module_path: record.module_path(),
line: record.line(),
}); });
} }