Skip to content

log: add file rotation support based on size and time intervals#2293

Draft
Dominik-esb wants to merge 1 commit intoprometheus-community:masterfrom
Dominik-esb:feature/add-log-rotation
Draft

log: add file rotation support based on size and time intervals#2293
Dominik-esb wants to merge 1 commit intoprometheus-community:masterfrom
Dominik-esb:feature/add-log-rotation

Conversation

@Dominik-esb
Copy link
Copy Markdown
Contributor

What this PR does / why we need it

This PR adds log rotation functionality to the windows_exporter logger to prevent log files from growing indefinitely and consuming excessive disk space.

The implementation adds a new rotate package that provides:

  • Size-based rotation: Automatically rotates logs when a file exceeds a configurable size limit
  • Time-based rotation: Automatically rotates logs at configurable time intervals (e.g., daily, hourly)
  • Archive management: Automatically deletes old log files to maintain a configurable number of archived logs
  • Thread-safe operations: Uses mutex locking to prevent corruption from concurrent writes

The FileWriter type implements the io.Writer interface, making it a drop-in replacement for standard file writers. Rotated files are renamed with timestamps (e.g., app-2026-01-08-1704672000.log) to ensure unique names and easy identification.

Which issue this PR fixes

(optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

  • fixes #

Special notes for your reviewer

  • This PR only adds the rotation infrastructure (internal/log/rotate.go). Integration into the existing logger (internal/log/logger.go) will be needed.
  • The implementation is inspired by the Telegraf log rotation.

Particularly user-facing changes

Once integrated into the logger, users will be able to configure:

  • --log.rotation-interval: Time interval for log rotation (e.g., 24h for daily rotation, 0 to disable)
  • --log.rotation-max-size: Maximum log file size in bytes before rotation (e.g., 10485760 for 10MB, 0 to disable)
  • --log.rotation-max-archives: Number of archived log files to keep (-1 to keep all)

Example configuration:

log:
  file: /var/log/windows_exporter.log
  rotation-interval: 24h
  rotation-max-size: 104857600  # 100MB
  rotation-max-archives: 7      # Keep 7 days of logs

Checklist

Complete these before marking the PR as ready to review:

  • DCO signed
  • The PR title has a summary of the changes and the area they affect
  • The PR body has a summary to reflect any significant (and particularly user-facing) changes introduced by this PR

Signed-off-by: EisenbergD <dominik.eisenberg@beiersdorf.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants