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 <fabianlars@fabianlars.de>
pull/636/head
mellobacon 2 years ago committed by GitHub
parent d5f5d78830
commit ac7390973b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<LogTarget>,
log_name: Option<String>,
}
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: <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 =
@ -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<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;
@ -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(

Loading…
Cancel
Save