參考文章:ios
log庫spdlog簡介及使用 - 網絡資源是無限的 - CSDN博客 http://blog.csdn.net/fengbingchun/article/details/78347105git
spdLog的使用 - 煙消bug雲散的專欄 - CSDN博客 http://blog.csdn.net/yanxiaobugyunsan/article/details/79088533github
官方參考文檔: QuickStart · gabime/spdlog Wiki · GitHub緩存
https://github.com/gabime/spdlog/wiki/1.-QuickStart網絡
一、下載源碼app
代碼地址在 https://github.com/gabime/spdlog函數
點擊downLoad下載便可。ui
二、example解析spa
下載壓縮包並解壓:使用visual studio 打開vcxproj後綴的項目文件(我用的是VS2013).net
在解決方案中找到example.cpp,這個源文件例舉了spdlog的各類用法:
首先須要包含spdlog的頭文件
#include "spdlog/spdlog.h"
而且要聲明spdlog的命名空間
namespace spd = spdlog;
(1)控制檯(console)輸出日誌
使用控制檯輸出日誌的話,須要這兩個頭文件:
#include <iostream>
#include <memory>
代碼以下:
// Console logger with color auto console = spd::stdout_color_mt("console"); console->info("Welcome to spdlog!"); console->error("Some error message with arg{}..", 1); // Formatting examples console->warn("Easy padding in numbers like {:08d}", 12); console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); console->info("Support for floats {:03.2f}", 1.23456); console->info("Positional args are {1} {0}..", "too", "supported"); console->info("{:<30}", "left aligned"); spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function");
auto console = spd::stdout_color_mt("console"); 中「console」爲logger名稱,能夠隨意命名。
warn,critical,info 爲不一樣等級的log,輸出在控制檯會以不一樣顏色表示。
注意,logger使用完,程序關閉以前須要調用drop函數釋放logger對象,不然若是程序沒有關閉,就沒法再創建一樣名稱的logger。
在example.cpp中main函數的最後調用了
// Release and close all loggers spdlog::drop_all();
若是隻想關閉console的log,能夠這樣寫:
spd::drop("basic_logger");
(2)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");
(3)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文件用來滾動。
下面的例子運行後生成了三個log文件。
// Create a file rotating logger with 5mb size max and 3 rotated files //auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 256, 2); for (int i = 0; i < 10; ++i) rotating_logger->info("{} * {} equals {:>10}", i, i, i*i);
每一個文件內容以下,後綴數字越大,日誌內容越早:
(4)daily log
天天會新建一個日誌文件,新建日誌文件的時間可本身設定。
// Create a daily logger - a new file is created every day on 2:30am auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); // trigger flush if the log severity is error or higher daily_logger->flush_on(spd::level::err); daily_logger->info(123.44);
上述代碼輸出的日誌,若是程序不退出的話,就是天天2:30 am建立新的文件。若是一天屢次運行這個程序,就會有多個日誌文件,以下圖:
爲了把天天的log寫到同一個文件中去,參考http://blog.csdn.net/yanxiaobugyunsan/article/details/79088533
能夠這樣寫:
//建立文件名相似於: log_2018-01-17.txt typedef spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::dateonly_daily_file_name_calculator> dateonly_daily_file_sink_mt; auto m_logger = spdlog::create<dateonly_daily_file_sink_mt>("m_logger", "logs/dateonly.txt", 0, 0); m_logger->info("test daily info"); m_logger->error("test daily error");
(5)flush 將buffer刷入文件
遇到指定級別的日誌會立馬將緩存輸出到文件中,若是不馬上寫入,當程序發生崩潰或產生異常而退出時,有些重要log可能還沒等寫入到文件中。日誌的各個級別以下面代碼所示:
typedef enum { trace = 0, debug = 1, info = 2, warn = 3, err = 4, critical = 5, off = 6 } level_enum;
// trigger flush if the log severity is error or higher daily_logger->flush_on(spd::level::err); daily_logger->info(123.44); daily_logger->error("Error happended! ");
基本的日誌輸出大概這樣,下一篇繼續。