From d469b9312c67c64c0f95821796ad71cc06c41994 Mon Sep 17 00:00:00 2001 From: Zhaohe Date: Fri, 9 Dec 2022 01:52:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=BC=BAdaily=5Ffile=5Fsink=5Fmt?= =?UTF-8?q?=EF=BC=8C=E8=87=AA=E5=8A=A8=E5=88=A0=E9=99=A4=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/spdlog/include/spdlog/sinks/daily_file_sink.h | 28 +++++ core/spdlogfactory/logger_factory.cpp | 139 ++++++++++++--------- 2 files changed, 110 insertions(+), 57 deletions(-) diff --git a/core/spdlog/include/spdlog/sinks/daily_file_sink.h b/core/spdlog/include/spdlog/sinks/daily_file_sink.h index f6f1bb1..0a14c34 100644 --- a/core/spdlog/include/spdlog/sinks/daily_file_sink.h +++ b/core/spdlog/include/spdlog/sinks/daily_file_sink.h @@ -141,6 +141,7 @@ public: if (max_files_ > 0) { + try_del_(); init_filenames_q_(); } } @@ -179,6 +180,33 @@ protected: } private: + void try_del_() + { + using details::os::filename_to_str; + using details::os::remove_if_exists; + using details::os::path_exists; + + std::vector filenames; + int count = 0; + auto now = log_clock::now(); + while (true) + { + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + if (!path_exists(filename)) + { + break; + } + // filenames.emplace_back(filename); + count++; + if (count > max_files_) + { + std::remove(filename.c_str()); + remove_if_exists(filename); + } + now -= std::chrono::hours(24); + } + } + void init_filenames_q_() { using details::os::path_exists; diff --git a/core/spdlogfactory/logger_factory.cpp b/core/spdlogfactory/logger_factory.cpp index ec06497..48ba97c 100644 --- a/core/spdlogfactory/logger_factory.cpp +++ b/core/spdlogfactory/logger_factory.cpp @@ -3,20 +3,21 @@ // #include "logger_factory.hpp" + #include #include + #include #include #include -#include "iflytopcpp/core/basic/nlohmann/json.hpp" - -#include #include + #include "default_logger_config.hpp" +#include "iflytopcpp/core/basic/nlohmann/json.hpp" +#include "iflytopcpp/core/spdlog/include/spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/stdout_sinks.h" -#include "iflytopcpp/core/spdlog/include/spdlog/sinks/rotating_file_sink.h" // #include "zwtimecpp/core/utils/compliler.h" using namespace std; using namespace iflytopcpp; @@ -25,9 +26,8 @@ using namespace nlohmann; using namespace spdlog; const static char* kRootLogerName = "root"; -const static char* kSpdDefaultConfigPaths[] = {"spd_logger_cfg.json"}; -const static char* kDefaultPattern = - "[%C-%m-%d %H:%M:%S.%e] [%-20n] [%^%L%$] %v"; +// const static char* kSpdDefaultConfigPaths[] = {"spd_logger_cfg.json"}; +const static char* kDefaultPattern = "[%C-%m-%d %H:%M:%S.%e] [%-20n] [%^%L%$] %v"; // const static string kDefaultPattern = ""; // const string WEAK spdLoggerConfig() { return ""; } @@ -140,6 +140,40 @@ static bool c_daily_file_sink_mt(json j) { }; #endif +static string default_config = R"( +[ + { + "name": "infologger", + "type": "daily_file_sink_mt", + "filename": "logs/infolog.log", + "max_files": 30, + "rotate_on_open": true, + "level" : 2 + }, + { + "name": "debuglogger", + "type": "daily_file_sink_mt", + "filename": "logs/debuglog.log", + "max_files": 10, + "rotate_on_open": true, + "level": 0 + }, + { + "name": "terminal", + "type": "stdout_color_sink_mt" + }, + { + "name": "root", + "type": "logger", + "level": 2, + "sinks": [ + "terminal", + "debuglogger", + "infologger" + ] + } +] +)"; #define LOGGER_ENABLE_BEGIN(_name) \ static bool c_##_name(json j) { \ logger_t var_logger; \ @@ -172,21 +206,20 @@ static bool c_daily_file_sink_mt(json j) { if (type == #var_name) { \ GET(string, name); -#define SINK_DEFINE_END(var_name) \ - sink_common_config(sink, j); \ - insertSink(name, sink); \ - return true; \ - } \ - else { \ - return false; \ - } \ - } \ - catch (const std::exception& e) { \ - spdlog::critical("c_" #var_name " fail {} reason {}", j.dump(1), \ - e.what()); \ - exit(-1); \ - } \ - } \ +#define SINK_DEFINE_END(var_name) \ + sink_common_config(sink, j); \ + insertSink(name, sink); \ + return true; \ + } \ + else { \ + return false; \ + } \ + } \ + catch (const std::exception& e) { \ + spdlog::critical("c_" #var_name " fail {} reason {}", j.dump(1), e.what()); \ + exit(-1); \ + } \ + } \ ; template @@ -218,8 +251,7 @@ static bool mkdirIfNotExist(const string& path) { if (exist(dirPath)) { return true; } - int ret = - mkdir(dirPath.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + int ret = mkdir(dirPath.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); return 0 == ret ? true : false; } @@ -268,8 +300,8 @@ static level::level_enum to_level(int value) { case SPDLOG_LEVEL_CRITICAL: return level::critical; default: - spdlog::critical("level is out of range {} level must in [{},{}]", value, - SPDLOG_LEVEL_TRACE, SPDLOG_LEVEL_CRITICAL); + spdlog::critical("level is out of range {} level must in [{},{}]", value, SPDLOG_LEVEL_TRACE, + SPDLOG_LEVEL_CRITICAL); exit(-1); break; } @@ -277,8 +309,7 @@ static level::level_enum to_level(int value) { } // #define GET(type,value,error_msg,...) -#define TRY_GET(type, value_name, default_value) \ - type value_name = tryGet(j, #value_name, default_value) +#define TRY_GET(type, value_name, default_value) type value_name = tryGet(j, #value_name, default_value) #define GET(T, value_name) T value_name = j.at(#value_name).get(); static void logger_common_config(logger_t var_logger, json j) { @@ -313,7 +344,8 @@ LOGGER_ENABLE_BEGIN(daily_logger_mt) { TRY_GET(int, hour, 0); TRY_GET(int, minute, 0); TRY_GET(bool, truncate, false); - var_logger = spdlog::daily_logger_mt(name, filename, hour, minute, truncate); + TRY_GET(int, max_files, 100); + var_logger = spdlog::daily_logger_mt(name, filename, hour, minute, truncate, max_files); } LOGGER_ENABLE_END(daily_logger_mt) @@ -323,8 +355,7 @@ LOGGER_ENABLE_BEGIN(rotating_logger_mt) { TRY_GET(int, max_file_size, 1000); TRY_GET(int, max_files, 100); TRY_GET(bool, rotate_on_open, false); - var_logger = spdlog::rotating_logger_mt(name, filename, max_file_size, - max_files, rotate_on_open); + var_logger = spdlog::rotating_logger_mt(name, filename, max_file_size, max_files, rotate_on_open); } LOGGER_ENABLE_END(rotating_logger_mt) @@ -343,8 +374,7 @@ static bool c_logger(json j) { GET(set, sinks); auto var_logger = make_shared(name, sinks_init_list{}); if (sinks.empty()) { - spdlog::critical("c_logger fail {} reason {}", j.dump(1), - "Not set sink"); + spdlog::critical("c_logger fail {} reason {}", j.dump(1), "Not set sink"); exit(-1); } @@ -370,8 +400,9 @@ SINK_DEFINE_BEGIN(daily_file_sink_mt) { TRY_GET(int, hour, 0); TRY_GET(int, minute, 0); TRY_GET(bool, truncate, false); - sink = - make_shared(filename, hour, minute, truncate); + TRY_GET(int, max_files, 100); + printf("filename:%s,max_files %d\n", filename.c_str(), max_files); + sink = make_shared(filename, hour, minute, truncate, max_files); } SINK_DEFINE_END(daily_file_sink_mt) @@ -381,19 +412,14 @@ SINK_DEFINE_BEGIN(rotating_file_sink_mt) { TRY_GET(int, max_file_size, 1000); TRY_GET(int, max_files, 100); TRY_GET(bool, rotate_on_open, false); - sink = make_shared(filename, max_file_size, - max_files, rotate_on_open); + sink = make_shared(filename, max_file_size, max_files, rotate_on_open); } SINK_DEFINE_END(rotating_file_sink_mt) -SINK_DEFINE_BEGIN(stdout_color_sink_mt) { - sink = make_shared(); -} +SINK_DEFINE_BEGIN(stdout_color_sink_mt) { sink = make_shared(); } SINK_DEFINE_END(stdout_color_sink_mt) -SINK_DEFINE_BEGIN(stderr_color_sink_mt) { - sink = make_shared(); -} +SINK_DEFINE_BEGIN(stderr_color_sink_mt) { sink = make_shared(); } SINK_DEFINE_END(stderr_color_sink_mt) /** @@ -483,7 +509,7 @@ static logger_t createLoggerWithoutType(json j) { */ void core::SpdLoggerFactory::parseSphLogConfig(string path) { try { - //这里必须清空,因为这个方法可能在main函数之前启动,所以数量可能未初始化 + // 这里必须清空,因为这个方法可能在main函数之前启动,所以数量可能未初始化 s_sinks.clear(); s_loggers.clear(); s_loggerAndSinks.clear(); @@ -504,7 +530,7 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) { } } - //组装logger and sink + // 组装logger and sink for (auto& las : s_loggerAndSinks) { logger_t logger = s_loggers[las->loggerName]; if (logger == nullptr) { @@ -524,7 +550,7 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) { } for (auto& var : s_loggers) myRegLogger(var.second); - //如果没有rootLogger,构造rootLogger + // 如果没有rootLogger,构造rootLogger if (!get(kRootLogerName)) { for (auto& j : configjson) { GET(string, name); @@ -545,10 +571,10 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) { } } } - //如果依然没有构造rootLogger则构造默认logger + // 如果依然没有构造rootLogger则构造默认logger if (!get(kRootLogerName)) myRegLogger(createRootLogger()); - //构造没有type的logger + // 构造没有type的logger for (auto& j : configjson) { TRY_GET(string, type, ""); GET(string, name); @@ -570,15 +596,10 @@ static string getConfigFilePath() { if (exist(spdLoggerConfig())) { return spdLoggerConfig(); } else { - spdlog::warn("can't find spdLoggerConfig file {}", spdLoggerConfig()); - } - } - for (auto var : kSpdDefaultConfigPaths) { - if (exist(var)) { - return var; + spdlog::warn("can't find spdLoggerConfig file {},use deafult config", spdLoggerConfig()); } } - return ""; + return "spd_logger_cfg.json"; } class MonitoringSpdLoggerConfigTask { @@ -617,8 +638,12 @@ shared_ptr SpdLoggerFactory::createLogger(string loggerName) { if (!configFilePath.empty() && exist(configFilePath)) { parseSphLogConfig(configFilePath); } else { - spdlog::warn("can't find logger config file use default config"); - myRegLogger(createRootLogger()); + spdlog::warn("can't find logger config file use default config {}", configFilePath); + // 写字符串default_config到文件中configFilePath + ofstream outfile(configFilePath); + outfile << default_config; + outfile.close(); + parseSphLogConfig(configFilePath); } initializeLogger = true; }