|
@@ -1,10 +1,14 @@
|
|
|
#include "logger.h"
|
|
#include "logger.h"
|
|
|
#include <spdlog/async.h>
|
|
#include <spdlog/async.h>
|
|
|
#include <spdlog/sinks/rotating_file_sink.h>
|
|
#include <spdlog/sinks/rotating_file_sink.h>
|
|
|
-#include "spdlog/sinks/stdout_color_sinks.h"
|
|
|
|
|
|
|
+#include <spdlog/sinks/stdout_color_sinks.h>
|
|
|
|
|
+#include <spdlog/sinks/basic_file_sink.h>
|
|
|
|
|
|
|
|
#define MAX_ROTATING_FILE_SIZE 1048576 * 5
|
|
#define MAX_ROTATING_FILE_SIZE 1048576 * 5
|
|
|
#define MAX_ROTATING_FILES 3
|
|
#define MAX_ROTATING_FILES 3
|
|
|
|
|
+
|
|
|
|
|
+#define CRITICAL_MAX_ROTATING_FILE_SIZE 100 * 1024 * 1024
|
|
|
|
|
+#define CRITICAL_MAX_ROTATING_FILES 5
|
|
|
|
|
|
|
|
Logger::Logger()
|
|
Logger::Logger()
|
|
|
{
|
|
{
|
|
@@ -49,4 +53,25 @@ void Logger::setup(LOG_LEVEL level, const char *path)
|
|
|
logger->set_level(static_cast<spdlog::level::level_enum>(level)); // 允许所有日志通过 logger 层
|
|
logger->set_level(static_cast<spdlog::level::level_enum>(level)); // 允许所有日志通过 logger 层
|
|
|
spdlog::register_logger(logger);
|
|
spdlog::register_logger(logger);
|
|
|
spdlog::set_default_logger(logger);
|
|
spdlog::set_default_logger(logger);
|
|
|
|
|
+
|
|
|
|
|
+ // ---------------- 重要信息日志 ----------------
|
|
|
|
|
+ // 使用 basic_file_sink_mt 保证同步写入
|
|
|
|
|
+ std::string critical_log_path(path);
|
|
|
|
|
+ auto pos = critical_log_path.rfind('.');
|
|
|
|
|
+ if (pos != std::string::npos) {
|
|
|
|
|
+ critical_log_path = critical_log_path.substr(0, pos) + ".critical.log";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ critical_log_path += ".critical.log"; // 没有扩展名,直接追加
|
|
|
|
|
+ }
|
|
|
|
|
+ // auto critical_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(critical_log_path, true); // true 表示 append
|
|
|
|
|
+ auto critical_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
|
|
|
|
|
+ critical_log_path, CRITICAL_MAX_ROTATING_FILE_SIZE, CRITICAL_MAX_ROTATING_FILES
|
|
|
|
|
+ );
|
|
|
|
|
+ critical_sink->set_level(spdlog::level::info); // 只记录 info 及以上
|
|
|
|
|
+ auto critical_logger = std::make_shared<spdlog::logger>("critical", critical_sink);
|
|
|
|
|
+ critical_logger->flush_on(spdlog::level::info); // 每条日志立即刷盘
|
|
|
|
|
+ critical_logger->set_level(spdlog::level::info);
|
|
|
|
|
+ critical_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
|
|
|
|
|
+
|
|
|
|
|
+ spdlog::register_logger(critical_logger);
|
|
|
}
|
|
}
|