-
Notifications
You must be signed in to change notification settings - Fork 5k
Sinks
Sinks are the objects that actually write the log to their target. Each sink should be responsible for only single target (e.g file, console, db)
Each logger contains a vector of one or morestd::shared_ptr<sink>. On each log call (if the log level is right) the logger will call the "sink(log_msg)" function on each of them.
spdlog's sinks have _mt (multi threaded) or _st (single threaded) suffixes to indicate the thread safety. While single threaded sinks cannot be used from multiple threads simultaneously, they are faster because no locking is employed.
rotating_file_sink (defined in [sinks/file_sinks.h] (https://github.com/gabime/spdlog/tree/master/include/spdlog/sinks/file_sinks.h)):
When reaching file max size it will close the file, rename it and create a new file. Both max size and max files to hold are configurable in the ctor.
Usage example:
auto rotating = std::make_shared<spdlog::sinks::rotating_file_sink> ("filename", ".log", 1024*1024, 5, false);will create a sink which will keep its file to max of 1MB and max of 5 rotated files. The last param(false) indicates not to flush upon every log call (for performance reasons) but to rely on whenever the OS decides to flush to the disk.
daily_file_sink ([sinks/file_sinks.h] (https://github.com/gabime/spdlog/tree/master/include/spdlog/sinks/file_sinks.h)):
Will create a new log file each day on the specified time.
Usage example:
auto daily = std::make_shared<spdlog::sinks::daily_file_sink> ("filename", ".log", 14, 55);will create a sink which will create a new log file each day on 14:55
simple_file_sink ([sinks/file_sinks.h] (https://github.com/gabime/spdlog/tree/master/include/spdlog/sinks/file_sinks.h)):
Just write to the give log file with no restrictions
Usage example:
auto simple = std::make_shared<spdlog::sinks::simple_file_sink> ("filename.log", true);will create file logger to "filename.log" and auto flush set to true (flush every log call to disk)
A sink must implement the sink interface.
To implement a new sink all you need to do is implement your own void log(const details::log_msg& msg) function
For example:
#include "spdlog/sinks/sink.h"
class my_sink : public sink
{
void log(const details::log_msg& msg) override
{
// Your code here.
//details::log_msg is a struct containing the log entry info like level, timestamp, thread id etc.
// msg.formatted contains the formatted log.
// msg.raw contains pre formatted log
std::cout << msg.formatted.str();
}
};
..If your sink needs to use locks for thread safety, you can inherit from [base_sink] (../tree/master/include/spdlog/sinks/base_sink.h) which will do the locking for you on each log call. In this case you will need to implement the protected "_sink_it" function.
For example:
class my_threaded_sink : public base_sink < std::mutex >
{
..
protected:
void _sink_it(const details::log_msg& msg) override
{
//Your code here
}
..
}©gabime 2023-2024 spdlog. All Rights Reserved.