spdlog
Very fast C++ logging, header-only, with synchronous and asynchronous modes.
What it is
spdlog is a fast, header-only C++ logging library. It provides synchronous and asynchronous logging capabilities with support for multiple sinks, custom formatting, and high-performance logging suitable for real-time applications.
spdlog allows logging messages with different severity levels (trace, debug, info, warn, error, critical) to console, files, or custom sinks. It supports formatting using the fmt library and provides both thread-safe and asynchronous logging.
- Licence
- MIT
- Built on
- fmt
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- You need structured, level-based logging with negligible overhead
- Rotating files, console colour and custom sinks without configuration files
Look elsewhere when
- A large enterprise setup requiring dynamic runtime reconfiguration across many components
Installation
sudo apt install libspdlog-devGetting started
The smallest useful thing you can do with it, and what each part means.
#include <spdlog/spdlog.h>
int main() {
spdlog::info("Hello, {}!", "world");
spdlog::warn("This is a warning");
spdlog::error("This is an error");
return 0;
}#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
int main() {
auto file_logger = spdlog::basic_logger_mt("file_logger", "logs.txt");
file_logger->info("File logger initialized");
return 0;
}Advanced usage
Where the library earns its place over a simpler alternative.
#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
int main() {
spdlog::init_thread_pool(8192, 1);
auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file", "async_logs.txt");
async_file->info("Async logging example");
return 0;
}#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
int main() {
auto console = spdlog::stdout_color_mt("console");
console->set_pattern("[%Y-%m-%d %H:%M:%S] [%^%l%$] %v");
console->info("Custom formatted log message");
return 0;
}#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
int main() {
auto rotating_logger = spdlog::rotating_logger_mt("rotating", "rotating_logs.txt", 1024*1024*5, 3);
rotating_logger->info("This is a rotating log example");
return 0;
}#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
int main() {
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("multi_sink.txt"));
auto logger = std::make_shared<spdlog::logger>("multi_logger", begin(sinks), end(sinks));
spdlog::register_logger(logger);
logger->info("Logging to multiple sinks");
return 0;
}Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- spdlog::spdlog_ex
- Thrown when logger creation fails, e.g., due to file access issues. Ensure file paths are valid and accessible.
Best practices
- Use asynchronous logging for high-performance applications to avoid blocking threads.
- Set log levels appropriately for development, testing, and production.
- Rotate log files to manage disk space for long-running applications.
- Use consistent log patterns for readability and structured log analysis.
- Leverage multiple sinks for different logging targets (console, files, network).
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
spdlog was created by Gabi Melman to offer a modern and high-performance logging library for C++ developers. It emphasizes simplicity, speed, and flexibility, becoming popular in projects where efficient logging is critical, including gaming, finance, and network applications.
