Class: Hickory::Formatters::JSON

Inherits:
Logger::Formatter
  • Object
show all
Includes:
ActiveSupport::TaggedLogging::Formatter
Defined in:
lib/hickory/formatters/json.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JSON

Returns a new instance of JSON.



11
12
13
14
15
16
17
# File 'lib/hickory/formatters/json.rb', line 11

def initialize(options = {})
  @key_mapping = Hash.new.tap { |mapping|
    mapping[:message]   = options.fetch(:message_key, :message)
    mapping[:severity]  = options.fetch(:severity_key, :level)
    mapping[:timestamp] = options.fetch(:timestamp_key, :timestamp)
  }
end

Instance Method Details

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



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hickory/formatters/json.rb', line 19

def call(severity, time, progname, msg)
  line = msg.tap { |message|
    message[@key_mapping[:message]]   = msg.delete(:message) unless @key_mapping[:message] == :message
    message[@key_mapping[:timestamp]] = time
    message[@key_mapping[:severity]]  = severity
    message[:progname]                = progname unless progname.nil?
    message[:tags]                    = current_tags unless current_tags.empty?
  }

  "#{line.to_json}\n"
end