C++11日志之spdlog

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: C++11日志之spdlog

c++日志工具spdlog


spdlog日志是纯头文件,使用起来比较方便。使用时只需要简单的初始化即可,这里对其初始化做了一个简单的封装,这样使用起来更加方便。


封装类代码


头文件


cspdlog.h

#ifndef _CSPDLOG_H_
#define _CSPDLOG_H_


#include "spdlog/spdlog.h"
#include "spdlog/fmt/bin_to_hex.h"
#include <memory>
#include <string>

class CSpdlog
{
protected:
  
  CSpdlog();
  ~CSpdlog();
  static CSpdlog *m_instance;
  
  
public:
  
  static CSpdlog *GetInstance();
  void Init(const std::string & name,const std::string &logPath, std::size_t max_size=1048576, std::size_t max_file = 2);
  void SetConsoleLogLevel(spdlog::level::level_enum log_level);
  void SetFileLogLevel(spdlog::level::level_enum log_level);

private:
  std::vector<spdlog::sink_ptr> m_sinks;
  std::shared_ptr<spdlog::logger> m_logger;
};


#endif


源文件


cspdlog.cpp

#include "cspdlog.h"

#include <cstdio>
#include <iostream>
#include "spdlog/sinks/stdout_color_sinks.h" // or "../stdout_sinks.h" if no color needed
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"




CSpdlog::CSpdlog()
{
  
}

CSpdlog::~CSpdlog()
{
  
}

void CSpdlog::Init(const std::string & name, const std::string &log_path, std::size_t max_size, std::size_t max_file )
{
  try 
  {   
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::debug);
    console_sink->set_pattern("%^[%Y-%m-%d %H:%M:%S:%e] [%n] [tid: %t] [%l] %v%$");

    std::string logFile = log_path + "/" + name + ".txt";
    
    //auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", false);
    auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logFile, max_size, max_file);
    file_sink->set_pattern("[%Y-%m-%d %H:%M:%S:%e] [%n] [tid: %t] [%l] %v");
    file_sink->set_level(spdlog::level::warn);
    
    m_sinks.push_back(console_sink);
    m_sinks.push_back(file_sink);
    
    //spdlog::logger *logger = new spdlog::logger("multi_sink", {console_sink, file_sink});
    m_logger = std::make_shared<spdlog::logger>(name, begin( m_sinks ), end( m_sinks ));

    //spdlog::set_error_handler([](const std::string& msg){printf("*****Custom log error handler, %s*****%\n", msg.c_str());});
    
    //注册到spdlog里
    spdlog::register_logger(m_logger);
    //m_logger->info("log init done.");
    m_logger->flush_on(spdlog::level::level_enum::warn);  

  }
  catch (const spdlog::spdlog_ex &ex)
  {
    std::cout<<"Log initialization faild"<<ex.what()<<std::endl;
  }
}

void CSpdlog::SetConsoleLogLevel(spdlog::level::level_enum log_level)
{
  m_logger->set_level(log_level);
}

void CSpdlog::SetFileLogLevel(spdlog::level::level_enum log_level)
{
  m_sinks[1]->set_level(log_level);
}

CSpdlog* CSpdlog::m_instance = NULL;

CSpdlog* CSpdlog::GetInstance()
{
  if ( m_instance == NULL )
  {
    m_instance = new CSpdlog;
  }

  return m_instance;
}


测试主程序

#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <cspdlog.h>

using namespace std;



int main()
{
  CSpdlog::GetInstance()->Init("test","./log"); //初始化日志
  CSpdlog::GetInstance()->SetConsoleLogLevel(spdlog::level::debug); //设置终端界面输出级别
  CSpdlog::GetInstance()->SetFileLogLevel(spdlog::level::warn);     //设置log文件输出级别
  
  auto logger = spdlog::get("test");   //获取日志句柄
  
  logger->warn("test start.");

  int counter = 0;  
  while(1)
  {
    logger->debug("debug msg, counter: {}",counter);
    logger->info("info msg, counter: {}",counter);
    logger->warn("warn msg, counter: {}",counter);
    logger->error("error msg, counter: {}",counter);
    logger->critical("critical msg, counter: {}",counter);
    logger->trace("trace msg, counter: {}",counter);
    usleep(500000);
  }

  return 0;
}


编译运行


我的通用Makefile

#通用makefile
#文件目录
DEBUG_DIR=.
SRC_DIR=.
INC_DIR=. ./spdlog

