spdlog

Language: CPP

CLi/Utils

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.

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.

Installation

linux: sudo apt install libspdlog-dev
mac: brew install spdlog
windows: Download from https://github.com/gabime/spdlog or use vcpkg: vcpkg install spdlog

Usage

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.

Basic console logging

#include <spdlog/spdlog.h>

int main() {
    spdlog::info("Hello, {}!", "world");
    spdlog::warn("This is a warning");
    spdlog::error("This is an error");
    return 0;
}

Logs messages to the console with different severity levels using fmt-style formatting.

Logging to a file

#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;
}

Creates a file logger that writes log messages to `logs.txt`.

Asynchronous logging

#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;
}

Sets up asynchronous logging to improve performance for high-frequency log messages.

Custom formatting

#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;
}

Demonstrates setting a custom log pattern with timestamps, log levels, and message content.

Rotating file logger

#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;
}

Creates a rotating file logger that maintains multiple log files of a specified maximum size.

Logging with multiple sinks

#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;
}

Logs messages simultaneously to console and a file using multiple sinks.

Error Handling

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).