From 60fc35d35cccaf1654eceb4446ecf0f89dc15502 Mon Sep 17 00:00:00 2001 From: Jim <8757851+3lpsy@users.noreply.github.com> Date: Sun, 6 Apr 2025 21:33:52 -0500 Subject: [PATCH] Support emitting traces behind feature flag (#2576) * Let logging be skippable so user can handle logger() * Add changes file * changes meta info * Update documentation * Support emitting to tracing behind feature flag * typo * rename key_values => kv * changes file * Remove rebase skip-logger * fix format --- .changes/emit-traces.md | 5 +++++ plugins/log/Cargo.toml | 3 +++ plugins/log/src/lib.rs | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .changes/emit-traces.md diff --git a/.changes/emit-traces.md b/.changes/emit-traces.md new file mode 100644 index 00000000..569d2504 --- /dev/null +++ b/.changes/emit-traces.md @@ -0,0 +1,5 @@ +--- +'log': 'minor:feat' +'log-js': 'minor:feat' +--- +Add a `tracing` feature to the `log` plugin that emits log messages to the `tracing` system. diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index a3b8b290..d78d6e84 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -33,6 +33,8 @@ byte-unit = "5" log = { workspace = true, features = ["kv_unstable"] } time = { version = "0.3", features = ["formatting", "local-offset"] } fern = "0.7" +tracing = { workspace = true, optional = true } + [target."cfg(target_os = \"android\")".dependencies] android_logger = "0.15" @@ -47,3 +49,4 @@ objc2-foundation = { version = "0.3", default-features = false, features = [ [features] colored = ["fern/colored"] +tracing = ["dep:tracing"] diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 3c4abdd7..5eba13e5 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -201,6 +201,38 @@ impl Target { } } +// Target becomes default and location is added as a parameter +#[cfg(feature = "tracing")] +fn emit_trace( + level: log::Level, + message: &String, + location: Option<&str>, + file: Option<&str>, + line: Option, + kv: &HashMap<&str, &str>, +) { + macro_rules! emit_event { + ($level:expr) => { + tracing::event!( + target: WEBVIEW_TARGET, + $level, + message = %message, + location = location, + file, + line, + ?kv + ) + }; + } + match level { + log::Level::Error => emit_event!(tracing::Level::ERROR), + log::Level::Warn => emit_event!(tracing::Level::WARN), + log::Level::Info => emit_event!(tracing::Level::INFO), + log::Level::Debug => emit_event!(tracing::Level::DEBUG), + log::Level::Trace => emit_event!(tracing::Level::TRACE), + } +} + #[tauri::command] fn log( level: LogLevel, @@ -227,6 +259,8 @@ fn log( kv.insert(k.as_str(), v.as_str()); } builder.key_values(&kv); + #[cfg(feature = "tracing")] + emit_trace(level, &message, location, file, line, &kv); logger().log(&builder.args(format_args!("{message}")).build()); } @@ -350,7 +384,7 @@ impl Builder { /// Skip the creation and global registration of a logger /// - /// If you wish to use your own global logger, you must call `skip_logger` so that the plugin does not attempt to set a second global logger. In this configuration, no logger will be created and the plugin's `log` command will rely on the result of `log::logger()`. You will be responsible for configuring the logger yourself and any included targets will be ignored. This can also be used with `tracing-log` or if running tests in parallel that require the plugin to be registered. + /// If you wish to use your own global logger, you must call `skip_logger` so that the plugin does not attempt to set a second global logger. In this configuration, no logger will be created and the plugin's `log` command will rely on the result of `log::logger()`. You will be responsible for configuring the logger yourself and any included targets will be ignored. If ever initializing the plugin multiple times, such as if registering the plugin while testing, call this method to avoid panicking when registering multiple loggers. For interacting with `tracing`, you can leverage the `tracing-log` logger to forward logs to `tracing` or enable the `tracing` feature for this plugin to emit events directly to the tracing system. Both scenarios require calling this method. /// ```rust /// static LOGGER: SimpleLogger = SimpleLogger; ///