Skip to content

Commit

Permalink
feat(level): add default level in config from the builder
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanBarriere committed Aug 20, 2020
1 parent a9e7f2a commit 7cb160a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod test {
report_logs_in_timings: true,
report_logs_in_console: true,
use_console_color: true,
level: tracing::Level::TRACE,
}
)
}
Expand Down Expand Up @@ -86,6 +87,25 @@ mod test {
assert_eq!(config.report_logs_in_console, true);
assert_eq!(config.use_console_color, true);
}

#[test]
fn test_default_config_log_level() {
let builder = WASMLayerConfigBuilder::new();

let config = builder.build();

assert_eq!(config.level, tracing::Level::TRACE);
}

#[test]
fn test_set_config_log_level_warn() {
let mut builder = WASMLayerConfigBuilder::new();
builder.set_level(tracing::Level::WARN);

let config = builder.build();

assert_eq!(config.level, tracing::Level::WARN);
}
}

pub enum ConsoleConfig {
Expand All @@ -101,6 +121,8 @@ pub struct WASMLayerConfigBuilder {
report_logs_in_console: bool,
/// Only relevant if report_logs_in_console is true, this will use color style strings in the console.
use_console_color: bool,
/// Log events will be reported from this level -- Default is ALL (TRACE)
level: tracing::Level
}

impl WASMLayerConfigBuilder {
Expand All @@ -117,6 +139,15 @@ impl WASMLayerConfigBuilder {
self
}

/// Set the minimal level on which events should be displayed
pub fn set_level(
&mut self,
level: tracing::Level,
) -> &mut WASMLayerConfigBuilder {
self.level = level;
self
}

/// Set if and how events should be displayed in the browser console
pub fn set_console_config(
&mut self,
Expand Down Expand Up @@ -146,6 +177,7 @@ impl WASMLayerConfigBuilder {
report_logs_in_timings: self.report_logs_in_timings,
report_logs_in_console: self.report_logs_in_console,
use_console_color: self.use_console_color,
level: self.level.clone(),
}
}
}
Expand All @@ -156,6 +188,7 @@ impl Default for WASMLayerConfigBuilder {
report_logs_in_timings: true,
report_logs_in_console: true,
use_console_color: true,
level: tracing::Level::TRACE,
}
}
}
Expand All @@ -165,6 +198,7 @@ pub struct WASMLayerConfig {
report_logs_in_timings: bool,
report_logs_in_console: bool,
use_console_color: bool,
level: tracing::Level
}

impl core::default::Default for WASMLayerConfig {
Expand All @@ -173,6 +207,7 @@ impl core::default::Default for WASMLayerConfig {
report_logs_in_timings: true,
report_logs_in_console: true,
use_console_color: true,
level: tracing::Level::TRACE
}
}
}
Expand Down Expand Up @@ -203,6 +238,11 @@ fn mark_name(id: &tracing::Id) -> String {
}

impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for WASMLayer {
fn enabled(&self, metadata: &tracing::Metadata<'_>, _: Context<'_, S>) -> bool {
let level = metadata.level();
level <= &self.config.level
}

fn new_span(
&self,
attrs: &tracing::span::Attributes<'_>,
Expand Down

0 comments on commit 7cb160a

Please sign in to comment.