2.7 KiB
name, group, category, update-time, description, key-word
| name | group | category | update-time | description | key-word | ||||
|---|---|---|---|---|---|---|---|---|---|
| with-file-rotation | api | config | 20260520 | Add or replace size-based file rotation on an existing file logger config preset. |
|
With-file-rotation
Add or replace a size-based file rotation policy on an existing LoggerConfig. This helper is intentionally narrow: it only mutates configs whose sink kind is File and leaves every non-file config unchanged.
Interface
pub fn with_file_rotation(
config : LoggerConfig,
max_bytes : Int,
max_backups~ : Int = 1,
) -> LoggerConfig {
input
config : LoggerConfig- Base logger config to inspect and possibly update.max_bytes : Int- Maximum active file size before rotation.max_backups : Int- Number of rotated backup files to retain.
output
LoggerConfig- Updated file config with rotation, or the original config unchanged when the sink is not file-based.
Explanation
Detailed rules explaining key parameters and behaviors
with_file_rotation(...)only applies to file presets or other configs whosesink.kind=SinkKind::File.- If the input config is not file-based, the helper returns the config unchanged.
- When the input config is file-based, the helper preserves
min_level,target,timestamp, file path, append mode, auto-flush, formatter, and queue settings while replacingsink.rotation. - Rotation policy creation follows
file_rotation(...)normalization rules formax_bytesandmax_backups.
How to Use
Here are some specific examples provided.
When Need To Extend A File Preset
When file rotation should be added after the base preset is assembled:
let config = with_file_rotation(file("service.log"), 1024 * 1024, max_backups=3)
In this example, the file preset gains a size-based rotation policy.
And all other file sink settings remain unchanged.
When Compose Queue And Rotation Together
When the file logger also needs bounded queueing:
let config = with_file_rotation(
with_queue(file("service.log"), max_pending=32),
4096,
max_backups=2,
)
In this example, queue settings are preserved while file rotation is added.
Error Case
e.g.:
-
If the input config is not file-based, no error is raised and the config is returned unchanged.
-
If
max_bytesormax_backupsare non-positive, normalization behavior followsfile_rotation(...).
Notes
-
This helper is a no-op for
console(...),json_console(...), andtext_console(...)presets. -
Use
file(...)when you need to provide the initial file path, becausewith_file_rotation(...)does not create a file sink from a non-file config.