spdlog API
spdlog支持向控制台、文件、系统日志以及控制器等目标输出日志,并可设置日志输出级别,以及定义日志输出格式等。
├── bench
├── cmake
├── example // 用法代码
├── include // 实现目录
│ └── spdlog
│ ├── cfg
│ ├── details // 功能函数目录
│ ├── fmt // 库目录
│ │ └── bundled
│ └── sinks // 落地文件格式实现
├── logos
├── scripts
├── src // 组成编译模块生成静态库使用
└── tests // 测试代码
-
设置日志输出样式
set_pattern(pattern_string);
Pattern flags are in the form of
%flag
and 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 -
设置日志等级
日志等级有如下几类://宏定义 #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
-
日志打印函数
使用如下方法默认输出终端/控制台,{}. 花括号为参数代表,可以定义为任意支持标准输出格式类型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");
-
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!
-
标准输出/控制台打印
标准输出依赖于头文件“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)"); }
-
基础文件输出 Basic file logger
#include "spdlog/sinks/basic_file_sink.h" void basic_logfile_example() { try