From c783b077d9452813f3aeec8089678edb241f6fde Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 29 Aug 2025 15:37:05 -0300 Subject: [PATCH 1/3] feat(log): re-export the log crate --- .changes/re-export-log.md | 6 ++++++ plugins/log/src/lib.rs | 1 + 2 files changed, 7 insertions(+) create mode 100644 .changes/re-export-log.md diff --git a/.changes/re-export-log.md b/.changes/re-export-log.md new file mode 100644 index 0000000000..9bee9b6005 --- /dev/null +++ b/.changes/re-export-log.md @@ -0,0 +1,6 @@ +--- +"log": patch +"log-js": patch +--- + +Re-export the log crate. diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index de5c5d54e1..e660312595 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -30,6 +30,7 @@ use tauri::{AppHandle, Emitter}; use time::{macros::format_description, OffsetDateTime}; pub use fern; +pub use log; pub const WEBVIEW_TARGET: &str = "webview"; From cccd3f1d7b1d1752975ac13326dd1604efb0795a Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 30 Aug 2025 22:54:44 -0300 Subject: [PATCH 2/3] code review --- .changes/re-export-log.md | 4 ++-- plugins/log/src/commands.rs | 41 +++++++++++++++++++++++++++++++++++++ plugins/log/src/lib.rs | 38 +++------------------------------- 3 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 plugins/log/src/commands.rs diff --git a/.changes/re-export-log.md b/.changes/re-export-log.md index 9bee9b6005..87e57d5b24 100644 --- a/.changes/re-export-log.md +++ b/.changes/re-export-log.md @@ -1,6 +1,6 @@ --- -"log": patch -"log-js": patch +"log": minor +"log-js": minor --- Re-export the log crate. diff --git a/plugins/log/src/commands.rs b/plugins/log/src/commands.rs new file mode 100644 index 0000000000..6a66e642d7 --- /dev/null +++ b/plugins/log/src/commands.rs @@ -0,0 +1,41 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +use std::collections::HashMap; + +use log::RecordBuilder; + +use crate::{LogLevel, WEBVIEW_TARGET}; + +#[tauri::command] +pub fn log( + level: LogLevel, + message: String, + location: Option<&str>, + file: Option<&str>, + line: Option, + key_values: Option>, +) { + let level = log::Level::from(level); + + let target = if let Some(location) = location { + format!("{WEBVIEW_TARGET}:{location}") + } else { + WEBVIEW_TARGET.to_string() + }; + + let mut builder = RecordBuilder::new(); + builder.level(level).target(&target).file(file).line(line); + + let key_values = key_values.unwrap_or_default(); + let mut kv = HashMap::new(); + for (k, v) in key_values.iter() { + kv.insert(k.as_str(), v.as_str()); + } + builder.key_values(&kv); + #[cfg(feature = "tracing")] + emit_trace(level, &message, location, file, line, &kv); + + log::logger().log(&builder.args(format_args!("{message}")).build()); +} diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index e660312595..aae42aead8 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -10,12 +10,10 @@ )] use fern::{Filter, FormatCallback}; -use log::{logger, RecordBuilder}; use log::{LevelFilter, Record}; use serde::Serialize; use serde_repr::{Deserialize_repr, Serialize_repr}; use std::borrow::Cow; -use std::collections::HashMap; use std::{ fmt::Arguments, fs::{self, File}, @@ -32,6 +30,8 @@ use time::{macros::format_description, OffsetDateTime}; pub use fern; pub use log; +mod commands; + pub const WEBVIEW_TARGET: &str = "webview"; #[cfg(target_os = "ios")] @@ -239,38 +239,6 @@ fn emit_trace( } } -#[tauri::command] -fn log( - level: LogLevel, - message: String, - location: Option<&str>, - file: Option<&str>, - line: Option, - key_values: Option>, -) { - let level = log::Level::from(level); - - let target = if let Some(location) = location { - format!("{WEBVIEW_TARGET}:{location}") - } else { - WEBVIEW_TARGET.to_string() - }; - - let mut builder = RecordBuilder::new(); - builder.level(level).target(&target).file(file).line(line); - - let key_values = key_values.unwrap_or_default(); - let mut kv = HashMap::new(); - for (k, v) in key_values.iter() { - 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()); -} - pub struct Builder { dispatch: fern::Dispatch, rotation_strategy: RotationStrategy, @@ -529,7 +497,7 @@ impl Builder { } fn plugin_builder() -> plugin::Builder { - plugin::Builder::new("log").invoke_handler(tauri::generate_handler![log]) + plugin::Builder::new("log").invoke_handler(tauri::generate_handler![commands::log]) } #[allow(clippy::type_complexity)] From 1ca741c44c60c07361387c18ee460d3a289054fe Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 31 Aug 2025 09:59:58 +0800 Subject: [PATCH 3/3] Move emit_trace --- plugins/log/src/commands.rs | 32 ++++++++++++++++++++++++++++++++ plugins/log/src/lib.rs | 32 -------------------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/plugins/log/src/commands.rs b/plugins/log/src/commands.rs index 6a66e642d7..c402db76ea 100644 --- a/plugins/log/src/commands.rs +++ b/plugins/log/src/commands.rs @@ -39,3 +39,35 @@ pub fn log( log::logger().log(&builder.args(format_args!("{message}")).build()); } + +// 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), + } +} diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index aae42aead8..c0642d4178 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -207,38 +207,6 @@ 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), - } -} - pub struct Builder { dispatch: fern::Dispatch, rotation_strategy: RotationStrategy,