2

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");
Ofiris
  • 6,047
  • 6
  • 35
  • 58
  • Look at sources. https://github.com/gabime/spdlog/blob/master/include/spdlog/sinks/file_sinks.h there is no simple way, as I can see, but you can write your own think by the way. – ForEveR Feb 02 '15 at 14:38

1 Answers1

5

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

GabiMe
  • 18,105
  • 28
  • 76
  • 113