Logging for C++: Best Solutions
Logging is a critically important component of any serious C++ project. It helps track program execution, find errors, analyze performance, and audit user actions. Unlike Python, where the built-in logging module is the de facto standard, C++ has no single built-in solution. However, the community has created several powerful and flexible libraries. In this article, we will look at the best of them: spdlog, Google glog, Boost.Log, and Easylogging++.
Criteria for Choosing a Logging Library
Before moving on to the review, it is important to understand what to look for when choosing:
- Performance: minimal impact on application speed.
- Flexibility: ability to configure formats, logging levels (DEBUG, INFO, WARN, ERROR) and outputs (file, console, syslog).
- Ease of Integration: header-only libraries are preferable for small projects.
- Multithreading Support: thread safety and asynchronous writing.
- License: compatibility with commercial projects (MIT, Apache, Boost).
1. spdlog — The Fastest and Most Modern
spdlog is a header-only library (optionally compilable), written in C++11/14/17. It is rightfully considered the most popular due to its lightning speed and minimal memory consumption. spdlog supports asynchronous logging, which is especially important for high-load systems.
Key Features:
- Thread safety out of the box.
- Many sinks: file, console, syslog, rotatable files, daily files.
- Support for custom formatters (based on fmtlib).
- Asynchronous mode with a message queue.
- Conditional logging.
Usage Example:
#include "spdlog/spdlog.h"#include "spdlog/sinks/basic_file_sink.h"
int main() { // Create a logger that writes to a file auto file_logger = spdlog::basic_logger_mt("file_logger", "logs/app.log"); spdlog::set_default_logger(file_logger); spdlog::set_level(spdlog::level::debug);
// Log messages spdlog::info("Application started"); spdlog::warn("Suspicious behavior detected: {}", 42); spdlog::error("Critical error: {}", errno);
// Asynchronous logging spdlog::flush_every(std::chrono::seconds(3)); return 0;}When to choose: for modern projects where speed is critical. Ideal for games, real-time servers, and embedded systems.
2. Google glog — A Time-Tested Standard
Google glog (Google Logging Library) is a library developed by Google for internal use. It is simple, reliable, and used in thousands of projects worldwide. glog does not try to be the fastest, but provides all the necessary functions for logging in production.
Features:
- Automatic log rotation by size.
- Logging levels: INFO, WARNING, ERROR, FATAL (with abort).
- Conditional logging (LOG_IF, LOG_EVERY_N).
- Call stack support on fatal errors.
- Minimal dependency — only pthreads.
Usage Example:
#include <glog/logging.h>
int main(int argc, char* argv[]) { google::InitGoogleLogging(argv[0]); FLAGS_log_dir = "./logs"; FLAGS_minloglevel = google::INFO;
LOG(INFO) << "Server started on port " << 8080; LOG(WARNING) < Also in library
Prometheus Metrics in Rust: prometheus and metrics-exporter-prometheus Libraries
Working with Images in Go: Resize and Crop with the imaging Library
Data Validation in Kotlin: Libraries, Approaches, and Code Examples
JSON Processing in C# (.NET): Complete Guide with Examples
Humanizer for C# (.NET): Complete Guide with Examples