Let logging be skippable so user can handle logger()

pull/2377/head
3lpsy 6 months ago
parent 6ae853c2e6
commit 7931603da9
No known key found for this signature in database
GPG Key ID: 9020819A703D4CEC

@ -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<Target>,
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<R>,
) -> Result<(TauriPlugin<R>, log::LevelFilter, Box<dyn log::Log>), 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,6 +514,7 @@ impl Builder {
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
Self::plugin_builder()
.setup(move |app_handle, _api| {
if !self.is_skip_logger {
let (max_level, log) = Self::acquire_logger(
app_handle,
self.dispatch,
@ -505,9 +523,8 @@ impl Builder {
self.max_file_size,
self.targets,
)?;
attach_logger(max_level, log)?;
}
Ok(())
})
.build()

Loading…
Cancel
Save