来源:微信公众号「编程学习基地」
html
@git
spdlog是一个开源的、快速的、仅有头文件的C++11 日志库,code地址在 https://github.com/gabime/spdlog 目前最新的发布版本为Version 1.8.0。它提供了向流、标准输出、文件、系统日志、调试器等目标输出日志的能力。它支持的平台包括Windows、Linux、Mac、Android。github
spdlog只要包含头文件便可使用,简单方便,下载源码以后找到include目录,将include目录下的spdlog复制到你须要打印日志的项目下,而后导入头文件编程
#include "spdlog/spdlog.h" #include "spdlog/sinks/rotating_file_sink.h"
basic log微信
不带滚动,日志文件会一直被写入,不断变大。多线程
// Create basic file logger (not rotated) auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt"); my_logger->info("Some log message");
rotating logapp
滚动日志,当日志文件超出规定大小时,会删除当前日志文件中全部内容,从新开始写入。函数
从函数声明能够看出,参数max_file_size 规定了文件的最大值,文件内容超过此值就会清空。学习
rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files)
参数max_files 规定了滚动文件的个数。当logger_name存满时,将其名称更改成logger_name.1,再新建一个logger_name文件来存储新的日志。再次存满时,把logger_name.1更名为logger_name.2,logger_name更名为logger_name.1,新建一个logger_name来存放新的日志。max_files 数量为几,就能够有几个logger_name文件用来滚动。
ui
示例:
size_t max_size = 1024 * 10; std::string basename = "testLog.log"; std::shared_ptr<spdlog::logger> file_logger; //日志的文件指针 file_logger = spdlog::rotating_logger_mt("2232", basename, max_size, 0);
日志保存在工做目录下的testLog.log里面
spdlog::set_pattern("%+ [thread %t]"); //设置输出格式直接打印 spdlog::trace("This message shold not be displayed!"); spdlog::debug("This message shold not be displayed!");
控制台带颜色输出
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, };
spdlog::set_level(spdlog::level::info); // Set global log level to debug
设置日志等级以后能够打印的日志等级最高位info,也就是debug和trace不打印
spdlog::set_pattern("%+ [thread %t]"); spdlog::set_level(spdlog::level::info); // Set global log level to debug spdlog::trace("This message shold not be displayed!"); spdlog::debug("This message shold not be displayed!"); spdlog::info("This message shold be displayed.."); spdlog::critical("This message shold be displayed.."); spdlog::set_level(spdlog::level::debug); // Set global log level to debug spdlog::debug("This message shold be displayed.."); spdlog::critical("This message shold be displayed..");
打印结果:
[2020-09-20 00:24:29.160] [info] This message shold be displayed.. [thread 12932] [2020-09-20 00:24:29.162] [critical] This message shold be displayed.. [thread 12932] [2020-09-20 00:24:29.162] [debug] This message shold be displayed.. [thread 12932] [2020-09-20 00:24:29.162] [critical] This message shold be displayed.. [thread 12932]
参考:https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
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 |
file_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e][thread %t][%@,%!][%l] : %v");
//Release and close all loggers //把全部的log对象智能指针放置到unordered_map中去,而后调用clear函数 spdlog::drop_all();
https://www.cnblogs.com/eskylin/archive/2017/03/01/6483199.html
[1] http://www.javashuo.com/article/p-zjprxayw-nw.html
+++ ↩︎