來源:微信公眾號「編程學習基地」
@
spdlog日志
spdlog是一個開源的、快速的、僅有頭文件的C++11 日志庫,code地址在 https://github.com/gabime/spdlog 目前最新的發布版本為Version 1.8.0。它提供了向流、標准輸出、文件、系統日志、調試器等目標輸出日志的能力。它支持的平台包括Windows、Linux、Mac、Android。
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 log
滾動日志,當日志文件超出規定大小時,會刪除當前日志文件中所有內容,重新開始寫入。
從函數聲明可以看出,參數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文件用來滾動。
示例:
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里面
輸出打印到Concole
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] https://www.cnblogs.com/oucsheep/p/8426548.html
+++ ↩︎