Class: CMDx::LogFormatters::KeyValue

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdx/log_formatters/key_value.rb

Overview

Formats log messages as key-value pairs for structured logging

This formatter converts log entries into key-value format with standardized fields including severity, timestamp, program name, process ID, and formatted message. The output is suitable for log parsing tools and human-readable structured logs.

Instance Method Summary collapse

Instance Method Details

#call(severity, time, progname, message) ⇒ String

Formats a log entry as a key-value string

Examples:

Basic usage

logger_formatter.call("INFO", Time.now, "MyApp", "User logged in")
# => "severity=INFO timestamp=2024-01-15T10:30:45.123456Z progname=MyApp pid=12345 message=User logged in\n"

Parameters:

  • severity (String)

    The log level (e.g., “INFO”, “ERROR”, “DEBUG”)

  • time (Time)

    The timestamp when the log entry was created

  • progname (String, nil)

    The program name or identifier

  • message (Object)

    The log message content

Returns:

  • (String)

    A key-value formatted log entry with a trailing newline



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cmdx/log_formatters/key_value.rb', line 26

def call(severity, time, progname, message)
  hash = {
    severity:,
    timestamp: time.utc.iso8601(6),
    progname:,
    pid: Process.pid,
    message: Utils::Format.to_log(message)
  }

  Utils::Format.to_str(hash) << "\n"
end