|
|
@ -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<sinks::rotating_file_sink_mt>(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<logger> 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<mutex> lock_gu(createLogger_lock); |
|
|
|
if (!loggerName.empty()) { |
|
|
|
if (s_loggerNames.size() == 0) { |
|
|
@ -661,14 +675,13 @@ shared_ptr<logger> SpdLoggerFactory::createLogger(string loggerName) { |
|
|
|
exit(-1); |
|
|
|
} |
|
|
|
logger_t newLogger = rootLogger->clone(loggerName); |
|
|
|
priRegLogger(newLogger); |
|
|
|
myRegLogger(newLogger); |
|
|
|
return newLogger; |
|
|
|
} |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
set<string> SpdLoggerFactory::loggerNames() { return s_loggerNames; } |
|
|
|
|
|
|
|
sink_ptr SpdLoggerFactory::getSink(string name) { |
|
|
|
auto result = s_sinks.find(name); |
|
|
|
if (result == s_sinks.end()) { |
|
|
@ -681,9 +694,9 @@ shared_ptr<logger> 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; |
|
|
|
} |