spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

Not able to save logs with rotating_file_sink_mt

Open sairaj2311 opened this issue 2 years ago • 2 comments

I am facing an issue with the rotating file logger in my project when run on Atom Board. Specifically, out of the 7 modules in my project, only 5 of them are working as expected, while the remaining 2 modules generate empty log files.

Code Overview: I have a SetupLogger function that sets up the logger using the spdlog library. Here is a simplified version of the function:

void SetupLogger(const std::string& LoggerName, std::string aLogDirPath, int FileSize, int FileCount) {
// ... (aLogFilepath setup) try {
std::vectorspdlog::sink_ptr sinks; sinks.push_back(std::make_sharedspdlog::sinks::stdout_sink_mt()); sinks.push_back(std::make_sharedspdlog::sinks::rotating_file_sink_mt(LogFilePath, FileSize * 1024 * 1024, FileCount));

    auto combined_logger = std::make_shared<spdlog::logger>(LoggerName, begin(sinks), end(sinks));
    combined_logger->set_level(spdlog::level::trace);
    spdlog::register_logger(combined_logger);

    std::string jsonpattern = {"{\"time\": \"%Y-%m-%dT%H:%M:%S.%f%z\", \"module\": \"%n\", \"level\": \"%^%l%$\", \"message\": \"%v\"}"};
    spdlog::set_pattern(jsonpattern);
}
catch(const std::exception& e)
{
    std::cout << "Exception while setup logger, Exception : " <<  e.what() << std::endl;
}

}

And I am calling this function in each of the 7 main functions:

std::shared_ptrspdlog::logger pLogger; int main() { SetupLogger("Module1", logFilePath, 10, 5); pLogger = spdlog::get("Module1"); pLogger->info("Module1 log started..."); // ... (rest of the module logic) }

Issue Details: When using spdlog::flush_every(std::chrono::milliseconds(100)); in the SetupLogger function, the problem is resolved for 6th module but still not able to generate log in 7th module. When using only spdlog::flush_on(spdlog::level::trace), the issue is resolved for the remaining modules. Additional Context: When running the same project on another laptop (without using flush_every or flush_on), all modules work perfectly fine. Laptop Specs:- Processor- i5-1135G7 @ 2.40GHz * 8 RAM - 16gb I am using 64 bit ubuntu 22.04 lts on both systems.

sairaj2311 avatar Jan 14 '24 18:01 sairaj2311

All log to the same file?

gabime avatar Jan 14 '24 18:01 gabime

No, each module logs in a different file.

sairaj2311 avatar Jan 14 '24 19:01 sairaj2311