Browse Source

v1.2| 修改默认日志配置文件

master
zhaohe 2 months ago
parent
commit
40d747c22e
  1. 2
      README.md
  2. 2
      src/configs/version.hpp
  3. 13
      src/main.cpp
  4. 75
      thirdlib/spdlogfactory/logger_factory.cpp
  5. 2
      thirdlib/spdlogfactory/logger_factory.hpp

2
README.md

@ -21,6 +21,8 @@
```
v1| 初始化
V1.1| 修改默认日志配置文件
```
## 脚本说明

2
src/configs/version.hpp

@ -1,2 +1,2 @@
#pragma once
#define VERSION "1.0"
#define VERSION "1.2"

13
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();

75
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<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;
}

2
thirdlib/spdlogfactory/logger_factory.hpp

@ -53,6 +53,8 @@ class SpdLoggerFactory {
static SpdLoggerFactory factory;
return factory;
}
void initialize();
shared_ptr<logger> createLogger(string loggerName);
set<string> loggerNames();

Loading…
Cancel
Save