安装及编译方法:ubuntu spdlog 日志安装及使用_spdlog_logger_info-CSDN博客
h文件:
#ifndef LOGGING_H
#define LOGGING_H
#include <iostream>
#include <cstring>
#include <sstream>
#include <string>
#include <memory>
#include <chrono>
#include <spdlog/spdlog.h>
#include <spdlog/logger.h>
#include <spdlog/common.h>
#include <spdlog/sinks/stdout_sinks.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>
#define ZLOG_PATH "./logs/"
#define ZLOG_FILENAME "mylog.log"
#define MAX_SIZE 1024*1024*5 //单个日志文件大小
#define MAX_FILES 3 //最多存几个日志文件
class ZLOG{
public:
///
/// \brief getInstance
/// \param filePath 日志文件存放的路径
/// \param fileName 日志文件的名字
/// \param mode 日志的模式 0-控制台 1-文件 2-控制台/文件
/// \return
///
static ZLOG *getInstance(std::string filePath = ZLOG_PATH, std::string fileName = ZLOG_FILENAME, int mode = 0){
static ZLOG zlog(filePath, fileName, mode);
return &zlog;
}
std::shared_ptr<spdlog::logger> getLogger(){
return my_logger;
}
private:
std::shared_ptr<spdlog::logger> my_logger; //创建的logger指针
ZLOG() = default;
ZLOG(std::string filePath, std::string fileName, int mode){
std::stringstream log_full_path;
log_full_path << filePath << fileName;
if(access(filePath.c_str(), F_OK) != 0)
{
mkdir(filePath.c_str(), S_IRWXU); //首先检查log目录是否存在,不存在则创建
}
// 限制 mode 范围
mode = mode < 0 ? 0 : mode;
mode = mode > 2 ? 2 : mode;
try{
if(mode == 0){
// 控制台模式
my_logger = spdlog::stdout_color_mt(log_full_path.str());
}else if(mode == 1){
// 日志模式
my_logger = spdlog::rotating_logger_mt("basic_logger", log_full_path.str(), MAX_SIZE, MAX_FILES);
}else if(mode == 2){
// 控制台 + 日志 模式
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_full_path.str(), MAX_SIZE, MAX_FILES, true);
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(console_sink);
sinks.push_back(file_sink);
my_logger = std::make_shared<spdlog::logger>("multi-sink", begin(sinks), end(sinks));
}
my_logger->set_level(spdlog::level::trace); // 打印等级,等级 trace 以下的将不会被打印出来,可更改
my_logger->set_pattern("[%Y-%m-%d %T][thread %t][%l]%v"); // 打印模板
}catch(const spdlog::spdlog_ex& ex){
std::cout << "ZLOG error !" << "Log initialization failed: " << ex.what() << std::endl;
}
}
~ZLOG(){
spdlog::drop_all(); //logger使用完成后,要执行drop操作,否则不能循环创建同一类型的logger
}
};
// use embedded macro to support file and line number
#define ZLOG_TRACE(...) SPDLOG_LOGGER_CALL(ZLOG::getInstance()->getLogger().get(), spdlog::level::trace, __VA_ARGS__)
#define ZLOG_DEBUG(...) SPDLOG_LOGGER_CALL(ZLOG::getInstance()->getLogger().get(), spdlog::level::debug, __VA_ARGS__)
#define ZLOG_INFO(...) SPDLOG_LOGGER_CALL(ZLOG::getInstance()->getLogger().get(), spdlog::level::info, __VA_ARGS__)
#define ZLOG_WARN(...) SPDLOG_LOGGER_CALL(ZLOG::getInstance()->getLogger().get(), spdlog::level::warn, __VA_ARGS__)
#define ZLOG_ERROR(...) SPDLOG_LOGGER_CALL(ZLOG::getInstance()->getLogger().get(), spdlog::level::err, __VA_ARGS__)
#endif // LOGGING_H
main.cpp文件
#include "logging.h"
int main() {
ZLOG::getInstance("./logs/", "mylog.log", 3);
int param = 1;
ZLOG_TRACE("this is trace log record, param: {}", ++param); // int type param is ok
ZLOG_DEBUG("this is debug log record, param: {}", ++param);
ZLOG_INFO("this is info log record, param: {}", ++param);
ZLOG_WARN("this is warn log record, param: {}", double(++param)); // double type param is ok
ZLOG_ERROR("this is error log record, param: {}", std::to_string(++param)); // string type param is ok
return 0;
}