Class: JsonFormatter

Inherits:
Logger::Formatter
  • Object
show all
Includes:
ActiveSupport::TaggedLogging::Formatter
Defined in:
lib/rails_stdout_json_logging/json_formatter.rb

Overview

Custom Json Logger Formatter

Instance Method Summary collapse

Instance Method Details

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



5
6
7
8
9
10
# File 'lib/rails_stdout_json_logging/json_formatter.rb', line 5

def call(severity, time, _progname, msg)
  return if msg.blank?
  log_line = { '@timestamp': time.utc.iso8601(6),
               level: severity.to_s }.merge(format(msg)).compact
  %(#{log_line.to_json}\n)
end

#format(msg) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails_stdout_json_logging/json_formatter.rb', line 12

def format(msg)
  case msg
  when ::String
    { message: msg }
  when ::Hash
    msg
  when ::Exception
    format_exception(msg)
  else
    { message: msg.inspect }
  end
end

#format_exception(exception) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rails_stdout_json_logging/json_formatter.rb', line 25

def format_exception(exception)
  {
    exception: exception.class,
    message: exception.message,
    backtrace: exception.backtrace
  }
end