spdlog

Ultra-fast logging library for C++. Header-only with no dependencies, provides rich formatting features using excellent fmt library. Supports multi-threading, rotating/daily log files, color console output, and backtrace support.

logging libraryC++fastasynchronousmultithreadedheader-onlyformatterssinks

Logging Library

spdlog

Overview

spdlog is a fast C++ logging library designed for high performance. Its header-only design enables easy integration, and it provides asynchronous logging, multithreading support, and rich sink (output destination) options. With powerful formatting capabilities based on the fmt library and performance-focused design, it's suitable for production applications.

Details

spdlog was developed by Gabriel Ime in 2014 as a C++ logging library emphasizing simple integration and high performance. The header-only approach eliminates the need for separate building, making project integration extremely straightforward.

Technical Features

  • Fast Asynchronous Logging: High-speed processing with background thread pools
  • Multi-Sink Support: Console, file, daily rotation, custom sinks, etc.
  • fmt Integration: Type-safe output with modern formatting syntax
  • Thread-Safe: Safe concurrent use in multithreaded environments
  • Customizable: Extensible formatters, converters, and custom flags
  • C++11 Compatible: Leveraging modern C++ features

Architecture

  • Logger: Central control and sink management
  • Sink: Output destination abstraction (file, console, network, etc.)
  • Formatter: Log message formatting
  • Thread Pool: Worker threads for asynchronous logging

Pros and Cons

Pros

  • Excellent Performance: One of the fastest C++ logging libraries
  • Header-Only: Easy integration without build configuration
  • Rich Sink Options: Support for various output destinations
  • Type-Safe Formatting: Modern formatting with fmt library
  • Flexible Configuration: Configurable via code or configuration files
  • Multi-Platform: Windows, Linux, macOS support
  • Memory Efficient: Optimized memory usage

Cons

  • Header Size: Large header files may increase compilation time
  • fmt Library Dependency: External dependency (bundled version available)
  • Learning Curve: Advanced features require understanding of configuration
  • Debug Information: Optimization may limit detailed debug information

Reference Links

Usage Examples

Basic Usage

#include "spdlog/spdlog.h"

int main() {
    spdlog::info("Welcome to spdlog!");
    spdlog::error("Error message: {}", 404);
    
    spdlog::warn("Easy padding in numbers like {:08d}", 12);
    spdlog::critical("Support for int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
    spdlog::info("Support for floats {:03.2f}", 1.23456);
    spdlog::info("Positional args are {1} {0}..", "too", "supported");
    
    // Set global log level
    spdlog::set_level(spdlog::level::debug);
    spdlog::debug("This message should be displayed..");
    
    // Change log pattern
    spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");
    
    return 0;
}

File Logging

#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"

// Basic file logging
void basic_file_logging() {
    try {
        auto logger = spdlog::basic_logger_mt("basic_logger", "logs/basic-log.txt");
        logger->info("Basic file log message");
    }
    catch (const spdlog::spdlog_ex &ex) {
        std::cout << "Log init failed: " << ex.what() << std::endl;
    }
}

// Rotating file logging
void rotating_file_logging() {
    // Create a file rotating logger with 5 MB size max and 3 rotated files
    auto max_size = 1048576 * 5;
    auto max_files = 3;
    auto logger = spdlog::rotating_logger_mt("rotating_logger", "logs/rotating.txt", max_size, max_files);
    
    for (int i = 0; i < 10000; ++i) {
        logger->info("Rotating test message {}", i);
    }
}

// Daily file logging
void daily_logging() {
    // Create a daily logger - a new file is created every day at 2:30 am
    auto logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
    logger->info("Daily log message");
}

Multi-Sink Logger

#include "spdlog/sinks/stdout_color_sinks.h"

void multi_sink_example() {
    // Console sink (warning and above)
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::warn);
    console_sink->set_pattern("[multi_sink] [%^%l%$] %v");

    // File sink (all levels)
    auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/multisink.txt", true);
    file_sink->set_level(spdlog::level::trace);

    // Create multi-sink logger
    spdlog::logger logger("multi_sink", {console_sink, file_sink});
    logger.set_level(spdlog::level::debug);
    
    logger.warn("This should appear in both console and file");
    logger.info("This message should not appear in the console, only in the file");
}

