因为自己想要个题目那种效果的,又没找到很符合的博客,就整合了下大佬们的博客封了个,具体的内容都放到代码注释里了,我这个目前在windows下只能将log文件创建到exe所处的目录中,暂时仍未解决该问题,欢迎大佬指正(已解决,直接将文件名设置为带路径的即可)
#ifndef __MY_LOG__
#define __MY_LOG__
#include <iostream>
#include <string>
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/logger.h"
#include "spdlog/sinks/stdout_sinks.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/common.h"
#include "spdlog/sinks/rotating_file_sink.h"
using namespace spdlog;
#define LOG_TRACE(...) SPDLOG::getInstance().logger().get()->trace(__VA_ARGS__)
#define LOG_DEBUG(...) SPDLOG::getInstance().logger().get()->debug(__VA_ARGS__)
#define LOG_INFO(...) SPDLOG::getInstance().logger().get()->info(__VA_ARGS__)
#define LOG_WARN(...) SPDLOG::getInstance().logger().get()->warn(__VA_ARGS__)
#define LOG_ERROR(...) SPDLOG::getInstance().logger().get()->error(__VA_ARGS__)
#define LOG_CRITICAL(...) SPDLOG::getInstance().logger().get()->critical(__VA_ARGS__)
// spdlog打印信息格式
/*
LOG_WARN("Easy padding in numbers like {:08d}", 12);
// [2021-04-07 23:49:24.916] [warning] Easy padding in numbers like 00000012
LOG_CRITICAL("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
// [2021-04-07 23:49:24.916] [critical] Support for int: 42; hex: 2a; oct: 52; bin: 101010
LOG_INFO("Support for floats {1:06.4f}", 1.23456, 9.8754321);
// [2021-04-07 23:49:24.916] [info] Support for floats 9.8754
LOG_INFO("Positional args are {1} {0}..", "too", "supported");
// [2021-04-07 23:49:24.916] [info] Positional args are supported too..
LOG_INFO("{:>8} aligned, {:<8} aligned", "right", "left");
// [2021-04-07 23:49:24.916] [info] right aligned, left aligned
*/
/*
使用时先执行init方法,参数
std::string logger_name :生成日志文件的名称,默认为log
std::size_t max_size :单个日志文件的大小,默认为10mb
std::size_t max_files :最大日志数目,默认为10
*/
class SPDLOG
{
private:
SPDLOG() = default;
std::shared_ptr<spdlog::logger> logger_ptr_;
public:
static SPDLOG& getInstance()
{
static SPDLOG instance;
return instance;
}
void inline init(std::string logger_name = "log", std::size_t max_size = 1024*1024*10, std::size_t max_files = 10);
std::shared_ptr<spdlog::logger> logger() { return logger_ptr_; }
};
void inline SPDLOG::init(std::string logger_name, std::size_t max_size, std::size_t max_files)
{
try {
/* 通过multi-sink的方式创建复合logger,实现方式为:先分别创建文件sink和控制台sink,并将两者放入sink 向量中,组成一个复合logger */
/* file sink */
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logger_name, max_size, max_files);
file_sink->set_level(spdlog::level::trace);
file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%t] %v");
/* 控制台sink */
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::trace);
console_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%t] %v");
/* Sink组合 */
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(console_sink);
sinks.push_back(file_sink);
logger_ptr_ = std::make_shared<spdlog::logger>("multi-sink", begin(sinks), end(sinks));
std::cout << "SPDLOG: create spdlog success!" << std::endl;
}
catch (const spdlog::spdlog_ex& ex) {
perror("spdlog init error.");
}
}
#endif