大家好,又见面了,我是你们的朋友全栈君。
#ifndef _MY_LOG_HPP_
#define _MY_LOG_HPP_
#include “spdlog/spdlog.h”
#include “spdlog/sinks/basic_file_sink.h” // support for basic file logging
#include “spdlog/sinks/rotating_file_sink.h” // support for rotating file logging
#include “spdlog/sinks/stdout_sinks.h”
#include “spdlog/sinks/stdout_color_sinks.h” // or “../stdout_sinks.h” if no colors needed
#include “spdlog/sinks/daily_file_sink.h”
class MyLog
{
public:
static MyLog& GetInstance();
protected:
MyLog();
~MyLog();
private:
MyLog(const MyLog& rhs) {}
MyLog& operator = (const MyLog& rhs) {}
public:
/*
trace = SPDLOG_LEVEL_TRACE,
debug = SPDLOG_LEVEL_DEBUG,
info = SPDLOG_LEVEL_INFO,
warn = SPDLOG_LEVEL_WARN,
err = SPDLOG_LEVEL_ERROR,
critical = SPDLOG_LEVEL_CRITICAL,
off = SPDLOG_LEVEL_OFF,
*/
bool InitLogObject(int consolelevel=0, const int filelevel=0, int maxsize=1000, int backIndex=10,int isDay=1);
auto& GetConsoleObject();
auto& GetFileObject();
public:
int m_isDay;
};
MyLog::MyLog()
{
}
MyLog::~MyLog()
{
std::cout << “析构了” << std::endl;
}
MyLog& MyLog::GetInstance()
{
static MyLog instance;
return instance;
}
auto& MyLog::GetConsoleObject()
{
return spdlog::get(“console”);
}
auto& MyLog::GetFileObject()
{
if (m_isDay==1)
{
return spdlog::get(“daily_logger”);
}
return spdlog::get(“file_logger”);
}
bool MyLog::InitLogObject(int consolelevel, int filelevel,int maxsize,int backIndex,int isDay)
{
try
{
m_isDay = isDay;
auto console = spdlog::stdout_logger_mt(“console”);
switch (consolelevel)
{
case 0:console->set_level(spdlog::level::trace); break;
case 1:console->set_level(spdlog::level::debug); break;
case 2:console->set_level(spdlog::level::info); break;
case 3:console->set_level(spdlog::level::warn); break;
case 4:console->set_level(spdlog::level::err); break;
case 5:console->set_level(spdlog::level::critical); break;
case 6:console->set_level(spdlog::level::off); break;
default:
console->set_level(spdlog::level::trace);
break;
}
// 单位M 即:maxsize M
auto file_logger = spdlog::rotating_logger_mt(“file_logger”, “logs/rotat.log”, 1024 * 1024 * maxsize, backIndex);
auto daily_logger = spdlog::daily_logger_mt(“daily_logger”, “logs/daily.log”, 23, 59);
switch (filelevel)
{
case 0:
{
file_logger->set_level(spdlog::level::trace);
daily_logger->set_level(spdlog::level::trace);
break;
}
case 1:
{
file_logger->set_level(spdlog::level::debug);
daily_logger->set_level(spdlog::level::debug);
break;
}
case 2:
{
file_logger->set_level(spdlog::level::info);
daily_logger->set_level(spdlog::level::info);
break;
}
case 3:
{
file_logger->set_level(spdlog::level::warn);
daily_logger->set_level(spdlog::level::warn);
break;
}
case 4:
{
file_logger->set_level(spdlog::level::err);
daily_logger->set_level(spdlog::level::err);
break;
}
case 5:
{
file_logger->set_level(spdlog::level::critical);
daily_logger->set_level(spdlog::level::critical);
break;
}
case 6:
{
file_logger->set_level(spdlog::level::off);
daily_logger->set_level(spdlog::level::off);
break;
}
default:
{
file_logger->set_level(spdlog::level::trace);
daily_logger->set_level(spdlog::level::trace);
break;
}
}
return true;
}
catch (const spdlog::spdlog_ex& ex)
{
std::cout << “Log initialization failed: ” << ex.what() << std::endl;
}
return true;
}
#endif // !_MY_LOG_HPP_
//调用代码片段
MyLog::GetInstance().InitLogObject();
…….
std::stringstream ss;
ss << __FILE__ << “:” << __FUNCTION__ << “:” << __LINE__ << “:”;
ss<< std::this_thread::get_id();
MyLog::GetInstance().GetFileObject()->info(“日志:{},{}”, “测试文件”, ss.str().data());
MyLog::GetInstance().GetConsoleObject()->info(“日志:{},{}”, “测试控制台”, ss.str().data());
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/152321.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...