Sinks¶
Sinks are objects responsible for writing logs to their respective targets.
A Sink
object serves as the base class for various sink-derived classes.
Each sink handles outputting logs to a single target, such as a file, console, or database.
Upon creation, a sink object is registered and owned by a central manager object, the SinkManager.
For files, one sink is created per filename, and the file is opened once. If a sink is requested that refers to an already opened file, the existing Sink object is returned.
When creating a logger, one or more sinks for that logger can be specified. Sinks can only be registered during the logger creation.
Customizing the Library with User-Defined Sinks¶
You can extend the library by creating and integrating your own Sink
types. The code within the Sink
class is executed by a single backend worker thread.
This can be useful if you want to direct log output to alternative destinations, such as a database, a network service, or even to write Parquet
files.
1#include "quill/Backend.h"
2#include "quill/Frontend.h"
3#include "quill/LogMacros.h"
4#include "quill/Logger.h"
5#include "quill/sinks/Sink.h"
6
7#include <cstdint>
8#include <iostream>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
14/**
15 * This example demonstrates how to implement a custom Sink
16 */
17
18class CustomSink final : public quill::Sink
19{
20public:
21 CustomSink() = default;
22
23 /***/
24 void write_log(quill::MacroMetadata const* /** log_metadata **/, uint64_t /** log_timestamp **/,
25 std::string_view /** thread_id **/, std::string_view /** thread_name **/,
26 std::string const& /** process_id **/, std::string_view /** logger_name **/,
27 quill::LogLevel /** log_level **/, std::string_view /** log_level_description **/,
28 std::string_view /** log_level_short_code **/,
29 std::vector<std::pair<std::string, std::string>> const* /** named_args - only populated when named args in the format placeholder are used **/,
30 std::string_view /** log_message **/, std::string_view log_statement) override
31 {
32 // This function is called by the logger backend worker thread for each LOG_* macro.
33
34 // Typically, this is where you would write the message to a file, send it over the network, etc.
35
36 // In this example, instead of immediately writing the log statement, we cache it.
37 // This can be useful for batching log messages to a database for example.
38
39 // The last character of log_statement is '\n', which we exclude by using size() - 1.
40 _cached_log_statements.push_back(std::string{log_statement.data(), log_statement.size() - 1});
41 }
42
43 /***/
44 void flush_sink() noexcept override
45 {
46 // This function is not called for each LOG_* invocation like the write function.
47
48 // Instead, it is called periodically, when there are no more LOG_* writes left to process,
49 // or when logger->flush() is invoked.
50
51 // In this example, we output all our cached log statements at this point.
52
53 for (auto const& message : _cached_log_statements)
54 {
55 std::cout << message << std::endl;
56 }
57
58 _cached_log_statements.clear();
59 }
60
61 /***/
62 void run_periodic_tasks() noexcept override
63 {
64 // Executes periodic user-defined tasks. This function is frequently invoked by the backend thread's main loop.
65 // Avoid including heavy tasks here to prevent slowing down the backend thread.
66
67 // For example, this could be another place to submit a batch commit to a database, as this
68 // function is called more frequently than `flush_sink`.
69 }
70
71private:
72 std::vector<std::string> _cached_log_statements;
73};
74
75int main()
76{
77 // Start the backend thread
78 quill::BackendOptions backend_options;
79 quill::Backend::start(backend_options);
80
81 auto file_sink = quill::Frontend::create_or_get_sink<CustomSink>("sink_id_1");
82 quill::Logger* logger = quill::Frontend::create_or_get_logger("root", std::move(file_sink));
83
84 LOG_INFO(logger, "Hello from {}", "sink example");
85 LOG_INFO(logger, "Invoking user sink flush");
86
87 logger->flush_log();
88
89 LOG_INFO(logger, "Log more {}", 123);
90}