File-rotation
Create a size-based file rotation policy for file_sink(...). This helper defines how large a file may grow before rotation and how many historical backups should be retained.
Interface
pub fn file_rotation(max_bytes : Int, max_backups~ : Int = 1) -> FileRotation {}input
max_bytes : Int- Maximum active file size threshold before rotation.max_backups : Int- Number of retained backup files.
output
FileRotation- Rotation policy usable by direct file sinks or config-driven sink state.
Explanation
Detailed rules explaining key parameters and behaviors
max_bytes <= 0is normalized to1.max_backups <= 0is normalized to1.- Rotation is size-based only.
- This policy is consumed by
file_sink(...), file policy helpers, and config-driven file sink assembly.
How to Use
Here are some specific examples provided.
When Need Bounded Local Log Files
When a file should not grow without limit:
let sink = file_sink(
"app.log",
rotation=Some(file_rotation(1024 * 1024, max_backups=3)),
)In this example, the file rotates after the configured size threshold.
And up to three backup files are retained.
When Need Runtime Policy Composition
When file sink policy is assembled explicitly:
let policy = FileSinkPolicy::new(rotation=Some(file_rotation(4096, max_backups=2)))In this example, rotation can be bundled with append and auto-flush settings.
Error Case
e.g.:
If
max_bytesis non-positive, it is clamped to1.If
max_backupsis non-positive, it is clamped to1.
Notes
This API defines policy only; it does not open files by itself.
Rotation currently focuses on size thresholds rather than time schedules or compression.