Skip to main content

spdlog Knowledge Base

spdlog is a very fast, header-only-by-default C++ logging library built on top of fmt. You add one include, call spdlog::info(...), and you have color console output — no build step, no configuration file, no init call required.

It displaced glog, log4cplus, and hand-rolled iostream/macro logging as the default choice for new C++ projects for a simple reason: it is both easier to start with and faster in production. Throughput is measured in millions of lines per second, formatting uses fmt's compile-time-checked {} syntax instead of printf, and every layer above the basics — file rotation, async logging, backtraces, custom sinks — is opt-in rather than mandatory ceremony.

How this is organised

Overview → Basics gets you logging in five minutes. Loggers and Registry and Sinks are the architecture you configure once per application — how loggers, sinks, and the global registry fit together. Formatting, Async Logging, and Performance and Configuration are the tuning layers you reach for once the defaults stop fitting: custom pattern flags, moving I/O off the hot path, and squeezing out the last bit of overhead.

Sections

SectionWhat it covers
OverviewWhat it is, installing it, the sink architecture, how it compares to glog/Boost.Log
BasicsThe default logger, log levels, fmt-style formatting
Loggers & RegistryCreating loggers, the global registry, lifetime, multi-sink loggers
SinksConsole, file, rotating, daily, syslog, and writing your own
Formatting & PatternsPattern flags, custom flag formatters, source location
Async LoggingThe thread pool, overflow policies, when async pays off
Performance & ConfigurationCompile-time levels, backtrace, flush policies, global setup

Suggested reading paths

Quick reference

the 90% of the API
#include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"

spdlog::info("plain message");
spdlog::warn("formatted: {} of {}", done, total); // fmt syntax

auto file = spdlog::rotating_logger_mt(
"app", "logs/app.log", 1024 * 1024 * 5, 3); // 5 MB x 3 files
file->set_level(spdlog::level::debug);
file->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v");
file->flush_on(spdlog::level::err);

spdlog::set_default_logger(file); // spdlog::info now goes here
spdlog::shutdown(); // at exit
TaskCall
Log at a levelspdlog::trace/debug/info/warn/error/critical(...)
Named console loggerspdlog::stdout_color_mt("name")
Named file loggerspdlog::basic_logger_mt("name", "path.log")
Fetch by namespdlog::get("name")
Runtime levellogger->set_level(spdlog::level::debug)
Compile-time level-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG
Patternlogger->set_pattern("%+")
Flushlogger->flush() / flush_on(level)