Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to create only date log file name #153

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ todo
tests/*logspec.toml
*~
.*~
.vscode
.vscode
.idea/
17 changes: 14 additions & 3 deletions src/file_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ impl FileSpec {
}
}

/// Makes the logger not include a basename into the names of the log files
///
/// Equivalent to `basename("")`.
#[must_use]
pub fn suppress_basename(mut self) -> Self {
self.basename = "".into();
self
}

/// The specified String is used as the basename of the log file name,
/// instead of the program name. Using a file separator within the argument is discouraged.
#[must_use]
Expand Down Expand Up @@ -214,10 +223,11 @@ impl FileSpec {
filename.reserve(50);

if let Some(discriminant) = &self.o_discriminant {
filename.push('_');
if !filename.is_empty() { filename.push('_'); }
filename.push_str(discriminant);
}
if let Some(timestamp) = &self.timestamp_cfg.get_timestamp() {
if !filename.is_empty() { filename.push('_'); }
filename.push_str(timestamp);
}
if let Some(infix) = o_infix {
Expand All @@ -239,10 +249,11 @@ impl FileSpec {
filename.reserve(50);

if let Some(discriminant) = &self.o_discriminant {
filename.push('_');
if !filename.is_empty() { filename.push('_'); }
filename.push_str(discriminant);
}
if let Some(timestamp) = &self.timestamp_cfg.get_timestamp() {
if !filename.is_empty() { filename.push('_'); }
filename.push_str(timestamp);
}
filename.push_str(infix_pattern);
Expand All @@ -266,7 +277,7 @@ impl FileSpec {
}
}

const TS_USCORE_DASHES_USCORE_DASHES: &str = "_%Y-%m-%d_%H-%M-%S";
const TS_USCORE_DASHES_USCORE_DASHES: &str = "%Y-%m-%d_%H-%M-%S";

#[derive(Debug, Clone, Eq, PartialEq)]
enum TimestampCfg {
Expand Down
17 changes: 16 additions & 1 deletion tests/test_file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod test_utils;
use flexi_logger::{detailed_format, opt_format, Cleanup, Criterion, FileSpec, Logger, Naming};
use log::*;

const COUNT: u8 = 6;
const COUNT: u8 = 8;

#[test]
fn test_write_modes() {
Expand Down Expand Up @@ -73,6 +73,21 @@ fn work(value: u8) {
.discriminant("foo"),
);
}
6 => {
logger = logger.format(opt_format).log_to_file(
FileSpec::default()
.directory(self::test_utils::dir())
.suppress_basename(),
);
}
7 => {
logger = logger.format(opt_format).log_to_file(
FileSpec::default()
.directory(self::test_utils::dir())
.suppress_basename()
.discriminant("foo"),
);
}
COUNT..=u8::MAX => {
unreachable!("dtrtgfg")
}
Expand Down
Loading