Introduction
spdlog is one of the fastest C++ logging libraries available, designed around the fmt formatting library. It supports synchronous and asynchronous logging to consoles, files, syslog, and custom sinks while keeping API surface minimal and intuitive.
What spdlog Does
- Logs messages with microsecond timestamps using lock-free queues for async mode
- Formats output via the {fmt} library with compile-time format checking
- Rotates log files by size or daily schedule automatically
- Supports multiple named loggers each with independent sinks and levels
- Provides colored console output on all major platforms
Architecture Overview
spdlog separates concerns into loggers, sinks, and formatters. A logger holds one or more sinks (console, file, syslog, etc.) and a formatter that renders the log record. In async mode, log records are pushed to a thread-safe ring buffer and flushed by a dedicated worker thread, minimizing latency on the hot path.
Self-Hosting & Configuration
- Header-only mode: include spdlog headers and compile; no library linking needed
- Compiled mode: build as a static/shared library for faster compile times in large projects
- CMake support via
find_package(spdlog)orFetchContent - Configure log pattern with strftime-like tokens:
spdlog::set_pattern("[%H:%M:%S.%e] [%l] %v") - Thread-safe by default; optional
_mt(multi-thread) and_st(single-thread) sink variants
Key Features
- Benchmarked at millions of log lines per second in async mode
- Built-in rotating, daily, and basic file sinks out of the box
- Backtrace support: store debug messages in ring buffer, dump on error
- Compile-time log level elimination to remove overhead in release builds
- MIT licensed with no dependencies beyond {fmt} (bundled)
Comparison with Similar Tools
- glog (Google) — feature-rich but heavier; uses CHECK macros and signal handlers
- Boost.Log — very flexible but complex configuration and slow compile times
- log4cxx — Java Log4j port for C++; XML config, larger binary
- plog — header-only and simple but fewer sinks and no async support
- Quill — newer async logger with similar speed; less adoption so far
FAQ
Q: Header-only or compiled — which should I choose? A: Use header-only for small projects. For large codebases, compiled mode reduces recompilation when only log calls change.
Q: Is spdlog thread-safe?
A: Yes. The default sinks are multi-thread safe. Use _st variants if you guarantee single-thread access for a small speed boost.
Q: Can I write my own sink?
A: Yes. Subclass spdlog::sinks::base_sink<Mutex> and override sink_it_() and flush_().
Q: Does async logging guarantee no message loss? A: By default the async queue blocks when full. You can configure an overflow policy to drop oldest messages instead.