Quick Start¶
The library is header only and consists of two main components: the frontend and the backend.
The frontend captures a copy of the log arguments and metadata from each LOG_* statement and places them in a thread-local SPSC queue buffer.
The backend runs in a separate thread, spawned by the library, asynchronously consuming messages from the frontend, formatting them, and writing them to the configured sinks.
To use the library, you need to start the backend thread in your application. Then, set up one or more output Sinks and a Logger.
Once the initialization is complete, you only need to include two header files to issue log statements:
#include "quill/LogMacros.h"#include "quill/Logger.h"
These headers have minimal dependencies, keeping compilation times low.
For even faster compilation, consider building the backend initialization as a static library, as shown in: Recommended Usage Example.
For a quick reference on usage see Cheat Sheet.
Logging to Console¶
1#include "quill/Backend.h"
2#include "quill/Frontend.h"
3#include "quill/LogMacros.h"
4#include "quill/Logger.h"
5#include "quill/sinks/ConsoleSink.h"
6
7#include "quill/std/Array.h"
8
9#include <string>
10#include <utility>
11
12int main()
13{
14 quill::Backend::start();
15
16 // Frontend
17 auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1");
18 quill::Logger* logger = quill::Frontend::create_or_get_logger("root", std::move(console_sink));
19
20 // Change the LogLevel to print everything
21 logger->set_log_level(quill::LogLevel::TraceL3);
22
23 // A log message with number 123
24 int a = 123;
25 std::string l = "log";
26 LOG_INFO(logger, "A {} message with number {}", l, a);
27
28 // libfmt formatting language is supported 3.14e+00
29 double pi = 3.141592653589793;
30 LOG_INFO(logger, "libfmt formatting language is supported {:.2e}", pi);
31
32 // Logging STD types is supported [1, 2, 3]
33 std::array<int, 3> arr = {1, 2, 3};
34 LOG_INFO(logger, "Logging STD types is supported {}", arr);
35
36 // Logging STD types is supported [arr: [1, 2, 3]]
37 LOGV_INFO(logger, "Logging STD types is supported", arr);
38
39 // A message with two variables [a: 123, b: 3.17]
40 double b = 3.17;
41 LOGV_INFO(logger, "A message with two variables", a, b);
42
43 for (uint32_t i = 0; i < 10; ++i)
44 {
45 // Will only log the message once per second
46 LOG_INFO_LIMIT(std::chrono::seconds{1}, logger, "A {} message with number {}", l, a);
47 LOGV_INFO_LIMIT(std::chrono::seconds{1}, logger, "A message with two variables", a, b);
48 }
49}
Logging to File¶
1#include "quill/Backend.h"
2#include "quill/Frontend.h"
3#include "quill/LogMacros.h"
4#include "quill/Logger.h"
5#include "quill/sinks/FileSink.h"
6
7#include <utility>
8
9int main()
10{
11 quill::Backend::start();
12
13 // Frontend
14 auto file_sink = quill::Frontend::create_or_get_sink<quill::FileSink>(
15 "trivial_logging.log",
16 []()
17 {
18 quill::FileSinkConfig cfg;
19 cfg.set_open_mode('w');
20 cfg.set_filename_append_option(quill::FilenameAppendOption::StartDateTime);
21 return cfg;
22 }(),
23 quill::FileEventNotifier{});
24
25 quill::Logger* logger = quill::Frontend::create_or_get_logger("root", std::move(file_sink));
26
27 LOG_INFO(logger, "log something {}", 123);
28 LOG_WARNING(logger, "something else {}", 456);
29}