From a4781e12659867003c5a09aa96e7019ee1123b6b Mon Sep 17 00:00:00 2001 From: Jackson Kruger Date: Tue, 16 May 2023 14:24:45 -0700 Subject: [PATCH] Add include_origin to config --- src/lib.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d45fb09..95496a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,7 @@ mod test { report_logs_in_console: true, use_console_color: true, max_level: tracing::Level::TRACE, + include_origin: true, } ) } @@ -109,6 +110,16 @@ mod test { assert_eq!(config.max_level, tracing::Level::WARN); } + + #[test] + fn test_set_include_origin() { + let mut builder = WASMLayerConfigBuilder::new(); + builder.set_include_origin(false); + + let config = builder.build(); + + assert_eq!(config.include_origin, false); + } } pub enum ConsoleConfig { @@ -126,6 +137,8 @@ pub struct WASMLayerConfigBuilder { use_console_color: bool, /// Log events will be reported from this level -- Default is ALL (TRACE) max_level: tracing::Level, + /// Log events will include the source file and line number -- Default is true + include_origin: bool, } impl WASMLayerConfigBuilder { @@ -148,6 +161,12 @@ impl WASMLayerConfigBuilder { self } + // Set whether logs should include the source file and line number + pub fn set_include_origin(&mut self, include_origin: bool) -> &mut WASMLayerConfigBuilder { + self.include_origin = include_origin; + self + } + /// Set if and how events should be displayed in the browser console pub fn set_console_config( &mut self, @@ -178,6 +197,7 @@ impl WASMLayerConfigBuilder { report_logs_in_console: self.report_logs_in_console, use_console_color: self.use_console_color, max_level: self.max_level, + include_origin: self.include_origin, } } } @@ -189,6 +209,7 @@ impl Default for WASMLayerConfigBuilder { report_logs_in_console: true, use_console_color: true, max_level: tracing::Level::TRACE, + include_origin: true, } } } @@ -199,6 +220,7 @@ pub struct WASMLayerConfig { report_logs_in_console: bool, use_console_color: bool, max_level: tracing::Level, + include_origin: bool, } impl core::default::Default for WASMLayerConfig { @@ -208,6 +230,7 @@ impl core::default::Default for WASMLayerConfig { report_logs_in_console: true, use_console_color: true, max_level: tracing::Level::TRACE, + include_origin: true, } } } @@ -302,10 +325,13 @@ impl LookupSpan<'a>> Layer for WASMLayer { let meta = event.metadata(); let level = meta.level(); if self.config.report_logs_in_console { - let origin = meta - .file() - .and_then(|file| meta.line().map(|ln| format!("{}:{}", file, ln))) - .unwrap_or_default(); + let origin = if self.config.include_origin { + meta.file() + .and_then(|file| meta.line().map(|ln| format!("{}:{}", file, ln))) + .unwrap_or_default() + } else { + String::new() + }; if self.config.use_console_color { log4(