Skip to content

Commit 60fc35d

Browse files
authored
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
1 parent 9629c2f commit 60fc35d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

.changes/emit-traces.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'log': 'minor:feat'
3+
'log-js': 'minor:feat'
4+
---
5+
Add a `tracing` feature to the `log` plugin that emits log messages to the `tracing` system.

plugins/log/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ byte-unit = "5"
3333
log = { workspace = true, features = ["kv_unstable"] }
3434
time = { version = "0.3", features = ["formatting", "local-offset"] }
3535
fern = "0.7"
36+
tracing = { workspace = true, optional = true }
37+
3638

3739
[target."cfg(target_os = \"android\")".dependencies]
3840
android_logger = "0.15"
@@ -47,3 +49,4 @@ objc2-foundation = { version = "0.3", default-features = false, features = [
4749

4850
[features]
4951
colored = ["fern/colored"]
52+
tracing = ["dep:tracing"]

plugins/log/src/lib.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,38 @@ impl Target {
201201
}
202202
}
203203

204+
// Target becomes default and location is added as a parameter
205+
#[cfg(feature = "tracing")]
206+
fn emit_trace(
207+
level: log::Level,
208+
message: &String,
209+
location: Option<&str>,
210+
file: Option<&str>,
211+
line: Option<u32>,
212+
kv: &HashMap<&str, &str>,
213+
) {
214+
macro_rules! emit_event {
215+
($level:expr) => {
216+
tracing::event!(
217+
target: WEBVIEW_TARGET,
218+
$level,
219+
message = %message,
220+
location = location,
221+
file,
222+
line,
223+
?kv
224+
)
225+
};
226+
}
227+
match level {
228+
log::Level::Error => emit_event!(tracing::Level::ERROR),
229+
log::Level::Warn => emit_event!(tracing::Level::WARN),
230+
log::Level::Info => emit_event!(tracing::Level::INFO),
231+
log::Level::Debug => emit_event!(tracing::Level::DEBUG),
232+
log::Level::Trace => emit_event!(tracing::Level::TRACE),
233+
}
234+
}
235+
204236
#[tauri::command]
205237
fn log(
206238
level: LogLevel,
@@ -227,6 +259,8 @@ fn log(
227259
kv.insert(k.as_str(), v.as_str());
228260
}
229261
builder.key_values(&kv);
262+
#[cfg(feature = "tracing")]
263+
emit_trace(level, &message, location, file, line, &kv);
230264

231265
logger().log(&builder.args(format_args!("{message}")).build());
232266
}
@@ -350,7 +384,7 @@ impl Builder {
350384

351385
/// Skip the creation and global registration of a logger
352386
///
353-
/// 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.
387+
/// 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.
354388
/// ```rust
355389
/// static LOGGER: SimpleLogger = SimpleLogger;
356390
///

0 commit comments

Comments
 (0)