Quick Start - Quill v11.0.2 (original) (raw)

Quickest Setup

For the quickest and simplest setup:

1#include "quill/SimpleSetup.h" 2#include "quill/LogFunctions.h" 3 4int main() 5{ 6 // log to the console 7 auto* logger = quill::simple_logger(); 8 quill::info(logger, "Hello from {}!", "Quill"); 9 10 // log to a file 11 auto* logger2 = quill::simple_logger("test.log"); 12 quill::warning(logger2, "This message goes to a file"); 13}

Architecture Overview

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 (Single Producer Single Consumer) queue buffer. Each frontend thread has its own lock-free queue, ensuring no contention between logging threads.

The backend runs in a separate thread, spawned by the library, asynchronously consuming messages from all frontend queues, formatting them, and writing them to the configured sinks.

Detailed Setup

For more detailed control, 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:

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 10#include 11 12int main() 13{ 14 quill::Backend::start(); 15 16 // Frontend 17 auto console_sink = quill::Frontend::create_or_get_sinkquill::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 8 9int main() 10{ 11 quill::Backend::start(); 12 13 // Frontend 14 auto file_sink = quill::Frontend::create_or_get_sinkquill::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}