Browse Source

加强daily_file_sink_mt,自动删除过期日志

disinfection_machine
zhaohe 3 years ago
parent
commit
d469b9312c
  1. 28
      core/spdlog/include/spdlog/sinks/daily_file_sink.h
  2. 139
      core/spdlogfactory/logger_factory.cpp

28
core/spdlog/include/spdlog/sinks/daily_file_sink.h

@ -141,6 +141,7 @@ public:
if (max_files_ > 0) if (max_files_ > 0)
{ {
try_del_();
init_filenames_q_(); init_filenames_q_();
} }
} }
@ -179,6 +180,33 @@ protected:
} }
private: private:
void try_del_()
{
using details::os::filename_to_str;
using details::os::remove_if_exists;
using details::os::path_exists;
std::vector<filename_t> 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_() void init_filenames_q_()
{ {
using details::os::path_exists; using details::os::path_exists;

139
core/spdlogfactory/logger_factory.cpp

@ -3,20 +3,21 @@
// //
#include "logger_factory.hpp" #include "logger_factory.hpp"
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include "iflytopcpp/core/basic/nlohmann/json.hpp"
#include <mutex>
#include <thread> #include <thread>
#include "default_logger_config.hpp" #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/basic_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/stdout_sinks.h" #include "spdlog/sinks/stdout_sinks.h"
#include "iflytopcpp/core/spdlog/include/spdlog/sinks/rotating_file_sink.h"
// #include "zwtimecpp/core/utils/compliler.h" // #include "zwtimecpp/core/utils/compliler.h"
using namespace std; using namespace std;
using namespace iflytopcpp; using namespace iflytopcpp;
@ -25,9 +26,8 @@ using namespace nlohmann;
using namespace spdlog; using namespace spdlog;
const static char* kRootLogerName = "root"; 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 static string kDefaultPattern = "";
// const string WEAK spdLoggerConfig() { return ""; } // const string WEAK spdLoggerConfig() { return ""; }
@ -140,6 +140,40 @@ static bool c_daily_file_sink_mt(json j) {
}; };
#endif #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) \ #define LOGGER_ENABLE_BEGIN(_name) \
static bool c_##_name(json j) { \ static bool c_##_name(json j) { \
logger_t var_logger; \ logger_t var_logger; \
@ -172,21 +206,20 @@ static bool c_daily_file_sink_mt(json j) {
if (type == #var_name) { \ if (type == #var_name) { \
GET(string, 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 <class type> template <class type>
@ -218,8 +251,7 @@ static bool mkdirIfNotExist(const string& path) {
if (exist(dirPath)) { if (exist(dirPath)) {
return true; 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; return 0 == ret ? true : false;
} }
@ -268,8 +300,8 @@ static level::level_enum to_level(int value) {
case SPDLOG_LEVEL_CRITICAL: case SPDLOG_LEVEL_CRITICAL:
return level::critical; return level::critical;
default: 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); exit(-1);
break; break;
} }
@ -277,8 +309,7 @@ static level::level_enum to_level(int value) {
} }
// #define GET(type,value,error_msg,...) // #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>(); #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) { 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, hour, 0);
TRY_GET(int, minute, 0); TRY_GET(int, minute, 0);
TRY_GET(bool, truncate, false); 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) 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_file_size, 1000);
TRY_GET(int, max_files, 100); TRY_GET(int, max_files, 100);
TRY_GET(bool, rotate_on_open, false); 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) LOGGER_ENABLE_END(rotating_logger_mt)
@ -343,8 +374,7 @@ static bool c_logger(json j) {
GET(set<string>, sinks); GET(set<string>, sinks);
auto var_logger = make_shared<logger>(name, sinks_init_list{}); auto var_logger = make_shared<logger>(name, sinks_init_list{});
if (sinks.empty()) { 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); exit(-1);
} }
@ -370,8 +400,9 @@ SINK_DEFINE_BEGIN(daily_file_sink_mt) {
TRY_GET(int, hour, 0); TRY_GET(int, hour, 0);
TRY_GET(int, minute, 0); TRY_GET(int, minute, 0);
TRY_GET(bool, truncate, false); 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) 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_file_size, 1000);
TRY_GET(int, max_files, 100); TRY_GET(int, max_files, 100);
TRY_GET(bool, rotate_on_open, false); 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_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_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) SINK_DEFINE_END(stderr_color_sink_mt)
/** /**
@ -483,7 +509,7 @@ static logger_t createLoggerWithoutType(json j) {
*/ */
void core::SpdLoggerFactory::parseSphLogConfig(string path) { void core::SpdLoggerFactory::parseSphLogConfig(string path) {
try { try {
//这里必须清空,因为这个方法可能在main函数之前启动,所以数量可能未初始化
// 这里必须清空,因为这个方法可能在main函数之前启动,所以数量可能未初始化
s_sinks.clear(); s_sinks.clear();
s_loggers.clear(); s_loggers.clear();
s_loggerAndSinks.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) { for (auto& las : s_loggerAndSinks) {
logger_t logger = s_loggers[las->loggerName]; logger_t logger = s_loggers[las->loggerName];
if (logger == nullptr) { if (logger == nullptr) {
@ -524,7 +550,7 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) {
} }
for (auto& var : s_loggers) myRegLogger(var.second); for (auto& var : s_loggers) myRegLogger(var.second);
//如果没有rootLogger,构造rootLogger
// 如果没有rootLogger,构造rootLogger
if (!get(kRootLogerName)) { if (!get(kRootLogerName)) {
for (auto& j : configjson) { for (auto& j : configjson) {
GET(string, name); GET(string, name);
@ -545,10 +571,10 @@ void core::SpdLoggerFactory::parseSphLogConfig(string path) {
} }
} }
} }
//如果依然没有构造rootLogger则构造默认logger
// 如果依然没有构造rootLogger则构造默认logger
if (!get(kRootLogerName)) myRegLogger(createRootLogger()); if (!get(kRootLogerName)) myRegLogger(createRootLogger());
//构造没有type的logger
// 构造没有type的logger
for (auto& j : configjson) { for (auto& j : configjson) {
TRY_GET(string, type, ""); TRY_GET(string, type, "");
GET(string, name); GET(string, name);
@ -570,15 +596,10 @@ static string getConfigFilePath() {
if (exist(spdLoggerConfig())) { if (exist(spdLoggerConfig())) {
return spdLoggerConfig(); return spdLoggerConfig();
} else { } 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 { class MonitoringSpdLoggerConfigTask {
@ -617,8 +638,12 @@ shared_ptr<logger> SpdLoggerFactory::createLogger(string loggerName) {
if (!configFilePath.empty() && exist(configFilePath)) { if (!configFilePath.empty() && exist(configFilePath)) {
parseSphLogConfig(configFilePath); parseSphLogConfig(configFilePath);
} else { } 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; initializeLogger = true;
} }

Loading…
Cancel
Save