Skip to content

Commit 0248150

Browse files
authored
feat(log): Add skip_logger setting (#2377)
1 parent e90cd9f commit 0248150

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

.changes/skip-logger.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 `is_skip_logger` flag to the Log Plugin `Builder` struct, a `skip_logger()` method to the Builder, and logic to avoid acquiring (creating) a logger and attaching it to the global logger. Since acquire_logger is pub, a `LoggerNotInitialized` is added and returned if it's called when the `is_skip_looger` flag is set. Overall, this feature permits a user to avoid calling `attach_logger` which can only be called once in a program's lifetime and allows the user to control the logger returned from `logger()`. Additionally, it also will allow users to generate multiple Tauri Mock apps in test suites that run and parallel and have the `log` plugin attached (assuming they use `skip_logger()`).

plugins/log/src/lib.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub enum Error {
5858
TimeFormat(#[from] time::error::Format),
5959
#[error(transparent)]
6060
InvalidFormatDescription(#[from] time::error::InvalidFormatDescription),
61+
#[error("Internal logger disabled and cannot be acquired or attached")]
62+
LoggerNotInitialized,
6163
}
6264

6365
/// An enum representing the available verbosity levels of the logger.
@@ -230,6 +232,7 @@ pub struct Builder {
230232
timezone_strategy: TimezoneStrategy,
231233
max_file_size: u128,
232234
targets: Vec<Target>,
235+
is_skip_logger: bool,
233236
}
234237

235238
impl Default for Builder {
@@ -258,6 +261,7 @@ impl Default for Builder {
258261
timezone_strategy: DEFAULT_TIMEZONE_STRATEGY,
259262
max_file_size: DEFAULT_MAX_FILE_SIZE,
260263
targets: DEFAULT_LOG_TARGETS.into(),
264+
is_skip_logger: false,
261265
}
262266
}
263267
}
@@ -339,6 +343,22 @@ impl Builder {
339343
self
340344
}
341345

346+
/// Skip the creation and global registration of a logger
347+
///
348+
/// 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.
349+
/// ```rust
350+
/// static LOGGER: SimpleLogger = SimpleLogger;
351+
///
352+
/// log::set_logger(&SimpleLogger)?;
353+
/// log::set_max_level(LevelFilter::Info);
354+
/// tauri_plugin_log::Builder::new()
355+
/// .skip_logger();
356+
/// ```
357+
pub fn skip_logger(mut self) -> Self {
358+
self.is_skip_logger = true;
359+
self
360+
}
361+
342362
/// Adds a collection of targets to the logger.
343363
///
344364
/// ```rust
@@ -481,6 +501,9 @@ impl Builder {
481501
self,
482502
app_handle: &AppHandle<R>,
483503
) -> Result<(TauriPlugin<R>, log::LevelFilter, Box<dyn log::Log>), Error> {
504+
if self.is_skip_logger {
505+
return Err(Error::LoggerNotInitialized);
506+
}
484507
let plugin = Self::plugin_builder();
485508
let (max_level, log) = Self::acquire_logger(
486509
app_handle,
@@ -497,17 +520,17 @@ impl Builder {
497520
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
498521
Self::plugin_builder()
499522
.setup(move |app_handle, _api| {
500-
let (max_level, log) = Self::acquire_logger(
501-
app_handle,
502-
self.dispatch,
503-
self.rotation_strategy,
504-
self.timezone_strategy,
505-
self.max_file_size,
506-
self.targets,
507-
)?;
508-
509-
attach_logger(max_level, log)?;
510-
523+
if !self.is_skip_logger {
524+
let (max_level, log) = Self::acquire_logger(
525+
app_handle,
526+
self.dispatch,
527+
self.rotation_strategy,
528+
self.timezone_strategy,
529+
self.max_file_size,
530+
self.targets,
531+
)?;
532+
attach_logger(max_level, log)?;
533+
}
511534
Ok(())
512535
})
513536
.build()

0 commit comments

Comments
 (0)