Skip to content

Commit 7742599

Browse files
authored
Merge pull request #345 from EriKWDev/main
feature: ability to display source file path and line number with def…
2 parents cc97bf7 + 59229bc commit 7742599

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/fmt/mod.rs

+75-1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ pub(crate) struct Builder {
210210
pub(crate) format_indent: Option<usize>,
211211
pub(crate) custom_format: Option<FormatFn>,
212212
pub(crate) format_suffix: &'static str,
213+
pub(crate) format_file: bool,
214+
pub(crate) format_line_number: bool,
213215
#[cfg(feature = "unstable-kv")]
214216
pub(crate) kv_format: Option<Box<KvFormatFn>>,
215217
built: bool,
@@ -244,6 +246,8 @@ impl Builder {
244246
written_header_value: false,
245247
indent: built.format_indent,
246248
suffix: built.format_suffix,
249+
source_file: built.format_file,
250+
source_line_number: built.format_line_number,
247251
#[cfg(feature = "unstable-kv")]
248252
kv_format: built.kv_format.as_deref().unwrap_or(&default_kv_format),
249253
buf,
@@ -262,6 +266,8 @@ impl Default for Builder {
262266
format_module_path: false,
263267
format_target: true,
264268
format_level: true,
269+
format_file: false,
270+
format_line_number: false,
265271
format_indent: Some(4),
266272
custom_format: None,
267273
format_suffix: "\n",
@@ -309,6 +315,8 @@ struct DefaultFormat<'a> {
309315
module_path: bool,
310316
target: bool,
311317
level: bool,
318+
source_file: bool,
319+
source_line_number: bool,
312320
written_header_value: bool,
313321
indent: Option<usize>,
314322
buf: &'a mut Formatter,
@@ -322,6 +330,7 @@ impl DefaultFormat<'_> {
322330
self.write_timestamp()?;
323331
self.write_level(record)?;
324332
self.write_module_path(record)?;
333+
self.write_source_location(record)?;
325334
self.write_target(record)?;
326335
self.finish_header()?;
327336

@@ -421,6 +430,22 @@ impl DefaultFormat<'_> {
421430
}
422431
}
423432

433+
fn write_source_location(&mut self, record: &Record<'_>) -> io::Result<()> {
434+
if !self.source_file {
435+
return Ok(());
436+
}
437+
438+
if let Some(file_path) = record.file() {
439+
let line = self.source_line_number.then(|| record.line()).flatten();
440+
match line {
441+
Some(line) => self.write_header_value(format_args!("{file_path}:{line}")),
442+
None => self.write_header_value(file_path),
443+
}
444+
} else {
445+
Ok(())
446+
}
447+
}
448+
424449
fn write_target(&mut self, record: &Record<'_>) -> io::Result<()> {
425450
if !self.target {
426451
return Ok(());
@@ -550,6 +575,8 @@ mod tests {
550575
module_path: true,
551576
target: false,
552577
level: true,
578+
source_file: false,
579+
source_line_number: false,
553580
#[cfg(feature = "unstable-kv")]
554581
kv_format: &hidden_kv_format,
555582
written_header_value: false,
@@ -570,6 +597,8 @@ mod tests {
570597
module_path: false,
571598
target: false,
572599
level: false,
600+
source_file: false,
601+
source_line_number: false,
573602
#[cfg(feature = "unstable-kv")]
574603
kv_format: &hidden_kv_format,
575604
written_header_value: false,
@@ -590,6 +619,8 @@ mod tests {
590619
module_path: true,
591620
target: false,
592621
level: true,
622+
source_file: false,
623+
source_line_number: false,
593624
#[cfg(feature = "unstable-kv")]
594625
kv_format: &hidden_kv_format,
595626
written_header_value: false,
@@ -610,6 +641,8 @@ mod tests {
610641
module_path: true,
611642
target: false,
612643
level: true,
644+
source_file: false,
645+
source_line_number: false,
613646
#[cfg(feature = "unstable-kv")]
614647
kv_format: &hidden_kv_format,
615648
written_header_value: false,
@@ -630,6 +663,8 @@ mod tests {
630663
module_path: false,
631664
target: false,
632665
level: false,
666+
source_file: false,
667+
source_line_number: false,
633668
#[cfg(feature = "unstable-kv")]
634669
kv_format: &hidden_kv_format,
635670
written_header_value: false,
@@ -650,6 +685,8 @@ mod tests {
650685
module_path: false,
651686
target: false,
652687
level: false,
688+
source_file: false,
689+
source_line_number: false,
653690
#[cfg(feature = "unstable-kv")]
654691
kv_format: &hidden_kv_format,
655692
written_header_value: false,
@@ -670,6 +707,8 @@ mod tests {
670707
module_path: false,
671708
target: false,
672709
level: false,
710+
source_file: false,
711+
source_line_number: false,
673712
#[cfg(feature = "unstable-kv")]
674713
kv_format: &hidden_kv_format,
675714
written_header_value: false,
@@ -692,6 +731,8 @@ mod tests {
692731
module_path: true,
693732
target: true,
694733
level: true,
734+
source_file: false,
735+
source_line_number: false,
695736
#[cfg(feature = "unstable-kv")]
696737
kv_format: &hidden_kv_format,
697738
written_header_value: false,
@@ -713,6 +754,8 @@ mod tests {
713754
module_path: true,
714755
target: true,
715756
level: true,
757+
source_file: false,
758+
source_line_number: false,
716759
#[cfg(feature = "unstable-kv")]
717760
kv_format: &hidden_kv_format,
718761
written_header_value: false,
@@ -735,6 +778,8 @@ mod tests {
735778
module_path: true,
736779
target: false,
737780
level: true,
781+
source_file: false,
782+
source_line_number: false,
738783
#[cfg(feature = "unstable-kv")]
739784
kv_format: &hidden_kv_format,
740785
written_header_value: false,
@@ -747,6 +792,28 @@ mod tests {
747792
assert_eq!("[INFO test::path] log\nmessage\n", written);
748793
}
749794

795+
#[test]
796+
fn format_with_source_file_and_line_number() {
797+
let mut f = formatter();
798+
799+
let written = write(DefaultFormat {
800+
timestamp: None,
801+
module_path: false,
802+
target: false,
803+
level: true,
804+
source_file: true,
805+
source_line_number: true,
806+
#[cfg(feature = "unstable-kv")]
807+
kv_format: &hidden_kv_format,
808+
written_header_value: false,
809+
indent: None,
810+
suffix: "\n",
811+
buf: &mut f,
812+
});
813+
814+
assert_eq!("[INFO test.rs:144] log\nmessage\n", written);
815+
}
816+
750817
#[cfg(feature = "unstable-kv")]
751818
#[test]
752819
fn format_kv_default() {
@@ -766,6 +833,8 @@ mod tests {
766833
module_path: false,
767834
target: false,
768835
level: true,
836+
source_file: false,
837+
source_line_number: false,
769838
kv_format: &default_kv_format,
770839
written_header_value: false,
771840
indent: None,
@@ -799,6 +868,8 @@ mod tests {
799868
module_path: true,
800869
target: true,
801870
level: true,
871+
source_file: true,
872+
source_line_number: true,
802873
kv_format: &default_kv_format,
803874
written_header_value: false,
804875
indent: None,
@@ -807,6 +878,9 @@ mod tests {
807878
},
808879
);
809880

810-
assert_eq!("[INFO test::path target] log\nmessage a=1 b=2\n", written);
881+
assert_eq!(
882+
"[INFO test::path test.rs:42 target] log\nmessage a=1 b=2\n",
883+
written
884+
);
811885
}
812886
}

src/logger.rs

+23
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,29 @@ impl Builder {
262262
self
263263
}
264264

265+
/// Whether or not to write the source file path in the default format.
266+
pub fn format_file(&mut self, write: bool) -> &mut Self {
267+
self.format.format_file = write;
268+
self
269+
}
270+
271+
/// Whether or not to write the source line number path in the default format.
272+
///
273+
/// Only has effect if `format_file` is also enabled
274+
pub fn format_line_number(&mut self, write: bool) -> &mut Self {
275+
self.format.format_line_number = write;
276+
self
277+
}
278+
279+
/// Whether or not to write the source path and line number
280+
///
281+
/// Equivalent to calling both `format_file` and `format_line_number`
282+
/// with `true`
283+
pub fn format_source_path(&mut self, write: bool) -> &mut Self {
284+
self.format_file(write).format_line_number(write);
285+
self
286+
}
287+
265288
/// Whether or not to write the module path in the default format.
266289
pub fn format_module_path(&mut self, write: bool) -> &mut Self {
267290
self.format.format_module_path = write;

0 commit comments

Comments
 (0)