Class: RabbitmqClient::JsonFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbitmq_client/json_formatter.rb

Overview

Formatter for json log messages

Constant Summary collapse

TIME_FORMAT =
'%Y-%m-%dT%H:%M:%S.%6N '

Instance Method Summary collapse

Constructor Details

#initializeJsonFormatter

Returns a new instance of JsonFormatter.



9
10
11
12
# File 'lib/rabbitmq_client/json_formatter.rb', line 9

def initialize
  @json = {}
  @msg = ''
end

Instance Method Details

#build_new_json(msg) ⇒ Object



23
24
25
26
27
# File 'lib/rabbitmq_client/json_formatter.rb', line 23

def build_new_json(msg)
  @msg  = msg
  @json = @msg.is_a?(Hash) ? @msg : { message: @msg.strip }
  @json.merge(TagsFilter.tags || {})
end

#call(severity, timestamp, progname, msg) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/rabbitmq_client/json_formatter.rb', line 14

def call(severity, timestamp, progname, msg)
  @json = build_new_json(msg)
  @json = @json.merge(progname: progname.to_s, level: severity,
                      timestamp: timestamp.strftime(TIME_FORMAT))
  @json = @json.reject { |_key, value| value.to_s.empty? }

  @json.to_json + "\n"
end