Basic Example

A minimal example using the full Backend / Frontend API with a console sink.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include "quill/Backend.h"
#include "quill/Frontend.h"
#include "quill/LogMacros.h"
#include "quill/Logger.h"
#include "quill/sinks/ConsoleSink.h"

int main()
{
  quill::Backend::start();

  quill::Logger* logger = quill::Frontend::create_or_get_logger(
    "root", quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1"));

  LOG_INFO(logger, "This is a log info example {}", 123);
}

Key points:

  • A Sink and a Logger are each identified by a unique name so they can be retrieved later.

  • Each Logger owns a PatternFormatter (controls layout) and one or more Sink objects (control output destinations).

  • Backend::start() must be called before log messages can be processed. The backend thread stops automatically when the application exits normally.

  • A macro-free logging interface is also available (see Macro-Free Mode), but the LOG_* macros are recommended for lowest latency because static metadata is resolved at compile time and arguments are not evaluated when the level is disabled.

See Quick Start for the simple-setup path, or Guides for sinks, formatters, and advanced configuration.