Skip to content

Add size-based rotating sink with timestamped archive names - #3691

Closed
Yaki-Blake wants to merge 1 commit into
gabime:v1.xfrom
Yaki-Blake:rotating-datetime-sink
Closed

Yaki-Blake wants to merge 1 commit into
gabime:v1.xfrom
Yaki-Blake:rotating-datetime-sink

Conversation

@Yaki-Blake

Copy link
Copy Markdown

Closes #580.

Adds rotating_datetime_file_sink: rotates on size like rotating_file_sink, but each rotated file is renamed to a name produced by a FileNameCalc template argument -- the same convention daily_file_sink/hourly_file_sink already use, so one calculator can be shared by all three.

logs/app.log  =>  logs/app-2026-10-23_10-56-25.log
logs/app.log                                          (reopened, keeps growing)
#include "spdlog/sinks/rotating_datetime_file_sink.h"

auto logger = spdlog::rotating_datetime_logger_mt(
    "logger", "logs/app.log", 5 * 1024 * 1024, /*max_files=*/10);

The active file keeps a fixed name on purpose: file_helper opens it in append mode, so a restarted process continues the same file and max_size keeps being enforced. A timestamp in the active name would start a new file on every run (measured: two runs give 2 files instead of 1), silently defeating max_size/max_files across restarts. That is what rotate_on_open is for.

API mirrors rotating_file_sink (set_max_size, set_max_files, set_buffer_size, rotate_now, rotate_on_open). Additive only -- no existing header or source is modified. 10 Catch2 cases added; full suite passes with MSVC 19.51 and MinGW g++ 8.1, header-only and compiled lib, no warnings.

Issue gabime#580 asks for size-based rotation with the rotated files named after a
timestamp instead of a .1/.2 index (logrotate's dateext). rotating_file_sink
hardcodes the suffixes; daily_file_sink/hourly_file_sink take a FileNameCalc but
only rotate on time.

The active file keeps a stable name because file_helper opens it in append mode
-- a timestamped active name would start a new file on every process start and
stop enforcing max_size across restarts.

Additive only: no existing header or source is modified.

Closes gabime#580
@gabime

gabime commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Thanks. Not sure I want to add whole new sink for this rarely required feature. Closing for now

@gabime gabime closed this Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom filename creation for all sinks

2 participants