|
|
@ -3,20 +3,21 @@ |
|
|
|
//
|
|
|
|
|
|
|
|
#include "logger_factory.hpp"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include "iflytopcpp/core/basic/nlohmann/json.hpp"
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#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; \ |
|
|
@ -182,8 +216,7 @@ static bool c_daily_file_sink_mt(json j) { |
|
|
|
} \ |
|
|
|
} \ |
|
|
|
catch (const std::exception& e) { \ |
|
|
|
spdlog::critical("c_" #var_name " fail {} reason {}", j.dump(1), \ |
|
|
|
e.what()); \ |
|
|
|
spdlog::critical("c_" #var_name " fail {} reason {}", j.dump(1), e.what()); \ |
|
|
|
exit(-1); \ |
|
|
|
} \ |
|
|
|
} \ |
|
|
@ -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<type>(j, #value_name, default_value) |
|
|
|
#define TRY_GET(type, value_name, default_value) type value_name = tryGet<type>(j, #value_name, default_value)
|
|
|
|
#define GET(T, value_name) T value_name = j.at(#value_name).get<T>();
|
|
|
|
|
|
|
|
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<string>, sinks); |
|
|
|
auto var_logger = make_shared<logger>(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<sinks::daily_file_sink_mt>(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<sinks::daily_file_sink_mt>(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<sinks::rotating_file_sink_mt>(filename, max_file_size, |
|
|
|
max_files, rotate_on_open); |
|
|
|
sink = make_shared<sinks::rotating_file_sink_mt>(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<sinks::stdout_color_sink_mt>(); |
|
|
|
} |
|
|
|
SINK_DEFINE_BEGIN(stdout_color_sink_mt) { sink = make_shared<sinks::stdout_color_sink_mt>(); } |
|
|
|
SINK_DEFINE_END(stdout_color_sink_mt) |
|
|
|
|
|
|
|
SINK_DEFINE_BEGIN(stderr_color_sink_mt) { |
|
|
|
sink = make_shared<sinks::stderr_color_sink_mt>(); |
|
|
|
} |
|
|
|
SINK_DEFINE_BEGIN(stderr_color_sink_mt) { sink = make_shared<sinks::stderr_color_sink_mt>(); } |
|
|
|
SINK_DEFINE_END(stderr_color_sink_mt) |
|
|
|
|
|
|
|
/**
|
|
|
@ -570,15 +596,10 @@ static string getConfigFilePath() { |
|
|
|
if (exist(spdLoggerConfig())) { |
|
|
|
return spdLoggerConfig(); |
|
|
|
} else { |
|
|
|
spdlog::warn("can't find spdLoggerConfig file {}", spdLoggerConfig()); |
|
|
|
} |
|
|
|
spdlog::warn("can't find spdLoggerConfig file {},use deafult config", spdLoggerConfig()); |
|
|
|
} |
|
|
|
for (auto var : kSpdDefaultConfigPaths) { |
|
|
|
if (exist(var)) { |
|
|
|
return var; |
|
|
|
} |
|
|
|
} |
|
|
|
return ""; |
|
|
|
return "spd_logger_cfg.json"; |
|
|
|
} |
|
|
|
|
|
|
|
class MonitoringSpdLoggerConfigTask { |
|
|
@ -617,8 +638,12 @@ shared_ptr<logger> 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; |
|
|
|
} |
|
|
|