Class: Logsy::JsonFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/logsy/json_formatter.rb

Overview

Logger formatter that emits one JSON object per log call.

Each line includes:

- ts:    UTC ISO-8601 timestamp with millis
- level: severity ('INFO', 'ERROR', ...)
- msg:   the log message (when message is a String or Exception)
- file/line: source location of the call (when include_caller_location)
- any non-nil attributes from the configured Logsy.context

When the message is a Hash, its keys are merged directly into the top-level payload (useful for “wide event” emissions like a request summary). Symbols and strings are accepted as keys.

Example output:

{"ts":"2026-05-02T10:00:00.123Z","level":"INFO","msg":"hello",
 "file":"app/controllers/orders_controller.rb","line":34,
 "request_id":"abc-123","user_id":"u-1"}

Instance Method Summary collapse

Instance Method Details

#call(severity, time, _progname, message) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/logsy/json_formatter.rb', line 26

def call(severity, time, _progname, message)
  payload = {
    ts: time.utc.iso8601(3),
    level: severity
  }

  add_caller_location!(payload) if Logsy.configuration.include_caller_location
  merge_context!(payload)
  merge_message!(payload, message)

  "#{JSON.dump(payload)}\n"
rescue StandardError => e
  fallback_line(severity, time, e)
end