spdlog 使用

spdlog 是一个日志库,直接引用头文件即可使用,速度快,异步打印日志。

对应的git地址 spdloggit地址

对应的目录

把上面划线的文件夹引入到自己的工程中,即可使用spdlog

下面是使用例子

cpp 复制代码
   inline static void create_logging(const std::string& dir) 
    {
        spdlog::init_thread_pool(8192 * 2, 1); // 16k
        std::shared_ptr<spdlog::logger> l;
        l = spdlog::create_async_nb<spdlog::sinks::rotating_file_sink_mt>
                ("async_file_logger", dir, 1024 * 1024 * 10, 5);
        // 日志级别从低到高 trace, debug, info, warn, err, critical, off, n_levels
        l->flush_on(spdlog::level::info);//warn
        l->set_pattern("[%Y-%m-%d %T.%e][tid:%t  %s:%#] [%l] %v"); //设置信息格式
        spdlog::flush_every(std::chrono::seconds(3));
        spdlog::set_default_logger(l);
    }
    int main()
    {
    	std::string path = "./log.txt";
    	create_logging(path); //创建log文件
    	SPDLOG_ERROR(u8"log文件 error 信息{}", path.c_str()); //打印error类信息
    	SPDLOG_INFO(u8"log文件 info 信息{}", path.c_str());
    	return 0;
    }
相关推荐
感哥7 小时前
C++ 面向对象
c++
沐怡旸9 小时前
【底层机制】std::shared_ptr解决的痛点?是什么?如何实现?如何正确用?
c++·面试
感哥15 小时前
C++ STL 常用算法
c++
saltymilk1 天前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥1 天前
C++ lambda 匿名函数
c++
沐怡旸1 天前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥1 天前
C++ 内存管理
c++
博笙困了2 天前
AcWing学习——双指针算法
c++·算法
感哥2 天前
C++ 指针和引用
c++
感哥2 天前
C++ 多态
c++