How to Change spdlog Date/Time Format: Complete Pattern Guide

To change spdlog's date/time format, call set_pattern() with strftime-style flags (e.g., %Y-%m-%d %H:%M:%S) or instantiate a custom pattern_formatter with your desired timestamp layout and time type.

The gabime/spdlog library handles timestamp formatting through its flexible pattern_formatter class, which interprets pattern strings containing special date/time flags. Understanding how to change spdlog date/time format enables you to output ISO-8601 timestamps, RFC-3339 formats, or completely custom layouts. This guide covers the implementation details in include/spdlog/pattern_formatter.h and practical code examples for customizing your log output.

How spdlog's Pattern Formatter Works

According to the spdlog source code, timestamp formatting is handled by the pattern_formatter class declared in include/spdlog/pattern_formatter.h and implemented in include/spdlog/pattern_formatter-inl.h.

The constructor signature accepts a pattern string and time type:

pattern_formatter(std::string pattern, pattern_time_type time_type = pattern_time_type::local, ...)

When you provide a pattern string, the internal compile_pattern_() method parses it and creates a series of flag_formatter objects for each flag. The actual time extraction happens in get_time_(), which returns a std::tm structure based on the chosen pattern_time_type (local or UTC).

Key Implementation Files

Methods to Change Date/Time Format

Using set_pattern() for Runtime Changes

The simplest way to change spdlog date/time format is calling set_pattern() on any logger instance. This method updates the formatter's pattern string and recompiles the internal flags via compile_pattern_().

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>

int main() {
    auto console = spdlog::stdout_color_mt("console");
    
    // Change to ISO-8601 format
    console->set_pattern("%^[%Y-%m-%dT%H:%M:%SZ]%$ %v");
    console->info("ISO-8601 timestamp");
}

Creating a Custom pattern_formatter

For greater control over time type and formatting, instantiate pattern_formatter directly and assign it to your logger:

// Force UTC with specific pattern
auto formatter = std::make_unique<spdlog::pattern_formatter>(
    "%Y-%m-%dT%H:%M:%SZ", 
    spdlog::pattern_time_type::utc
);
console->set_formatter(std::move(formatter));

Local Time vs UTC

By default, spdlog uses local time (pattern_time_type::local). To use UTC instead, pass spdlog::pattern_time_type::utc as the second parameter to the pattern_formatter constructor. This affects how get_time_() populates the std::tm structure.

Common Date/Time Pattern Examples

The pattern flags follow strftime conventions. Here are practical examples for different timestamp formats:

#include <spdlog/spdlog.h>

int main() {
    auto logger = spdlog::default_logger();
    
    // 1. ISO-8601 with milliseconds (local time)
    logger->set_pattern("%^[%Y-%m-%dT%H:%M:%S.%e]%$ %v");
    logger->info("ISO-8601 with ms");
    
    // 2. European style (day/month/year)
    logger->set_pattern("%^[%d/%m/%Y %H:%M]%$ %v");
    logger->info("European format");
    
    // 3. With timezone offset
    logger->set_pattern("%^[%Y-%m-%d %H:%M:%S %z]%$ %v");
    logger->info("With timezone offset");
    
    // 4. Compact format (no separators)
    logger->set_pattern("[%Y%m%d_%H%M%S] %v");
    logger->info("Compact timestamp");
}

Key date/time flags:

  • %Y - Year with century (e.g., 2024)
  • %m - Month as decimal (01-12)
  • %d - Day of month (01-31)
  • %H - Hour (24-hour clock, 00-23)
  • %M - Minute (00-59)
  • %S - Second (00-59)
  • %e - Millisecond (spdlog-specific extension)
  • %z - Timezone offset (e.g., +0530)

Note: %^ and %$ are color-control flags that enable/disable color output and can be omitted if not using color sinks.

Summary

  • Pattern-based formatting: spdlog uses pattern_formatter in include/spdlog/pattern_formatter.h to parse pattern strings containing strftime-style flags.
  • Runtime changes: Call logger->set_pattern() with your desired format string to change spdlog date/time format dynamically.
  • Time type control: Pass pattern_time_type::utc to the constructor for UTC timestamps, or use the default pattern_time_type::local for local time.
  • Milliseconds support: Use %e to include milliseconds in your timestamp.
  • Implementation detail: The pattern is compiled into flag_formatter objects via compile_pattern_(), with actual time extraction performed by get_time_().

Frequently Asked Questions

How do I change spdlog to use UTC instead of local time?

Instantiate a custom pattern_formatter with spdlog::pattern_time_type::utc as the second argument, then assign it to your logger using set_formatter(). Alternatively, create the logger with a UTC-patterned formatter from initialization.

What is the default date/time pattern in spdlog?

The default pattern is [%Y-%m-%d %H:%M:%S.%e] (year-month-day hour:minute:second.millisecond), which produces timestamps like [2024-01-15 14:30:25.123]. This is defined when the default pattern_formatter is constructed without explicit arguments.

Can I use custom date formats not supported by strftime?

No, spdlog's pattern_formatter only supports the standard strftime-compatible flags plus spdlog-specific extensions like %e (milliseconds) and color flags. For completely custom formatting, you would need to implement a custom formatter class according to the interface in include/spdlog/formatter.h.

Does changing the pattern affect existing log messages?

No, changing the pattern via set_pattern() only affects subsequent log messages. Messages already written to sinks or buffers retain the format that was active at the time they were processed. The change takes effect immediately for the next logging call after set_pattern() returns.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →