來源:微信公衆號「編程學習基地」
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
+++ ↩︎