【C++】spdlog--log4cxx有点笨重,试一试spdlog

本文介绍了从log4cxx转向spdlog的原因,因为log4cxx较笨重且更新不频繁。spdlog是一个高性能的C++11日志库,支持跨平台,并能满足轻量级日志需求,如文件存储、日志按天分割。文章提供了spdlog的快速入门指南,包括编译、使用方法,以及如何解决打印行号、控制台输出、文件输出、日志按天分割等问题。此外,作者还分享了自己的开源IM项目CoffeeChat,基于Golang和Flutter开发。
摘要由CSDN通过智能技术生成

 

spdlog是什么

Fast C++ logging library
按照官方介绍,是一个高性能的C++日志组件,支持跨平台,兼容 C++11。原来项目中使用的是log4cxx,我感觉稍微有点笨重,并且很久没有更新了。

在新项目中,我只需要一款轻量级的日志组件,能:

  • 存文件
  • 按照天数切分

快速的浏览了spdlog,满足我的需求,于是开搞!

spdlog快速入门

githubhttps://github.com/gabime/spdlog

以下内容来自spdlog的 README

编译

$ git clone https://github.com/gabime/spdlog.git
$ cd spdlog && mkdir build && cd build
$ cmake .. && make -j

PS:使用cmake来编译,cmake 命令会生成makefile。如果机器上没有cmake,请先安装一下,我的cmake3.14.5macos 10.15

meki-mac-pro:~ xuyc$ cmake -version
cmake version 3.14.5

CMake suite maintained and supported by Kitware (kitware.com/cmake).

使用

#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"

int main() 
{
    spdlog::info("Welcome to spdlog!");
    spdlog::error("Some error message with arg: {}", 1);
    
    spdlog::warn("Easy padding in numbers like {:08d}", 12);
    spdlog::critical("Support for int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
    spdlog::info("Support for floats {:03.2f}", 1.23456);
    spdlog::info("Positional args are {1} {0}..", "too", "supported");
    spdlog::info("{:<30}", "left aligned");
    
    spdlog::set_level(spdlog::level::debug); // Set global log level to debug
    spdlog::debug("This message should be displayed..");    
    
    // change lo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值