#2553·spdlog

Daily rotation does not remove oldest files if one day is missing

Author: daniele77Created Nov 25, 2022Updated Aug 4, 2026

If for any reason one of the old files is missing (e.g., because one day the application has not logged), the oldest file is no more removed. For example, having these files:

myapp_2022-11-25.log
myapp_2022-11-24.log
myapp_2022-11-23.log
myapp_2022-11-21.log
myapp_2022-11-20.log

myapp_2022-11-21.log and myapp_2022-11-20.log won't be never deleted.

This function causes the issue:

https://github.com/gabime/spdlog/blob/v1.x/include/spdlog/sinks/daily_file_sink.h#L183

void init_filenames_q_()
{
    using details::os::path_exists;

    filenames_q_ = details::circular_q<filename_t>(static_cast<size_t>(max_files_));
    std::vector<filename_t> filenames;
    auto now = log_clock::now();
    while (filenames.size() < max_files_)
    {
        auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
        if (!path_exists(filename))  // <---------------- stop at the first file missing
        {
            break;
        }
        filenames.emplace_back(filename);
        now -= std::chrono::hours(24);    // <-------- goes backward 24h
    }
    for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter)
    {
        filenames_q_.push_back(std::move(*iter));
    }
}

I guess one fix could be to change the function so that it enumerates the files with the right pattern and remove the oldest one based on the file timestamp.