Scripts2026年5月26日·1 分钟阅读

spdlog — Super Fast C++ Logging Library

A very fast, header-only or compiled, C++ logging library with fmt-style formatting and multiple sink targets.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
spdlog Overview
直接安装命令
npx -y tokrepo@latest install 4a927595-5898-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

spdlog is one of the fastest C++ logging libraries available, designed around the fmt formatting library. It supports synchronous and asynchronous logging to consoles, files, syslog, and custom sinks while keeping API surface minimal and intuitive.

What spdlog Does

  • Logs messages with microsecond timestamps using lock-free queues for async mode
  • Formats output via the {fmt} library with compile-time format checking
  • Rotates log files by size or daily schedule automatically
  • Supports multiple named loggers each with independent sinks and levels
  • Provides colored console output on all major platforms

Architecture Overview

spdlog separates concerns into loggers, sinks, and formatters. A logger holds one or more sinks (console, file, syslog, etc.) and a formatter that renders the log record. In async mode, log records are pushed to a thread-safe ring buffer and flushed by a dedicated worker thread, minimizing latency on the hot path.

Self-Hosting & Configuration

  • Header-only mode: include spdlog headers and compile; no library linking needed
  • Compiled mode: build as a static/shared library for faster compile times in large projects
  • CMake support via find_package(spdlog) or FetchContent
  • Configure log pattern with strftime-like tokens: spdlog::set_pattern("[%H:%M:%S.%e] [%l] %v")
  • Thread-safe by default; optional _mt (multi-thread) and _st (single-thread) sink variants

Key Features

  • Benchmarked at millions of log lines per second in async mode
  • Built-in rotating, daily, and basic file sinks out of the box
  • Backtrace support: store debug messages in ring buffer, dump on error
  • Compile-time log level elimination to remove overhead in release builds
  • MIT licensed with no dependencies beyond {fmt} (bundled)

Comparison with Similar Tools

  • glog (Google) — feature-rich but heavier; uses CHECK macros and signal handlers
  • Boost.Log — very flexible but complex configuration and slow compile times
  • log4cxx — Java Log4j port for C++; XML config, larger binary
  • plog — header-only and simple but fewer sinks and no async support
  • Quill — newer async logger with similar speed; less adoption so far

FAQ

Q: Header-only or compiled — which should I choose? A: Use header-only for small projects. For large codebases, compiled mode reduces recompilation when only log calls change.

Q: Is spdlog thread-safe? A: Yes. The default sinks are multi-thread safe. Use _st variants if you guarantee single-thread access for a small speed boost.

Q: Can I write my own sink? A: Yes. Subclass spdlog::sinks::base_sink<Mutex> and override sink_it_() and flush_().

Q: Does async logging guarantee no message loss? A: By default the async queue blocks when full. You can configure an overflow policy to drop oldest messages instead.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产