Search code examples
c++loggingspdlog

How to change the default rolling time in spdlog daily logger?


Using spdlog, how can I change the default rolling time of the daily logger?

In the following example, the rolling happens only at midnight:

auto logger = spd::daily_logger_st("my_logger", "fl_log.txt");

Solution

  • Currently there is no direct way to change the rotation time in the daily logger - please open an issue here..

    A quick workaround would be to modify the _calc_midnight_tp() function to return the desired rotation time instead of midnight in the daily_file_sink class..

    For example to rotate in 1am instead of midnight:

    static std::chrono::system_clock::time_point _calc_midnight_tp()
    {
        using namespace std::chrono;
        auto now = system_clock::now();
        time_t tnow = std::chrono::system_clock::to_time_t(now);
        tm date = spdlog::details::os::localtime(tnow);
        date.tm_min = date.tm_sec = 0;
        date.tm_hour = 1;
        auto rotate_time = std::chrono::system_clock::from_time_t(std::mktime(&date));
        return system_clock::time_point(rotate_time + hours(24));
    }
    

    Edit: I committed a fix to this issue. Now user can set HH:MM of the desired rolling time