Skip to content

Commit

Permalink
Merge pull request #345 from EriKWDev/main
Browse files Browse the repository at this point in the history
feature: ability to display source file path and line number with def…
  • Loading branch information
epage authored Dec 20, 2024
2 parents cc97bf7 + 59229bc commit 7742599
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
76 changes: 75 additions & 1 deletion src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ pub(crate) struct Builder {
pub(crate) format_indent: Option<usize>,
pub(crate) custom_format: Option<FormatFn>,
pub(crate) format_suffix: &'static str,
pub(crate) format_file: bool,
pub(crate) format_line_number: bool,
#[cfg(feature = "unstable-kv")]
pub(crate) kv_format: Option<Box<KvFormatFn>>,
built: bool,
Expand Down Expand Up @@ -244,6 +246,8 @@ impl Builder {
written_header_value: false,
indent: built.format_indent,
suffix: built.format_suffix,
source_file: built.format_file,
source_line_number: built.format_line_number,
#[cfg(feature = "unstable-kv")]
kv_format: built.kv_format.as_deref().unwrap_or(&default_kv_format),
buf,
Expand All @@ -262,6 +266,8 @@ impl Default for Builder {
format_module_path: false,
format_target: true,
format_level: true,
format_file: false,
format_line_number: false,
format_indent: Some(4),
custom_format: None,
format_suffix: "\n",
Expand Down Expand Up @@ -309,6 +315,8 @@ struct DefaultFormat<'a> {
module_path: bool,
target: bool,
level: bool,
source_file: bool,
source_line_number: bool,
written_header_value: bool,
indent: Option<usize>,
buf: &'a mut Formatter,
Expand All @@ -322,6 +330,7 @@ impl DefaultFormat<'_> {
self.write_timestamp()?;
self.write_level(record)?;
self.write_module_path(record)?;
self.write_source_location(record)?;
self.write_target(record)?;
self.finish_header()?;

Expand Down Expand Up @@ -421,6 +430,22 @@ impl DefaultFormat<'_> {
}
}

fn write_source_location(&mut self, record: &Record<'_>) -> io::Result<()> {
if !self.source_file {
return Ok(());
}

if let Some(file_path) = record.file() {
let line = self.source_line_number.then(|| record.line()).flatten();
match line {
Some(line) => self.write_header_value(format_args!("{file_path}:{line}")),
None => self.write_header_value(file_path),
}
} else {
Ok(())
}
}

fn write_target(&mut self, record: &Record<'_>) -> io::Result<()> {
if !self.target {
return Ok(());
Expand Down Expand Up @@ -550,6 +575,8 @@ mod tests {
module_path: true,
target: false,
level: true,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -570,6 +597,8 @@ mod tests {
module_path: false,
target: false,
level: false,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -590,6 +619,8 @@ mod tests {
module_path: true,
target: false,
level: true,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -610,6 +641,8 @@ mod tests {
module_path: true,
target: false,
level: true,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -630,6 +663,8 @@ mod tests {
module_path: false,
target: false,
level: false,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -650,6 +685,8 @@ mod tests {
module_path: false,
target: false,
level: false,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -670,6 +707,8 @@ mod tests {
module_path: false,
target: false,
level: false,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -692,6 +731,8 @@ mod tests {
module_path: true,
target: true,
level: true,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -713,6 +754,8 @@ mod tests {
module_path: true,
target: true,
level: true,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -735,6 +778,8 @@ mod tests {
module_path: true,
target: false,
level: true,
source_file: false,
source_line_number: false,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
Expand All @@ -747,6 +792,28 @@ mod tests {
assert_eq!("[INFO test::path] log\nmessage\n", written);
}

#[test]
fn format_with_source_file_and_line_number() {
let mut f = formatter();

let written = write(DefaultFormat {
timestamp: None,
module_path: false,
target: false,
level: true,
source_file: true,
source_line_number: true,
#[cfg(feature = "unstable-kv")]
kv_format: &hidden_kv_format,
written_header_value: false,
indent: None,
suffix: "\n",
buf: &mut f,
});

assert_eq!("[INFO test.rs:144] log\nmessage\n", written);
}

#[cfg(feature = "unstable-kv")]
#[test]
fn format_kv_default() {
Expand All @@ -766,6 +833,8 @@ mod tests {
module_path: false,
target: false,
level: true,
source_file: false,
source_line_number: false,
kv_format: &default_kv_format,
written_header_value: false,
indent: None,
Expand Down Expand Up @@ -799,6 +868,8 @@ mod tests {
module_path: true,
target: true,
level: true,
source_file: true,
source_line_number: true,
kv_format: &default_kv_format,
written_header_value: false,
indent: None,
Expand All @@ -807,6 +878,9 @@ mod tests {
},
);

assert_eq!("[INFO test::path target] log\nmessage a=1 b=2\n", written);
assert_eq!(
"[INFO test::path test.rs:42 target] log\nmessage a=1 b=2\n",
written
);
}
}
23 changes: 23 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ impl Builder {
self
}

/// Whether or not to write the source file path in the default format.
pub fn format_file(&mut self, write: bool) -> &mut Self {
self.format.format_file = write;
self
}

/// Whether or not to write the source line number path in the default format.
///
/// Only has effect if `format_file` is also enabled
pub fn format_line_number(&mut self, write: bool) -> &mut Self {
self.format.format_line_number = write;
self
}

/// Whether or not to write the source path and line number
///
/// Equivalent to calling both `format_file` and `format_line_number`
/// with `true`
pub fn format_source_path(&mut self, write: bool) -> &mut Self {
self.format_file(write).format_line_number(write);
self
}

/// Whether or not to write the module path in the default format.
pub fn format_module_path(&mut self, write: bool) -> &mut Self {
self.format.format_module_path = write;
Expand Down

0 comments on commit 7742599

Please sign in to comment.