quill: Loggers not flushing until application exit
Hello, I’m creating three loggers as so and logging a message to each:
int main()
{
quill::Logger *logger_a = createLogger();
quill::Logger *logger_b = createLogger();
quill::Logger *logger_c = createLogger();
LOG_INFO(logger_a, "hello! - logger_a");
LOG_INFO(logger_b, "hello! - logger_b");
LOG_INFO(logger_c, "hello! - logger_c");
logger_a->flush_log();
logger_b->flush_log();
logger_c->flush_log();
std::cout << "sleeping..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(20000));
}
The issue is that logger_a and logger_b will not log to file until the point that the application exits after 20 seconds. Logger C will log to file immediately. Checking the log files while the application is sleeping for 20 seconds, the log file created by loggers A and B will be empty, while C will contain “hello! - logger_c”. Checking the log files again after the application exits, the log files created by logger a and b will become populated with their own log messages. Reversing the order of the LOG_INFO calls has no effect - logger C will still be the only logger that records to file immediately, probably as it is the last logger to be created.
Here is my createLogger function, I have tried to cut it down as best I can:
quill::Logger *createLogger() {
if (!quill::Backend::is_running()) {
quill::BackendOptions backend_options;
backend_options.sleep_duration = std::chrono::nanoseconds(100);
#ifdef _DEBUG
backend_options.error_notifier = [](std::string const &error_message) {
std::cout << "QUILL ERROR: " << error_message << std::endl;
};
#endif
quill::Backend::start(backend_options);
}
// work out the log index number to add to the filename
std::string log_index;
int highest_index = 0;
std::filesystem::path log_path = "D:\\samsteadman\\quill_log_test\\";
if (std::filesystem::exists(log_path)) {
int file_int = 0;
// iterate through files in folder, find the highest log index number in filenames.
for (const auto &file : std::filesystem::directory_iterator(log_path)) {
if (file.is_regular_file()) {
std::string file_str = file.path().filename().generic_string();
size_t underscore_1_index = file_str.find('_');
size_t underscore_2_index = file_str.find('_', underscore_1_index + 1);
if (underscore_1_index != std::string::npos && underscore_2_index != std::string::npos) {
std::string number_str = file_str.substr(underscore_1_index + 1, underscore_2_index - underscore_1_index - 1);
try {
file_int = std::stoi(number_str);
} catch (const std::exception &e) {
file_int = 0;
}
if (file_int > highest_index) {
highest_index = file_int;
}
}
}
}
log_index = (boost::format("%03d") % (file_int + 1)).str();
} else {
log_index = "001";
}
auto file_sink = quill::Frontend::create_or_get_sink<quill::RotatingFileSink>(
(log_path / ("PS_" + log_index + ".log")).generic_string(),
[]() {
quill::RotatingFileSinkConfig cfg;
cfg.set_open_mode('w');
cfg.set_filename_append_option(quill::FilenameAppendOption::StartDateTime);
cfg.set_rotation_time_daily("00:00");
cfg.set_rotation_max_file_size(1024 * 1024 * 10); // 10MB
cfg.set_max_backup_files(100);
return cfg;
}());
quill::Logger *new_logger = quill::Frontend::create_or_get_logger(
"logger_" + log_index,
std::move(file_sink),
"[%(time)] [%(log_level)] [" + log_index + ":%(tags):%(caller_function)]: %(message)");
return new_logger;
}
This is probably an issue with my implementation of creating the loggers, but can’t work out how/why. I’m using quill 4.0.0 Thanks in advance!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (9 by maintainers)
Just to note that calling flush on one logger will flush everything until the current timestamp of all loggers, so you do not need to call it three times. Also if you do not wish to use a logger you can just use
quill::Frontend::get_valid_logger()->flush_log();That will get the first valid logger object and call flush which is equivalentWith regards to the file creation, the file should be opened during the Sink construction in
create_or_get_sink. So i would except the filesystem to see the opened file after that call