diff --git a/README.md b/README.md index a194414..806877f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ ``` v1| 初始化 +V1.1| 修改默认日志配置文件 + ``` ## 脚本说明 diff --git a/src/configs/version.hpp b/src/configs/version.hpp index 8820105..ad8ec44 100644 --- a/src/configs/version.hpp +++ b/src/configs/version.hpp @@ -1,2 +1,2 @@ #pragma once -#define VERSION "1.0" \ No newline at end of file +#define VERSION "1.2" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a6578f4..5bbaf00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,13 +18,14 @@ using namespace std; * MAIN => MAIN * *******************************************************************************/ int main(int argc, char* argv[]) { - spdlog::info("Iflytop Hardware Service Start...."); spdlog::flush_on(spdlog::level::debug); - spdlog::info("#"); - spdlog::info("# company:{}", "ifytop"); - spdlog::info("# version:{}", VERSION); - spdlog::info("#"); - + LoggerFactory.initialize(); + auto mainLogger = LoggerFactory.createLogger("main"); + mainLogger->info("Iflytop Hardware Service Start...."); + mainLogger->info("#"); + mainLogger->info("# company:{}", "ifytop"); + mainLogger->info("# version:{}", VERSION); + mainLogger->info("#"); App app; app.initialize(); diff --git a/thirdlib/spdlogfactory/logger_factory.cpp b/thirdlib/spdlogfactory/logger_factory.cpp index 045aba1..22bff14 100644 --- a/thirdlib/spdlogfactory/logger_factory.cpp +++ b/thirdlib/spdlogfactory/logger_factory.cpp @@ -27,7 +27,7 @@ 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* kDefaultPattern = "[%C-%m-%d %H:%M:%S.%e] [%-30n] [%^%L%$] %v"; // const static string kDefaultPattern = ""; // const string WEAK spdLoggerConfig() { return ""; } @@ -139,22 +139,24 @@ static bool c_daily_file_sink_mt(json j) { } }; #endif - +// 10485760 == 10M static string default_config = R"( [ { "name": "info-sink", - "type": "daily_file_sink_mt", + "type": "rotating_file_sink_mt", "filename": "logs/infolog.log", - "max_files": 30, + "max_file_size":10485760, + "max_files": 3, "rotate_on_open": true, "level" : 2 }, { "name": "debug-sink", - "type": "daily_file_sink_mt", + "type": "rotating_file_sink_mt", "filename": "logs/debuglog.log", - "max_files": 10, + "max_file_size":10485760, + "max_files": 3, "rotate_on_open": true, "level": 0 }, @@ -351,9 +353,9 @@ LOGGER_ENABLE_END(daily_logger_mt) LOGGER_ENABLE_BEGIN(rotating_logger_mt) { GET(string, filename); mkdirIfNotExist(filename); - TRY_GET(int, max_file_size, 1000); - TRY_GET(int, max_files, 100); - TRY_GET(bool, rotate_on_open, false); + TRY_GET(int, max_file_size, 10 * 1024 * 1024); + TRY_GET(int, max_files, 3); + TRY_GET(bool, rotate_on_open, true); var_logger = spdlog::rotating_logger_mt(name, filename, max_file_size, max_files, rotate_on_open); } LOGGER_ENABLE_END(rotating_logger_mt) @@ -408,9 +410,9 @@ SINK_DEFINE_END(daily_file_sink_mt) SINK_DEFINE_BEGIN(rotating_file_sink_mt) { GET(string, filename); mkdirIfNotExist(filename); - TRY_GET(int, max_file_size, 1000); - TRY_GET(int, max_files, 100); - TRY_GET(bool, rotate_on_open, false); + TRY_GET(int, max_file_size, 10 * 1024 * 1024); + TRY_GET(int, max_files, 5); + TRY_GET(bool, rotate_on_open, true); sink = make_shared(filename, max_file_size, max_files, rotate_on_open); } SINK_DEFINE_END(rotating_file_sink_mt) @@ -452,7 +454,7 @@ static logger_t createRootLogger() { * 则不会自动注册logger * @param var_logger */ -static void priRegLogger(logger_t var_logger) { +static void myRegLogger(logger_t var_logger) { if (var_logger->name() == kRootLogerName) { spdlog::set_default_logger(var_logger); } @@ -460,6 +462,10 @@ static void priRegLogger(logger_t var_logger) { if (!get(var_logger->name())) { register_logger(var_logger); } + if (!get(var_logger->name())) { + spdlog::critical("reg root logger fail {}"); + exit(-1); + } } static void __parseSphLogConfig(json var) { @@ -543,7 +549,7 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) { } for (auto& sink : sinks) logger->sinks().push_back(sink); } - for (auto& var : s_loggers) priRegLogger(var.second); + for (auto& var : s_loggers) myRegLogger(var.second); // 如果没有rootLogger,构造rootLogger if (!get(kRootLogerName)) { @@ -557,7 +563,7 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) { auto rootLogger = createRootLogger(); rootLogger->set_level(to_level(level)); if (!pattern.empty()) rootLogger->set_pattern(pattern); - priRegLogger(rootLogger); + myRegLogger(rootLogger); } else { spdlog::critical("shouldn't go here"); exit(-1); @@ -567,7 +573,7 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) { } } // 如果依然没有构造rootLogger则构造默认logger - if (!get(kRootLogerName)) priRegLogger(createRootLogger()); + if (!get(kRootLogerName)) myRegLogger(createRootLogger()); // 构造没有type的logger for (auto& j : configjson) { @@ -575,7 +581,7 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) { GET(string, name); if (type.empty() && name != kRootLogerName) { auto newlogger = createLoggerWithoutType(j); - priRegLogger(newlogger); + myRegLogger(newlogger); } } @@ -608,16 +614,24 @@ class MonitoringSpdLoggerConfigTask { } ~MonitoringSpdLoggerConfigTask() { wthread->join(); } }; +void SpdLoggerFactory::initialize() { + if (!initializeLogger) { + string configFilePath = getConfigFilePath(); + if (!configFilePath.empty() && exist(configFilePath)) { + parseSphLogConfig(configFilePath); + } else { + 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; + } +} shared_ptr SpdLoggerFactory::createLogger(string loggerName) { - /** - * @brief - * 如果在main函数之前就创建了logger,会导致部分全局变量未初始化 - */ - if (default_config.empty()) { - spdlog::critical("you may construct a logger {} before main!!", loggerName); - exit(-1); - } lock_guard lock_gu(createLogger_lock); if (!loggerName.empty()) { if (s_loggerNames.size() == 0) { @@ -661,15 +675,14 @@ shared_ptr SpdLoggerFactory::createLogger(string loggerName) { exit(-1); } logger_t newLogger = rootLogger->clone(loggerName); - priRegLogger(newLogger); + myRegLogger(newLogger); return newLogger; } return nullptr; } set SpdLoggerFactory::loggerNames() { return s_loggerNames; } - -sink_ptr SpdLoggerFactory::getSink(string name) { +sink_ptr SpdLoggerFactory::getSink(string name) { auto result = s_sinks.find(name); if (result == s_sinks.end()) { return nullptr; @@ -681,9 +694,9 @@ shared_ptr SpdLoggerFactory::createRotatingFileLogger(const std::string& bool bindDebug, bool bindInfo) { auto newlogger = spdlog::rotating_logger_mt(logger_name, fmt::format("logs/{}.log", logger_name), 5 * 1024 * 1024 /*5M*/, 3 /*times*/); newlogger->set_level(spdlog::level::info); - if (bindTerminal) newlogger->sinks().push_back(GET_SINK("terminal-sink")); - if (bindDebug) newlogger->sinks().push_back(GET_SINK("debug-sink")); - if (bindInfo) newlogger->sinks().push_back(GET_SINK("info-sink")); - priRegLogger(newlogger); + if (bindTerminal && GET_SINK("terminal-sink")) newlogger->sinks().push_back(GET_SINK("terminal-sink")); + if (bindDebug && GET_SINK("debug-sink")) newlogger->sinks().push_back(GET_SINK("debug-sink")); + if (bindInfo && GET_SINK("info-sink")) newlogger->sinks().push_back(GET_SINK("info-sink")); + myRegLogger(newlogger); return newlogger; -} \ No newline at end of file +} diff --git a/thirdlib/spdlogfactory/logger_factory.hpp b/thirdlib/spdlogfactory/logger_factory.hpp index 4618fb6..d6aacf1 100644 --- a/thirdlib/spdlogfactory/logger_factory.hpp +++ b/thirdlib/spdlogfactory/logger_factory.hpp @@ -53,6 +53,8 @@ class SpdLoggerFactory { static SpdLoggerFactory factory; return factory; } + void initialize(); + shared_ptr createLogger(string loggerName); set loggerNames();