spdlog for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

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.

  1. Download the latest release from GitHub.
  2. Unpack the archive.
  3. Copy the include/spdlog folder into your project.
  4. 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 install


Method 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=missing


Method 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

Recommendations