Logging in C++: Overview of the Best Libraries and Solutions

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

Logging for C++: Best Solutions



Logging is one of the most important tools in a developer's arsenal. It helps track program execution, find errors, analyze performance, and collect data for debugging. The C++ language does not have a built-in logging system (unlike Java or Python), but the community and large companies have created many excellent libraries. In this article, we will look at the best solutions for logging in C++: spdlog, Boost.Log, Google glog, and fmtlog. You will learn their features, advantages, and get code examples.



Why is logging needed in C++?

In modern C++ projects, logging solves several tasks:

  • Debugging: recording variable values, call stacks, object states.
  • Monitoring: recording execution time of functions, I/O operations.
  • Auditing: saving the history of user or system actions.
  • Failure diagnostics: recording exceptions and critical errors.

A good logging library should be fast, flexible, support different log levels (debug, info, warn, error, fatal), and be able to output data to a file, console, or remote server.



1. spdlog — Fast and Modern Logger

spdlog is currently the most popular library for logging in C++. It is written in modern C++ (C++11/14/17), is header-only, and has no external dependencies except fmtlib for string formatting. Main advantages:

  • High performance (asynchronous logging).
  • Support for many sinks: file, console, syslog, databases.
  • Flexible formatting configuration.
  • Simple API.

Example of using spdlog:

#include <spdlog/spdlog.h>#include <spdlog/sinks/basic_file_sink.h>

int main() { // Create a logger that writes to the file "logs.txt" auto logger = spdlog::basic_logger_mt("file_logger", "logs.txt"); spdlog::set_default_logger(logger); spdlog::set_level(spdlog::level::debug); // Logging level

spdlog::info("Welcome to spdlog!"); spdlog::debug("Some debug message: {}", 42); spdlog::error("Error occurred: {}", errno);

// Asynchronous logging spdlog::flush_on(spdlog::level::err); return 0;}

spdlog also supports asynchronous mode, which is critical for high-load applications. Configuring asynchronicity is trivial: just call spdlog::init_thread_pool(...).



2. Boost.Log — Powerful but Heavyweight

Boost.Log is part of the Boost libraries. It is the most feature-rich solution, but it requires installing Boost and can slow down compilation. Features:

  • Maximum flexibility: custom filters, formatters, sinks.
  • Support for multithreading and thread safety.
  • Ability to write logs to multiple destinations simultaneously (file + console + network).
  • Support for log rotation by size, date, or time.

Example of Boost.Log:

#include <boost/log/trivial.hpp>#include <boost/log/expressions.hpp>#include <boost/log/utility/setup/file.hpp>

namespace logging = boost::log;

int main() { // Configure output to a file logging::add_file_log("sample.log"); logging::core::get()->set_filter( logging::trivial::severity >= logging::trivial::info );

BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; BOOST_LOG_TRIVIAL(debug) << "A debug severity message"; BOOST_LOG_TRIVIAL(error) << "An error severity message"; return 0;}

Boost.Log is a cho

Recommendations