spdlog API一览

本文介绍了spdlog这个日志库的API,包括如何设置日志输出样式,设定日志等级,以及各种日志打印函数的使用。还详细讲解了backtrace/dump功能,标准输出/控制台打印,基础文件输出,Rotating和daily文件输出,以及异步日志和系统日志的实现。此外,文章提到了如何在项目中集成和使用spdlog。
摘要由CSDN通过智能技术生成

spdlog API

spdlog支持向控制台、文件、系统日志以及控制器等目标输出日志,并可设置日志输出级别,以及定义日志输出格式等。

├── bench
├── cmake
├── example             // 用法代码
├── include             // 实现目录
│   └── spdlog
│       ├── cfg
│       ├── details     // 功能函数目录
│       ├── fmt         // 库目录
│       │   └── bundled
│       └── sinks       // 落地文件格式实现
├── logos
├── scripts
├── src                 // 组成编译模块生成静态库使用
└── tests               //  测试代码
  1. 设置日志输出样式

    set_pattern(pattern_string);
    

    Pattern flags are in the form of %flagand resembles the strftime function:

    flag meaning example
    %v The actual text to log “some user text”
    %t Thread id “1232”
    %P Process id “3456”
    %n Logger’s name “some logger name”
    %l The log level of the message “debug”, “info”, etc
    %L Short log level of the message “D”, “I”, etc
    %a Abbreviated weekday name “Thu”
    %A Full weekday name “Thursday”
    %b Abbreviated month name “Aug”
    %B Full month name “August”
    %c Date and time representation “Thu Aug 23 15:35:46 2014”
    %C Year in 2 digits “14”
    %Y Year in 4 digits “2014”
    %D or %x Short MM/DD/YY date “08/23/14”
    %m Month 01-12 “11”
    %d Day of month 01-31 “29”
    %H Hours in 24 format 00-23 “23”
    %I Hours in 12 format 01-12 “11”
    %M Minutes 00-59 “59”
    %S Seconds 00-59 “58”
    %e Millisecond part of the current second 000-999 “678”
    %f Microsecond part of the current second 000000-999999 “056789”
    %F Nanosecond part of the current second 000000000-999999999 “256789123”
    %p AM/PM “AM”
    %r 12 hour clock “02:55:02 pm”
    %R 24-hour HH:MM time, equivalent to %H:%M “23:55”
    %T or %X ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S “23:55:59”
    %z ISO 8601 offset from UTC in timezone ([+/-]HH:MM) “+02:00”
    %E Seconds since the epoch “1528834770”
    %% The % sign “%”
    %+ spdlog’s default format “[2014-10-31 23:46:59.678] [mylogger] [info] Some message”
    %^ start color range (can be used only once) “[mylogger] [info(green)] Some message”
    %$ end color range (for example %1%$ %v) (can be used only once) [+++] Some message
    %@ Source file and line (use SPDLOG_TRACE(…), SPDLOG_INFO(…) etc. instead of spdlog::trace(…) my_file.cpp:123
    %s Basename of the source file (use SPDLOG_TRACE(…), SPDLOG_INFO(…) etc.) my_file.cpp
    %g Full or relative path of the source file as appears in the __FILE__ macro (use SPDLOG_TRACE(…), SPDLOG_INFO(…) etc.) /some/dir/my_file.cpp
    %# Source line (use SPDLOG_TRACE(…), SPDLOG_INFO(…) etc.) 123
    %! Source function (use SPDLOG_TRACE(…), SPDLOG_INFO(…) etc. see tweakme for pretty-print) my_func
    %o Elapsed time in milliseconds since previous message 456
    %i Elapsed time in microseconds since previous message 456
    %u Elapsed time in nanoseconds since previous message 11456
    %O Elapsed time in seconds since previous message 4
  2. 设置日志等级
    日志等级有如下几类:

    //宏定义
    #define SPDLOG_LEVEL_TRACE 0
    #define SPDLOG_LEVEL_DEBUG 1
    #define SPDLOG_LEVEL_INFO 2
    #define SPDLOG_LEVEL_WARN 3
    #define SPDLOG_LEVEL_ERROR 4
    #define SPDLOG_LEVEL_CRITICAL 5
    #define SPDLOG_LEVEL_OFF 6
    //枚举定义
    enum level_enum
    {
         
        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,
        n_levels
    };
    
    spdlog::set_level(spdlog::level::info); // Set global log level to info
    
  3. 日志打印函数
    使用如下方法默认输出终端/控制台,{}. 花括号为参数代表,可以定义为任意支持标准输出格式类型

    spdlog::info("Welcome to spdlog version {}.{}.{}  !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
    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("{:>8} aligned, {:<8} aligned", "right", "left");
    
  4. backtrace/dump使用
    backtrace/dump 会开辟一个缓冲区,将其他的等级之下的日志保存起来,在需要的时候弹出来。

    //enable_backtrace(20); 打开 backtrace/dump 功能,参数为 dump个数
    spdlog::enable_backtrace(20); // create ring buffer with capacity of 10  messages
    for (int i = 0; i < 100; i++)
    {
         
        //注意为设置输出等级之下的日志
        spdlog::debug("Backtrace message {}", i); // not logged..
    }
    // e.g. if some error happened:
    spdlog::dump_backtrace(); // log them now!
    
  5. 标准输出/控制台打印
    标准输出依赖于头文件“spdlog/sinks/stdout_color_sinks.h” 或者 #include “spdlog/sinks/stdout_sinks.h”,区别是是否支持对输出颜色的设置。

    #include "spdlog/spdlog.h"
    #include "spdlog/sinks/stdout_color_sinks.h"
    // or #include "spdlog/sinks/stdout_sinks.h" if no colors needed.
    void stdout_example()
    {
         
        // create color multi threaded logger
        auto console = spdlog::stdout_color_mt("console");    
        auto err_logger = spdlog::stderr_color_mt("stderr");    
        spdlog::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name)");
    }
    
  6. 基础文件输出 Basic file logger

    #include "spdlog/sinks/basic_file_sink.h"
    void basic_logfile_example()
    {
         
        try 
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

血_影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值