Class: CMDx::LogFormatters::JSON
- Inherits:
-
Object
- Object
- CMDx::LogFormatters::JSON
- 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). Falls back to `message.inspect` if the original `JSON.dump` raises (e.g. for non-JSON-encodable or cyclic structures) so logging never crashes the caller.
Instance Method Summary collapse
-
#call(severity, time, progname, message) ⇒ String
JSON line terminated by ‘“n”`.
Instance Method Details
#call(severity, time, progname, message) ⇒ String
Returns JSON line terminated by ‘“n”`.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cmdx/log_formatters/json.rb', line 18 def call(severity, time, progname, ) hash = { severity:, timestamp: time.utc.iso8601(6), progname:, pid: Process.pid, message: .respond_to?(:to_h) ? .to_h : } ::JSON.dump(hash) << "\n" rescue StandardError => e hash[:message] = .inspect hash[:logerr] = Util.to_error_s(e) ::JSON.dump(hash) << "\n" end |