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.
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
| Section | What it covers | |
|---|---|---|
| Overview | What it is, installing it, the sink architecture, how it compares to glog/Boost.Log | |
| Basics | The default logger, log levels, fmt-style formatting | |
| Loggers & Registry | Creating loggers, the global registry, lifetime, multi-sink loggers | |
| Sinks | Console, file, rotating, daily, syslog, and writing your own | |
| Formatting & Patterns | Pattern flags, custom flag formatters, source location | |
| Async Logging | The thread pool, overflow policies, when async pays off | |
| Performance & Configuration | Compile-time levels, backtrace, flush policies, global setup |
Suggested reading paths
- Just want logs in my app: Quick start → Log levels → Console sinks.
- Setting up logging for a real service: Creating loggers → Multi-sink loggers → Rotating and daily sinks → Flush policies.
- Logging is showing up in the profile: Compile-time log level → Async vs sync → Overflow policies.
Quick reference
#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
| Task | Call |
|---|---|
| Log at a level | spdlog::trace/debug/info/warn/error/critical(...) |
| Named console logger | spdlog::stdout_color_mt("name") |
| Named file logger | spdlog::basic_logger_mt("name", "path.log") |
| Fetch by name | spdlog::get("name") |
| Runtime level | logger->set_level(spdlog::level::debug) |
| Compile-time level | -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG |
| Pattern | logger->set_pattern("%+") |
| Flush | logger->flush() / flush_on(level) |