Skip to content

Commit fc0b0e6

Browse files
committed
feat(stackable-telemetry): Allow customization of the rolling file appender max log files
1 parent 363f492 commit fc0b0e6

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

crates/stackable-telemetry/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ All notable changes to this project will be documented in this file.
77
### Added
88

99
- Allow customization of the rolling file appender [#995].
10-
- rotation period
11-
- filename suffix
10+
- Add required `filename_suffix` field.
11+
- Add `with_rotation_period` method.
12+
- Add `with_max_log_files` method.
1213

1314
[#995]: https://github.com/stackabletech/operator-rs/pull/995
1415

crates/stackable-telemetry/src/tracing/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ impl Tracing {
277277
file_log_dir,
278278
rotation_period,
279279
filename_suffix,
280+
max_log_files,
280281
} = &self.file_log_settings
281282
{
282283
let env_filter_layer = env_filter_builder(
@@ -287,8 +288,15 @@ impl Tracing {
287288
let file_appender = RollingFileAppender::builder()
288289
.rotation(rotation_period.clone())
289290
.filename_prefix(self.service_name.to_string())
290-
.filename_suffix(filename_suffix)
291-
.max_log_files(6)
291+
.filename_suffix(filename_suffix);
292+
293+
let file_appender = if let Some(max_log_files) = max_log_files {
294+
file_appender.max_log_files(*max_log_files)
295+
} else {
296+
file_appender
297+
};
298+
299+
let file_appender = file_appender
292300
.build(file_log_dir)
293301
.context(InitRollingFileAppenderSnafu)?;
294302

@@ -732,6 +740,7 @@ mod test {
732740
file_log_dir: PathBuf::from("/abc_file_dir"),
733741
rotation_period: Rotation::NEVER,
734742
filename_suffix: "tracing-rs.json".to_owned(),
743+
max_log_files: None,
735744
}
736745
);
737746
assert_eq!(

crates/stackable-telemetry/src/tracing/settings/file_log.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub enum FileLogSettings {
2727

2828
/// Suffix for log filenames.
2929
filename_suffix: String,
30+
31+
/// Keep the last `n` files on disk.
32+
max_log_files: Option<usize>,
3033
},
3134
}
3235

@@ -49,6 +52,7 @@ pub struct FileLogSettingsBuilder {
4952
pub(crate) file_log_dir: PathBuf,
5053
pub(crate) rotation_period: Rotation,
5154
pub(crate) filename_suffix: String,
55+
pub(crate) max_log_files: Option<usize>,
5256
}
5357

5458
impl FileLogSettingsBuilder {
@@ -58,13 +62,20 @@ impl FileLogSettingsBuilder {
5862
self
5963
}
6064

65+
/// Set maximum number of log files to keep.
66+
pub fn with_max_log_files(mut self, max_log_files: usize) -> Self {
67+
self.max_log_files = Some(max_log_files);
68+
self
69+
}
70+
6171
/// Consumes self and returns a valid [`FileLogSettings`] instance.
6272
pub fn build(self) -> FileLogSettings {
6373
FileLogSettings::Enabled {
6474
common_settings: self.common_settings,
6575
file_log_dir: self.file_log_dir,
6676
rotation_period: self.rotation_period,
6777
filename_suffix: self.filename_suffix,
78+
max_log_files: self.max_log_files,
6879
}
6980
}
7081
}
@@ -97,12 +108,14 @@ mod test {
97108
file_log_dir: PathBuf::from("/logs"),
98109
rotation_period: Rotation::HOURLY,
99110
filename_suffix: "tracing-rs.log".to_owned(),
111+
max_log_files: Some(6),
100112
};
101113
let result = Settings::builder()
102114
.with_environment_variable("hello")
103115
.with_default_level(LevelFilter::DEBUG)
104116
.file_log_settings_builder(PathBuf::from("/logs"), "tracing-rs.json")
105117
.with_rotation_period(Rotation::HOURLY)
118+
.with_max_log_files(6)
106119
.build();
107120

108121
assert_eq!(expected, result);

crates/stackable-telemetry/src/tracing/settings/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl SettingsBuilder {
9898
file_log_dir: path.as_ref().to_path_buf(),
9999
rotation_period: Rotation::NEVER,
100100
filename_suffix: filename_suffix.into(),
101+
max_log_files: None,
101102
}
102103
}
103104

0 commit comments

Comments
 (0)