Skip to content

Commit 03a39ca

Browse files
authored
feat(logging): log file rotation (#1088)
1 parent d392660 commit 03a39ca

File tree

7 files changed

+424
-12
lines changed

7 files changed

+424
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datadog-log-ffi/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,21 @@ pub extern "C" fn ddog_logger_disable_std() -> Option<Box<Error>> {
5050
pub struct FileConfig<'a> {
5151
/// Path to the log file
5252
pub path: CharSlice<'a>,
53+
/// Maximum total number of files (current + rotated) to keep on disk.
54+
/// When this limit is exceeded, the oldest rotated files are deleted.
55+
/// Set to 0 to disable file cleanup.
56+
pub max_files: u64,
57+
/// Maximum size in bytes for each log file.
58+
/// Set to 0 to disable size-based rotation.
59+
pub max_size_bytes: u64,
5360
}
5461

5562
impl<'a> From<FileConfig<'a>> for logger::FileConfig {
5663
fn from(config: FileConfig<'a>) -> Self {
5764
logger::FileConfig {
5865
path: config.path.to_string(),
66+
max_files: config.max_files,
67+
max_size_bytes: config.max_size_bytes,
5968
}
6069
}
6170
}

datadog-log/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ddcommon-ffi = { path = "../ddcommon-ffi", default-features = false }
1414
tracing = { version = "0.1", default-features = false }
1515
tracing-subscriber = { version = "0.3.18", default-features = false, features = ["json", "env-filter"] }
1616
tracing-appender = "0.2.3"
17+
chrono = { version = "0.4.38", default-features = false, features = ["clock", "std"] }
1718

1819
[dev-dependencies]
1920
tempfile = "3.10"

datadog-log/src/logger.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use crate::writers::{FileWriter, StdWriter};
55
use ddcommon_ffi::Error;
6-
use std::path::Path;
76
use std::sync::Mutex;
87
use std::sync::OnceLock;
98
use tracing::subscriber::DefaultGuard;
@@ -42,6 +41,13 @@ pub enum LogEventLevel {
4241
pub struct FileConfig {
4342
/// Path where log files will be written.
4443
pub path: String,
44+
/// Maximum size in bytes for each log file.
45+
/// Set to 0 to disable size-based rotation.
46+
pub max_size_bytes: u64,
47+
/// Maximum total number of files (current + rotated) to keep on disk.
48+
/// When this limit is exceeded, the oldest rotated files are deleted.
49+
/// Set to 0 to disable file cleanup.
50+
pub max_files: u64,
4551
}
4652

4753
/// Target for standard stream output.
@@ -134,7 +140,7 @@ impl Logger {
134140

135141
// Add file layer if configured
136142
if let Some(file_config) = &self.file_config {
137-
if let Ok(file_layer) = file_layer(&file_config.path) {
143+
if let Ok(file_layer) = file_layer(file_config) {
138144
layers.push(file_layer);
139145
}
140146
}
@@ -215,13 +221,12 @@ fn std_layer(
215221

216222
#[allow(clippy::type_complexity)]
217223
fn file_layer(
218-
path: &str,
224+
config: &FileConfig,
219225
) -> Result<
220226
Box<dyn Layer<Layered<reload::Layer<EnvFilter, Registry>, Registry>> + Send + Sync + 'static>,
221227
Error,
222228
> {
223-
let file_path = Path::new(path);
224-
let writer = FileWriter::new(file_path)
229+
let writer = FileWriter::new(config)
225230
.map_err(|e| Error::from(format!("Failed to create file writer: {}", e)))?;
226231

227232
Ok(fmt::layer()
@@ -516,6 +521,8 @@ mod tests {
516521

517522
let file_config = FileConfig {
518523
path: log_path.to_string_lossy().to_string(),
524+
max_files: 0,
525+
max_size_bytes: 0,
519526
};
520527

521528
logger
@@ -582,6 +589,8 @@ mod tests {
582589
let log_path = temp_dir.path().join("test.log");
583590
let file_config = FileConfig {
584591
path: log_path.to_string_lossy().to_string(),
592+
max_size_bytes: 0,
593+
max_files: 0,
585594
};
586595
logger
587596
.configure_file(file_config)

0 commit comments

Comments
 (0)