// Levels (lowest to highest):// trace < debug < info < warn < error < critical < offspdlog::set_level(spdlog::level::debug); // show debug and abovespdlog::set_level(spdlog::level::warn); // show warn and above onlyspdlog::debug("This shows if level <= debug");spdlog::info("This shows if level <= info");
Named Loggers
#include <spdlog/spdlog.h>auto logger = spdlog::stdout_color_mt("my_logger");logger->info("Message from named logger");logger->set_level(spdlog::level::debug);// Retrieve logger by name anywhere in codeauto same = spdlog::get("my_logger");same->warn("Retrieved logger");
#include <spdlog/sinks/basic_file_sink.h>auto file_logger = spdlog::basic_logger_mt("file_logger", "logs/app.log");file_logger->info("Logged to file");// Truncate on openauto trunc = spdlog::basic_logger_mt("trunc", "logs/app.log", true);
Rotating File Sink
#include <spdlog/sinks/rotating_file_sink.h>// Max 5MB per file, keep 3 filesauto rotating = spdlog::rotating_logger_mt( "rotating", "logs/app.log", 1024 * 1024 * 5, // 5MB 3 // 3 files);rotating->info("This rotates when file hits 5MB");
Daily File Sink
#include <spdlog/sinks/daily_file_sink.h>// New file every day at 02:30auto daily = spdlog::daily_logger_mt("daily", "logs/daily.log", 2, 30);daily->info("Daily log entry");
Multi-Sink Logger
#include <spdlog/sinks/stdout_color_sinks.h>#include <spdlog/sinks/rotating_file_sink.h>#include <spdlog/logger.h>auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>( "logs/multi.log", 1024*1024, 5);spdlog::logger logger("multi", {console_sink, file_sink});logger.info("Goes to both console and file");
// Flush on every error or abovespdlog::flush_on(spdlog::level::err);// Periodic flush every 3 secondsspdlog::flush_every(std::chrono::seconds(3));// Manual flushspdlog::get("my_logger")->flush();