From ac7390973b85fed127790e79a61518c4195da79b Mon Sep 17 00:00:00 2001 From: mellobacon <42365887+mellobacon@users.noreply.github.com> Date: Thu, 5 Oct 2023 08:24:04 -0500 Subject: [PATCH] feat(log): Custom .log file names (#633) * feat (log): custom .log file names * fix (log): corrected typo * chore (log): refactor * chore (log): replaced empty string with Option * Apply suggestions from code review * Update plugins/log/src/lib.rs --------- Co-authored-by: Fabian-Lars --- plugins/log/src/lib.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 9db4aa4e0b..f80360ded4 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -118,7 +118,7 @@ pub enum LogTarget { /// /// The plugin will ensure the directory exists before writing logs. Folder(PathBuf), - /// Write logs to the OS specififc logs directory. + /// Write logs to the OS specific logs directory. /// /// ### Platform-specific /// @@ -167,6 +167,7 @@ pub struct Builder { timezone_strategy: TimezoneStrategy, max_file_size: u128, targets: Vec, + log_name: Option, } impl Default for Builder { @@ -189,6 +190,7 @@ impl Default for Builder { timezone_strategy: DEFAULT_TIMEZONE_STRATEGY, max_file_size: DEFAULT_MAX_FILE_SIZE, targets: DEFAULT_LOG_TARGETS.into(), + log_name: None, } } } @@ -262,6 +264,29 @@ impl Builder { self } + /// Writes logs to the given file. Default: .log) + /// + /// Note: This does not modify the directory logs go into. For that refer to `LogTarget::Folder`. + /// + /// # Examples + /// + /// ``` + /// use tauri_plugin_log::Builder; + /// let name = "custom-name"; + /// let builder = Builder::default() + /// .targets([ + /// LogTarget::LogDir + /// ]) + /// .log_name(name) + /// .build() + /// ); // Outputs content to custom-name.log + /// + /// ``` + pub fn log_name>(mut self, log_name: S) -> Self { + self.log_name = Some(log_name.into()); + self + } + #[cfg(feature = "colored")] pub fn with_colors(self, colors: fern::colors::ColoredLevelConfig) -> Self { let format = @@ -284,7 +309,10 @@ impl Builder { plugin::Builder::new("log") .invoke_handler(tauri::generate_handler![log]) .setup(move |app_handle| { - let app_name = &app_handle.package_info().name; + let log_name = self + .log_name + .as_deref() + .unwrap_or_else(|| &app_handle.package_info().name); // setup targets for target in &self.targets { @@ -298,7 +326,7 @@ impl Builder { fern::log_file(get_log_file_path( &path, - app_name, + log_name, &self.rotation_strategy, &self.timezone_strategy, self.max_file_size, @@ -313,7 +341,7 @@ impl Builder { fern::log_file(get_log_file_path( &path, - app_name, + log_name, &self.rotation_strategy, &self.timezone_strategy, self.max_file_size, @@ -347,12 +375,12 @@ impl Builder { fn get_log_file_path( dir: &impl AsRef, - app_name: &str, + log_name: &str, rotation_strategy: &RotationStrategy, timezone_strategy: &TimezoneStrategy, max_file_size: u128, ) -> plugin::Result { - let path = dir.as_ref().join(format!("{app_name}.log")); + let path = dir.as_ref().join(format!("{log_name}.log")); if path.exists() { let log_size = File::open(&path)?.metadata()?.len() as u128; @@ -361,7 +389,7 @@ fn get_log_file_path( RotationStrategy::KeepAll => { let to = dir.as_ref().join(format!( "{}_{}.log", - app_name, + log_name, timezone_strategy .get_now() .format(