Asynchronous Logging

#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"

void async_logging_example() {
    // Default thread pool settings can be modified *before* creating the async logger
    spdlog::init_thread_pool(8192, 1); // queue with 8k items and 1 backing thread
    
    // Create async file logger
    auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
    
    // Alternatively:
    // auto async_file = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger", "logs/async_log.txt");
    
    for (int i = 0; i < 100; ++i) {
        async_file->info("Async log message {}", i);
    }
}

// Multi-sink async logger
void multi_sink_async_example() {
    spdlog::init_thread_pool(8192, 1);
    
    auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("mylog.txt", 1024*1024*10, 3);
    
    std::vector<spdlog::sink_ptr> sinks {stdout_sink, rotating_sink};
    auto logger = std::make_shared<spdlog::async_logger>("async_multi", sinks.begin(), sinks.end(), 
                                                         spdlog::thread_pool(), spdlog::async_overflow_policy::block);
    spdlog::register_logger(logger);
    
    logger->info("Multi-sink async log");
}

Custom Formatters and Backtrace

#include "spdlog/stopwatch.h"

// Custom formatter flag
class my_formatter_flag : public spdlog::custom_flag_formatter {
public:
    void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override {
        std::string custom_text = "custom-flag";
        dest.append(custom_text.data(), custom_text.data() + custom_text.size());
    }

    std::unique_ptr<custom_flag_formatter> clone() const override {
        return spdlog::details::make_unique<my_formatter_flag>();
    }
};

void custom_formatter_example() {
    auto formatter = std::make_unique<spdlog::pattern_formatter>();
    formatter->add_flag<my_formatter_flag>('*').set_pattern("[%n] [%*] [%^%l%$] %v");
    spdlog::set_formatter(std::move(formatter));
    
    spdlog::info("Message with custom flag");
}

// Backtrace feature
void backtrace_example() {
    // Store the latest 32 messages in a buffer
    spdlog::enable_backtrace(32);
    
    for(int i = 0; i < 100; i++) {
        spdlog::debug("Backtrace message {}", i); // not logged yet..
    }
    
    // e.g. if some error happened:
    spdlog::dump_backtrace(); // log them now! show the last 32 messages
}

// Stopwatch feature
void stopwatch_example() {
    spdlog::stopwatch sw;
    
    // Simulate heavy work
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    spdlog::debug("Elapsed {}", sw);
    spdlog::debug("Elapsed {:.3}", sw);
}

Advanced Features

#include "spdlog/sinks/callback_sink.h"
#include "spdlog/mdc.h"

// Callback sink
void callback_sink_example() {
    auto callback_sink = std::make_shared<spdlog::sinks::callback_sink_mt>([](const spdlog::details::log_msg &msg) {
        // for example you can be notified by sending an email to yourself
        if (msg.level >= spdlog::level::err) {
            // Alert processing
            std::cout << "Alert: Critical error occurred!" << std::endl;
        }
    });
    callback_sink->set_level(spdlog::level::err);

    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    spdlog::logger logger("custom_callback_logger", {console_sink, callback_sink});

    logger.info("some info log");
    logger.error("critical issue"); // will notify you
}

// MDC (Mapped Diagnostic Context)
void mdc_example() {
    spdlog::mdc::put("user_id", "12345");
    spdlog::mdc::put("session_id", "abcdef");
    
    // Use the %& formatter to print mdc data
    spdlog::set_pattern("[%H:%M:%S] [%^%L%$] [%&] %v");
    
    spdlog::info("User action log");
    spdlog::error("Session related error");
}

// Periodic flush
void flush_example() {
    // Periodically flush all *registered* loggers every 3 seconds
    // Warning: only use if all your loggers are thread-safe ("_mt" loggers)
    spdlog::flush_every(std::chrono::seconds(3));
    
    // Manual flush
    spdlog::info("Important message");
    spdlog::get("default")->flush(); // Flush immediately
}

// Error handler
void error_handler_example() {
    spdlog::set_error_handler([](const std::string &msg) { 
        spdlog::get("console")->error("*** LOGGER ERROR ***: {}", msg); 
    });
    
    // Trigger error intentionally
    spdlog::get("console")->info("Invalid message to trigger error {}{}{}{}", 3);
}