What is spdlog and why is it needed?
spdlog is an ultra-fast, header-only logging library for C++, distributed under the MIT license. Its main feature is incredible performance, achieved through asynchronous writing, minimal allocations, and the use of modern C++11/14/17 features. In benchmarks, spdlog is often 5-10 times faster than libraries like glog or Boost.Log.
Why use spdlog? In any non-trivial C++ application, logging is not a luxury but a necessity. It helps:
- Debug errors in a production environment where there is no debugger.
- Track program execution flow (tracing).
- Analyze performance (measuring execution time of code sections).
- Conduct security audits (recording login attempts, data changes).
spdlog provides a flexible system: you can write logs to the console, to files (including rotation by size or time), to the system log (syslog), to a database (via ODBC), or even send them over the network. Asynchronous mode is supported, which prevents blocking the main application thread when writing logs.
Installing spdlog
Unlike Python, C++ does not have a single package manager, but there are several standard ways to install spdlog.
Method 1: Header-only (recommended for beginners)
The simplest way is to download the header files and simply include them in your project. No compilation is required.
- Download the latest release from GitHub.
- Unpack the archive.
- Copy the
include/spdlogfolder into your project. - In your code, use
#include "spdlog/spdlog.h".
Method 2: Via the vcpkg package manager
vcpkg is a popular C++ package manager from Microsoft.
# Installationgit clone https://github.com/Microsoft/vcpkg.gitcd vcpkg./bootstrap-vcpkg.sh # or bootstrap-vcpkg.bat on Windows./vcpkg install spdlog
# Integration with your build system./vcpkg integrate installMethod 3: Via Conan
Conan is another popular package manager.
# In the conanfile.txt file[requires]spdlog/1.12.0
[generators]cmake_find_package
# Installationconan install . --build=missingMethod 4: CMake FetchContent (modern approach)
If you use CMake, you can pull the library directly from the repository:
include(FetchContent)FetchContent_Declare( spdlog GIT_REPOSITORY https://github.com/gabime/spdlog.git GIT_TAG v1.12.0)FetchContent_MakeAvailable(spdlog)
# In your targettarget_link_libraries(my_app PRIVATE spdlog::spdlog)Quick Start with Code Example
Let's write a simple program that outputs logs of different levels to the console and to a file.
#include "spdlog/spdlog.h"#include "spdlog/sinks/basic_file_sink.h"#include <memory>
int main() { // 1. Create a logger for the console (colorful by default) auto console = spdlog::stdout_color_mt("console");
// 2. Create a logger for the file auto file_logger = spdlog::basic_logger_mt("file_logger", "logs/app.log");
// 3. Set the logging level (default is trace) spdlog::set_level(spdlog::level::trace);
// 4. Write messages of different levels console->trace("This is a trace message (most detailed level)"); console->debug("Debug: variable x value = {}", 42); console->info("Program started successfully"); console->warn("Warning: memory is almost full"); console->error("Error: failed to open file"); console->critical("Crit Also in library
gRPC for Go (Golang): Libraries, Examples, and Practical Guide
Firebase SDK for Swift: Complete Guide with Examples
wiremock-rs for Rust: Complete Guide with Examples
Testing in C++: Overview of Frameworks and Tools (Google Test, Catch2, doctest)