diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index e7848ff4..a6997fe2 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -58,6 +58,8 @@ pub enum Error { TimeFormat(#[from] time::error::Format), #[error(transparent)] InvalidFormatDescription(#[from] time::error::InvalidFormatDescription), + #[error("Internal logger disabled and cannot be acquired or attached")] + LoggerNotInitialized, } /// An enum representing the available verbosity levels of the logger. @@ -230,6 +232,7 @@ pub struct Builder { timezone_strategy: TimezoneStrategy, max_file_size: u128, targets: Vec, + is_skip_logger: bool, } impl Default for Builder { @@ -258,6 +261,7 @@ impl Default for Builder { timezone_strategy: DEFAULT_TIMEZONE_STRATEGY, max_file_size: DEFAULT_MAX_FILE_SIZE, targets: DEFAULT_LOG_TARGETS.into(), + is_skip_logger: false, } } } @@ -339,6 +343,16 @@ impl Builder { self } + /// Skips logger acquisition and attachment. Certain functions such as split will be unusable. + /// ```rust + /// tauri_plugin_log::Builder::new() + /// .skip_logger(); + /// ``` + pub fn skip_logger(mut self) -> Self { + self.is_skip_logger = true; + self + } + /// Adds a collection of targets to the logger. /// /// ```rust @@ -481,6 +495,9 @@ impl Builder { self, app_handle: &AppHandle, ) -> Result<(TauriPlugin, log::LevelFilter, Box), Error> { + if self.is_skip_logger { + return Err(Error::LoggerNotInitialized); + } let plugin = Self::plugin_builder(); let (max_level, log) = Self::acquire_logger( app_handle, @@ -497,17 +514,17 @@ impl Builder { pub fn build(self) -> TauriPlugin { Self::plugin_builder() .setup(move |app_handle, _api| { - let (max_level, log) = Self::acquire_logger( - app_handle, - self.dispatch, - self.rotation_strategy, - self.timezone_strategy, - self.max_file_size, - self.targets, - )?; - - attach_logger(max_level, log)?; - + if !self.is_skip_logger { + let (max_level, log) = Self::acquire_logger( + app_handle, + self.dispatch, + self.rotation_strategy, + self.timezone_strategy, + self.max_file_size, + self.targets, + )?; + attach_logger(max_level, log)?; + } Ok(()) }) .build()