Skip to content

Commit

Permalink
Add method to FileLogWriterBuilder to set the max log level
Browse files Browse the repository at this point in the history
- fix new clippies
  • Loading branch information
emabee committed Dec 9, 2022
1 parent 12e6571 commit 97bbf79
Show file tree
Hide file tree
Showing 40 changed files with 93 additions and 123 deletions.
2 changes: 1 addition & 1 deletion benches/bench_reconfigurable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn b20_initialize_logger(_: &mut Bencher) {
.unwrap()
.log_to_file(FileSpec::default().directory("log_files"))
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));
}

#[bench]
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn b20_initialize_logger(_: &mut Bencher) {
.unwrap()
.log_to_file(FileSpec::default().directory("log_files"))
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));
}

#[bench]
Expand Down
2 changes: 1 addition & 1 deletion examples/dedup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl LogLineFilter for DedupWriter {
.line(Some(line!()))
.module_path_static(Some("flexi_logger"))
.target("flexi_logger")
.args(format_args!("last record was skipped {} times", skipped))
.args(format_args!("last record was skipped {skipped} times"))
.build(),
)?;
// Log new record
Expand Down
2 changes: 1 addition & 1 deletion examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
// With format
let start = Instant::now();
for s in &structs {
log::info!("{}", format!("{}", s));
log::info!("{}", format!("{s}"));
}
eprintln!("with format: {:?}", start.elapsed()); // 2-7ms
}
Expand Down
11 changes: 3 additions & 8 deletions src/file_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,26 +307,21 @@ mod test {
.to_string();
assert!(
stem.starts_with(&progname),
"stem: {:?}, progname: {:?}",
stem,
progname
"stem: {stem:?}, progname: {progname:?}",
);
if with_timestamp {
// followed by _ and timestamp
assert_eq!(stem.as_bytes()[progname.len()], b'_');
let s_ts = &stem[progname.len() + 1..];
assert!(
chrono::NaiveDateTime::parse_from_str(s_ts, "%Y-%m-%d_%H-%M-%S").is_ok(),
"s_ts: \"{}\"",
s_ts
"s_ts: \"{s_ts}\"",
);
} else {
assert_eq!(
stem.as_bytes().len(),
progname.len(),
"stem: {:?}, progname: {:?}",
stem,
progname
"stem: {stem:?}, progname: {progname:?}",
);
}

Expand Down
5 changes: 1 addition & 4 deletions src/log_specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,7 @@ fn contains_whitespace(s: &str, parse_errs: &mut String) -> bool {
let result = s.chars().any(char::is_whitespace);
if result {
push_err(
&format!(
"ignoring invalid part in log spec '{}' (contains a whitespace)",
s
),
&format!("ignoring invalid part in log spec '{s}' (contains a whitespace)"),
parse_errs,
);
}
Expand Down
6 changes: 1 addition & 5 deletions src/primary_writer/std_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,7 @@ impl LogWriter for StdWriter {
}
buf.clear();
reader.read_line(&mut buf).unwrap();
assert!(
buf.is_empty(),
"Found more log lines than expected: {} ",
buf
);
assert!(buf.is_empty(), "Found more log lines than expected: {buf} ",);
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/writers/file_log_writer/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct FileLogWriterBuilder {

/// Methods for influencing the behavior of the [`FileLogWriter`].
impl FileLogWriterBuilder {
pub(crate) fn new(file_spec: FileSpec) -> FileLogWriterBuilder {
FileLogWriterBuilder {
pub(crate) fn new(file_spec: FileSpec) -> Self {
Self {
o_rotation_config: None,
cfg_print_message: false,
file_spec,
Expand Down Expand Up @@ -155,6 +155,15 @@ impl FileLogWriterBuilder {
self
}

/// Set the maximum log level.
///
/// The default is `log::LevelFilter::Trace`, i.e., all log levels are written.
#[must_use]
pub fn max_level(mut self, max_log_level: log::LevelFilter) -> Self {
self.max_log_level = max_log_level;
self
}

/// Enforces the use of UTC, rather than local time.
#[must_use]
pub fn use_utc(mut self) -> Self {
Expand Down
6 changes: 1 addition & 5 deletions src/writers/file_log_writer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,7 @@ fn validate_logs_in_file(
reader
.read_line(&mut buf)
.expect("validate_logs: can't read file");
assert!(
buf.is_empty(),
"Found more log lines than expected: {} ",
buf
);
assert!(buf.is_empty(), "Found more log lines than expected: {buf} ");
}

#[allow(clippy::type_complexity)]
Expand Down
17 changes: 7 additions & 10 deletions tests/test_age_or_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn test_age_or_size() {
Cleanup::Never,
)
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));
// info!("test correct rotation by age or size");

write_log_lines();
Expand Down Expand Up @@ -75,38 +75,35 @@ fn verify_logs(directory: &Path) {
// read all files
let pattern = directory.display().to_string().add("/*");
let globresults = match glob(&pattern) {
Err(e) => panic!(
"Is this ({}) really a directory? Listing failed with {}",
pattern, e
),
Err(e) => panic!("Is this ({pattern}) really a directory? Listing failed with {e}",),
Ok(globresults) => globresults,
};
let mut no_of_log_files = 0;
let mut total_line_count = 0_usize;
for (index, globresult) in globresults.into_iter().enumerate() {
let mut line_count = 0_usize;
let pathbuf = globresult.unwrap_or_else(|e| panic!("Ups - error occured: {}", e));
let pathbuf = globresult.unwrap_or_else(|e| panic!("Ups - error occured: {e}"));
let f = File::open(&pathbuf)
.unwrap_or_else(|e| panic!("Cannot open file {:?} due to {}", pathbuf, e));
.unwrap_or_else(|e| panic!("Cannot open file {pathbuf:?} due to {e}"));
no_of_log_files += 1;
let mut reader = BufReader::new(f);
let mut buffer = String::new();
while reader.read_line(&mut buffer).unwrap() > 0 {
line_count += 1;
}
println!("file {:?}:\n{}", pathbuf, buffer);
println!("file {pathbuf:?}:\n{buffer}");
if line_count != expected_line_counts[index] {
error_detected = true;
}
total_line_count += line_count;
}

if no_of_log_files != 8 {
println!("wrong file count: {} instead of 8", no_of_log_files);
println!("wrong file count: {no_of_log_files} instead of 8");
error_detected = true;
}
if total_line_count != 16 {
println!("wrong line count: {} instead of 16", total_line_count);
println!("wrong line count: {total_line_count} instead of 16");
error_detected = true;
};

Expand Down
2 changes: 1 addition & 1 deletion tests/test_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_mods() {
.unwrap()
.log_to_stdout()
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

error!("This is an error message");
warn!("This is a warning");
Expand Down
6 changes: 3 additions & 3 deletions tests/test_custom_log_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn work(value: u8) {
}
let handle = logger
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

error!("This is an error message");
warn!("This is a warning");
Expand Down Expand Up @@ -85,13 +85,13 @@ impl LogWriter for CustomWriter {
0 => expected
.iter()
.fold(Vec::new(), |mut acc, (level, module, message)| {
acc.extend(format!("{} [{}] {}", level, module, message).bytes());
acc.extend(format!("{level} [{module}] {message}").bytes());
acc
}),
1 => expected
.iter()
.fold(Vec::new(), |mut acc, (level, _module, message)| {
acc.extend(format!("{}: {}", level, message).bytes());
acc.extend(format!("{level}: {message}").bytes());
acc
}),
COUNT..=u8::MAX => {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_default_file_and_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_default_file_and_writer() {
.log_to_file_and_writer(file_spec_foo, Box::new(bar_writer))
.format(detailed_format)
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

error!("This is an error message");
warn!("This is a warning");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_error_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn work(value: u8) {
// start logger, and force its immediate drop
let _logger_handle = logger
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));
}

error!("This is an error message");
Expand Down
12 changes: 3 additions & 9 deletions tests/test_external_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn work(value: u8) {

let logger = logger
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

// write some log lines to initialize the file
info!("XXX 1 AAA");
Expand All @@ -59,17 +59,11 @@ fn work(value: u8) {
let lines = count_lines(&file_path);
match std::fs::remove_file(file_path.clone()) {
Ok(()) => {
println!(
"Removed the log file {:?}, which had {} lines",
file_path, lines
);
println!("Removed the log file {file_path:?}, which had {lines} lines");
logger.reopen_outputfile().unwrap();
}
Err(e) => {
panic!(
"Cannot remove log file {:?}, i = {}, reason {:?}",
file_path, i, e
)
panic!("Cannot remove log file {file_path:?}, i = {i}, reason {e:?}")
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/test_external_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn work(value: u8) {
{
let logger = logger
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

// write some log lines to initialize the file
info!("XXX 1 AAA");
Expand All @@ -71,8 +71,7 @@ fn work(value: u8) {
}
Err(e) => {
panic!(
"Cannot rename log file {:?} to {:?} due to {:?}",
file_path, target_path, e
"Cannot rename log file {file_path:?} to {target_path:?} due to {e:?}",
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn work(value: u8) {

let handle = logger
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

error!("This is an error message");
warn!("This is a warning");
Expand All @@ -103,7 +103,7 @@ mod platform {
#[cfg(target_family = "unix")]
pub fn check_link(link_name: &str) {
match std::fs::symlink_metadata(link_name) {
Err(e) => panic!("error with symlink: {}", e),
Err(e) => panic!("error with symlink: {e}"),
Ok(metadata) => assert!(metadata.file_type().is_symlink(), "not a symlink"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_force_utc_1_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_force_utc_1_panic() {
.unwrap()
.format_for_stderr(flexi_logger::detailed_format)
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));
info!("MUST BE REACHED");
DeferredNow::force_utc();
info!("MUST NOT BE REACHED");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_force_utc_2_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_force_utc_2_panic() {
.unwrap()
.format_for_stderr(flexi_logger::detailed_format)
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));
info!("test");
DeferredNow::force_utc();
info!("MUST NOT BE REACHED");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_force_utc_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn test_force_utc_3() {
let _ = Logger::try_with_str("info")
.unwrap()
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));
DeferredNow::force_utc();
info!("must be printed");
}
4 changes: 2 additions & 2 deletions tests/test_force_utc_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn test_force_utc_4() {
.suppress_timestamp(),
)
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

info!("must be printed");
let now = Local::now();
Expand All @@ -36,7 +36,7 @@ fn test_force_utc_4() {
// local TZ is different from UTC -> verify that UTC was written to the file
let now_local = now.naive_local();
let diff = (now_local - d).num_seconds();
println!("d: {}, now_local: {}, diff: {}", d, now_local, diff);
println!("d: {d}, now_local: {now_local}, diff: {diff}");
assert!(diff >= 10);
}
}
2 changes: 1 addition & 1 deletion tests/test_mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn test_mods() {
.directory(self::test_utils::dir()),
)
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

error!("This is an error message");
warn!("This is a warning");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mods_off.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn test_mods_off() {
.directory(self::test_utils::dir()),
)
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

error!("This is an error message");
warn!("This is a warning");
Expand Down
2 changes: 1 addition & 1 deletion tests/test_multi_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn test() {
.add_writer("Sec", sec_writer)
.add_writer("Alert", alert_logger())
.start()
.unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));
.unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

// Explicitly send logs to different loggers
error!(target : "{Sec}", "This is a security-relevant error message");
Expand Down
Loading

0 comments on commit 97bbf79

Please sign in to comment.