- 首页 > 生活百科 > >
【C++】spdlog光速入门,C++logger最简单最快的库( 三 )
一键获取logger,假如logger不存在则创建头文件
#ifndef SIDECAR_LOGGER_H#define SIDECAR_LOGGER_H#include <stdlib.h>#include "spdlog/spdlog.h"#include "spdlog/sinks/stdout_color_sinks.h"#include "spdlog/sinks/stdout_sinks.h"#include "spdlog/sinks/basic_file_sink.h"#include "spdlog/sinks/rotating_file_sink.h"#include "spdlog/sinks/daily_file_sink.h"#include <vector>class SidecarLoggers{public:static void init();static spdlog::level::level_enum getGlobalLevel();static std::vector<spdlog::sink_ptr> createSinks(const std::string &log_file_name);static void createLogger(const std::string &logger_name);static std::shared_ptr<spdlog::logger> getLogger(const std::string &logger_name);private:static spdlog::level::level_enum global_level;};#endif
源文件
#include "sidecar_logger.h"spdlog::level::level_enum SidecarLoggers::global_level = spdlog::level::info;spdlog::level::level_enum SidecarLoggers::getGlobalLevel(){return global_level;}std::vector<spdlog::sink_ptr> SidecarLoggers::createSinks(const std::string &log_file_name){std::vector<spdlog::sink_ptr> sinks;auto sink1 = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();sink1->set_level(SidecarLoggers::getGlobalLevel());sinks.push_back(sink1);auto sink2 = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file_name, 1024 * 1024 * 10, 100, false);sink2->set_level(spdlog::level::debug);sinks.push_back(sink2);return sinks;}void SidecarLoggers::createLogger(const std::string &logger_name){std::string log_file_name = logger_name + "_log.txt";auto sinks = SidecarLoggers::createSinks(log_file_name);auto logger = std::make_shared<spdlog::logger>(logger_name, begin(sinks), end(sinks));logger->set_level(spdlog::level::debug);spdlog::register_logger(logger);}std::shared_ptr<spdlog::logger> SidecarLoggers::getLogger(const std::string &logger_name){auto logger = spdlog::get(logger_name);if(!logger){//looger指向为空createLogger(logger_name);logger = spdlog::get(logger_name);}return logger;}void SidecarLoggers::init(){auto level = spdlog::level::debug;if (std::getenv("STAGE") != NULL){std::string stage = std::getenv("STAGE");if (stage == "dev")level = spdlog::level::debug;}SidecarLoggers::global_level = level;spdlog::flush_every(std::chrono::seconds(1));spdlog::flush_on(spdlog::level::debug);SidecarLoggers::createLogger("sidecar");}
【【C++】spdlog光速入门,C++logger最简单最快的库】
推荐阅读
-
-
-
-
-
-
-
攀升MaxBook P2参数_攀升MaxBook P2配置
-
-
-
-
-
-
-
-
-
-
-
-
电脑版微信怎么发朋友圈 电脑版微信怎么发朋友圈视频
-
江苏省公务员abc类区别 江苏省公务员abc类的区别