I am using spdlog and gtest in my library. I need to save some logs (not gtest logs) from test execution to files. Here is code for creating loggers:
void createLogger() {
auto debug_logger = spdlog::rotating_logger_mt("debug_logger", "logs/test_debug", 1024 * 1024 * 25, 3);
debug_logger->set_level(spdlog::level::debug);
auto logger = spdlog::rotating_logger_mt("logger", "logs/test_info", 1024 * 1024 * 5, 2);
}
It works fine in example program with my library. When I run tests debug_logger
saves everything in files but logger
saves nothing. When I remove
debug_logger->set_level(spdlog::level::debug);
debug_logger
is not working too. So I thought that it's problem with loggers but when I add
logger->set_level(spdlog::level::info);
it is still not working.
Here is main for tests:
int main(int argc, char **argv) {
try {
utility::createLogger();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} catch (const spdlog::spdlog_ex& ex) {
std::cout << "Log initialization failed: " << ex.what() << std::endl;
}
}
Setup and one of test cases:
class CPUTest: public ::testing::Test {
protected:
std::shared_ptr<spdlog::logger> logger;
void SetUp() {
spdlog::get("logger")->info("test get");
logger = spdlog::get("logger");
}
};
TEST_F(CPUTest, Resolve15) {
logger->info("Something from info logger");
// ...
}
debug_logger
with level debug
saves info
logs too but logger
with debug
not.
Do you have any clue what is wrong with tests and loggers initialization in my code? Should I use two loggers in my library or one with two files?
Due to very long execution of my tests I was killing process. debug
logs were saved always in both files, info
logs not. It was caused by flush policy in spdlog
(debug
level is more important and it was flushed very often - I suppose that almost after each call).
info
logs are flushed after all tests (not after each test case or each test class). That's why my files were always empty (I killed process).
It is possible to change flush policy for logger by
logger->flush_on(spdlog::level::info);