Skip to content

Commit

Permalink
feat(log): Custom .log file names (#633)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mellobacon and FabianLars authored Oct 5, 2023
1 parent d5f5d78 commit ac73909
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions plugins/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -167,6 +167,7 @@ pub struct Builder {
timezone_strategy: TimezoneStrategy,
max_file_size: u128,
targets: Vec<LogTarget>,
log_name: Option<String>,
}

impl Default for Builder {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -262,6 +264,29 @@ impl Builder {
self
}

/// Writes logs to the given file. Default: <app_name>.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<S: Into<String>>(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 =
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -347,12 +375,12 @@ impl Builder {

fn get_log_file_path(
dir: &impl AsRef<Path>,
app_name: &str,
log_name: &str,
rotation_strategy: &RotationStrategy,
timezone_strategy: &TimezoneStrategy,
max_file_size: u128,
) -> plugin::Result<PathBuf> {
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;
Expand All @@ -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(
Expand Down

0 comments on commit ac73909

Please sign in to comment.