Class: CMDx::LogFormatters::JSON

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

Overview

‘Logger` formatter that emits one JSON object per line with `severity`, ISO8601 `timestamp`, `progname`, `pid`, and `message` (rendered via `#to_h` when available — Result instances serialize themselves).

Instance Method Summary collapse

Instance Method Details

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

Returns JSON line terminated by ‘“n”`.

Parameters:

  • severity (String)

    Logger severity name

  • time (Time)
  • progname (String, nil)
  • message (Object)

    anything ‘Logger` was handed

Returns:

  • (String)

    JSON line terminated by ‘“n”`



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cmdx/log_formatters/json.rb', line 15

def call(severity, time, progname, message)
  hash = {
    severity:,
    timestamp: time.utc.iso8601(6),
    progname:,
    pid: Process.pid,
    message: message.respond_to?(:to_h) ? message.to_h : message
  }

  ::JSON.dump(hash) << "\n"
end