SRC=$(wildcard $(SRC_DIR)/*.cpp )  #源文件
OBJS=$(patsubst $(SRC_DIR)/%.cpp,$(DEBUG_DIR)/%.o,$(SRC))

#目标文件名
TARGET=test
INSTALL_PATH ?= .

#修改编译器
ARCH ?= 
CC=$(ARCH)gcc
CPP=$(ARCH)g++
AR=$(ARCH)ar
STRIP=$(ARCH)strip


CFLAGS += -Wall -std=c++11
LDFLAGS += -lpthread

CFLAGS  += $(foreach dir,$(INC_DIR),-I$(dir))

all:$(TARGET)
$(TARGET): $(OBJS)
  $(CPP) $(OBJS) -o $@ $(CFLAGS) $(LDFLAGS)
  $(STRIP) $(TARGET)
#cp $(TARGET) $(INSTALL_PATH)


$(DEBUG_DIR)/%.o: $(SRC_DIR)/%.cpp
  $(CPP) $(CFLAGS) -c $< -o $@ 
  
#@echo $(SRC)
#@echo $(OBJS)

  
clean:
  -rm $(OBJS) $(LIB_TARGET)


编译

make 
g++ -Wall -std=c++11 -I. -I./spdlog -c cspdlog.cpp -o cspdlog.o 
g++ -Wall -std=c++11 -I. -I./spdlog -c main.cpp -o main.o 
g++ ./cspdlog.o ./main.o -o test -Wall -std=c++11 -I. -I./spdlog 
strip test


运行


console输出:


文件输出:

$cat log/test.txt 
[2021-08-17 15:30:59:526] [test] [tid: 20418] [warning] test start.
[2021-08-17 15:30:59:527] [test] [tid: 20418] [warning] warn msg, counter: 0
[2021-08-17 15:30:59:528] [test] [tid: 20418] [error] error msg, counter: 0
[2021-08-17 15:30:59:529] [test] [tid: 20418] [critical] critical msg, counter: 0
[2021-08-17 15:31:00:031] [test] [tid: 20418] [warning] warn msg, counter: 0
[2021-08-17 15:31:00:032] [test] [tid: 20418] [error] error msg, counter: 0
[2021-08-17 15:31:00:032] [test] [tid: 20418] [critical] critical msg, counter: 0
[2021-08-17 15:31:00:533] [test] [tid: 20418] [warning] warn msg, counter: 0
[2021-08-17 15:31:00:533] [test] [tid: 20418] [error] error msg, counter: 0
[2021-08-17 15:31:00:534] [test] [tid: 20418] [critical] critical msg, counter: 0


测试源代码以上传码云


地址:

https://gitee.com/fensnote/demo_code/tree/master/cpp/spdlog

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
4月前
|
缓存 Linux 编译器
【C++】CentOS环境搭建-安装log4cplus日志组件包及报错解决方案
通过上述步骤,您应该能够在CentOS环境中成功安装并使用log4cplus日志组件。面对任何安装或使用过程中出现的问题,仔细检查错误信息,对照提供的解决方案进行调整,通常都能找到合适的解决之道。log4cplus的强大功能将为您的项目提供灵活、高效的日志管理方案,助力软件开发与维护。
132 0
|
5月前
|
存储 运维 监控
超级好用的C++实用库之日志类
超级好用的C++实用库之日志类
72 0
|
6月前
|
Java 编译器 数据库
异步日志方案——spdlog
异步日志方案——spdlog
|
8月前
spdlog 日志库部分源码说明——让你可以自定义的指定自动切换日志时间
spdlog 日志库部分源码说明——让你可以自定义的指定自动切换日志时间
213 7
|
8月前
Liunx怎么安装spdlog(这是用来管理日志部分)
Liunx怎么安装spdlog(这是用来管理日志部分)
163 7
|
8月前
|
C++
spdlog 日志库部分源码说明——日志格式设定,DIY你自己喜欢的调试信息,你能调试的远比你想象的还要丰富
spdlog 日志库部分源码说明——日志格式设定,DIY你自己喜欢的调试信息,你能调试的远比你想象的还要丰富
465 6
|
8月前
|
监控 C++
c++实战篇(二)——基于自旋锁实现的日志服务模块
c++实战篇(二)——基于自旋锁实现的日志服务模块
|
9月前
|
C++
glog --- C++日志库
glog --- C++日志库
|
1天前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
1月前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
68 19