Implement log file names rendering

This commit is contained in:
Victor Krasnov 2023-07-06 14:14:48 +08:00
parent 376c903da9
commit d86ceef663

View File

@ -34,6 +34,16 @@ static std::string createDirectory(const std::string & file)
return path; return path;
} }
static std::string renderFileNameTemplate(time_t now, const std::string & file_path)
{
fs::path path{file_path};
std::tm buf;
localtime_r(&now, &buf);
std::stringstream ss;
ss << std::put_time(&buf, file_path.c_str());
return path.replace_filename(ss.str());
}
#ifndef WITHOUT_TEXT_LOG #ifndef WITHOUT_TEXT_LOG
void Loggers::setTextLog(std::shared_ptr<DB::TextLog> log, int max_priority) void Loggers::setTextLog(std::shared_ptr<DB::TextLog> log, int max_priority)
{ {
@ -68,9 +78,12 @@ void Loggers::buildLoggers(Poco::Util::AbstractConfiguration & config, Poco::Log
/// The maximum (the most verbose) of those will be used as default for Poco loggers /// The maximum (the most verbose) of those will be used as default for Poco loggers
int max_log_level = 0; int max_log_level = 0;
const auto log_path = config.getString("logger.log", ""); time_t now = std::time({});
if (!log_path.empty())
const auto log_path_prop = config.getString("logger.log", "");
if (!log_path_prop.empty())
{ {
const auto log_path = renderFileNameTemplate(now, log_path_prop);
createDirectory(log_path); createDirectory(log_path);
std::string ext; std::string ext;
@ -109,9 +122,10 @@ void Loggers::buildLoggers(Poco::Util::AbstractConfiguration & config, Poco::Log
split->addChannel(log, "log"); split->addChannel(log, "log");
} }
const auto errorlog_path = config.getString("logger.errorlog", ""); const auto errorlog_path_prop = config.getString("logger.errorlog", "");
if (!errorlog_path.empty()) if (!errorlog_path_prop.empty())
{ {
const auto errorlog_path = renderFileNameTemplate(now, errorlog_path_prop);
createDirectory(errorlog_path); createDirectory(errorlog_path);
// NOTE: we don't use notice & critical in the code, so in practice error log collects fatal & error & warning. // NOTE: we don't use notice & critical in the code, so in practice error log collects fatal & error & warning.