C++11日志之spdlog

简介: 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日志并进行多维度分析。
目录
相关文章
|
19天前
|
编译器 vr&ar C语言
高效c/c++日志工具zlog使用介绍
高效c/c++日志工具zlog使用介绍
174 2
|
19天前
|
存储 监控 安全
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
170 0
|
19天前
|
C++
glog --- C++日志库
glog --- C++日志库
|
19天前
|
C++
c++日志工具spdLog
c++日志工具spdLog
16 1
|
19天前
|
自然语言处理 Java API
常见C++ 开源日志库的比较
常见C++ 开源日志库的比较
34 0
|
19天前
|
算法 Java 测试技术
【深入探究 C++ 日志库性能比较】glog、log4cplus 和 spdlog 的日志输出性能分析
【深入探究 C++ 日志库性能比较】glog、log4cplus 和 spdlog 的日志输出性能分析
278 0
|
19天前
|
XML 运维 监控
【深入探究 C++ 日志库清理策略】glog、log4cplus 和 spdlog 的日志文件管理策略
【深入探究 C++ 日志库清理策略】glog、log4cplus 和 spdlog 的日志文件管理策略
103 0
|
19天前
|
安全 编译器 API
C++系统日志库精选:深入剖析glog与log4cplus,轻松搭建高效日志系统
C++系统日志库精选:深入剖析glog与log4cplus,轻松搭建高效日志系统
261 0
|
19天前
|
消息中间件 设计模式 Java
spdlog中的异步日志方案
spdlog中的异步日志方案
291 2
|
3天前
|
存储 Serverless 数据安全/隐私保护
C++ 类的成员函数和数据成员的技术性探讨
C++ 类的成员函数和数据成员的技术性探讨
11 0