gabime / spdlog

Fast C++ logging library.
Other
24.37k stars 4.55k forks source link

spdlog can't flush to file immediately on windows11 #3117

Open aiyolo opened 4 months ago

aiyolo commented 4 months ago

I create two sinks, one is console sink and the other is file sink, the console sink can flush immediately, but the file sink always delay for multiple seconds, event though I had set flush strategy.

here is my code:

    // 输出到终端
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::trace);
    console_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%-5l%$] [%t] %v");

    // 输出到文件
    auto file_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>(
        "logs/app.log", 0, 0);
    file_sink->set_level(spdlog::level::trace);
    file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%-5l%$] [%t] %v");

    // 设置默认logger
    std::vector<spdlog::sink_ptr> sinks{console_sink, file_sink};
    auto                          logger = std::make_shared<spdlog::logger>(
        "multi_sink", sinks.begin(), sinks.end());
    logger->set_level(spdlog::level::debug);
    spdlog::set_default_logger(logger);
    spdlog::flush_on(spdlog::level::debug);
tt4g commented 4 months ago

but the file sink always delay for multiple seconds, event though I had set flush strategy.

I don't know what is going on because I don't know how to check the writes to the file. Can you give me a specific way to check, for example, "When I monitor the log file with tail -f logs/app.log, the log display is a few seconds slower in the file than in the console"?

tt4g commented 4 months ago

Regardless of the confirmation method, the following candidates are expected causes