Class: Huyegger::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/huyegger/formatter.rb

Constant Summary collapse

SEVERITY_STR =
{
  ::Logger::DEBUG => "DEBUG",
  ::Logger::INFO => "INFO",
  ::Logger::WARN => "WARN",
  ::Logger::ERROR => "ERROR",
  ::Logger::FATAL => "FATAL",
  ::Logger::UNKNOWN => "UNKNOWN"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_formatter) ⇒ Formatter

Returns a new instance of Formatter.



18
19
20
# File 'lib/huyegger/formatter.rb', line 18

def initialize(original_formatter)
  @original_formatter = original_formatter
end

Instance Attribute Details

#original_formatterObject (readonly)

Returns the value of attribute original_formatter.



16
17
18
# File 'lib/huyegger/formatter.rb', line 16

def original_formatter
  @original_formatter
end

Instance Method Details

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

This method is invoked when a log event occurs



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/huyegger/formatter.rb', line 31

def call(severity, timestamp, progname, msg)
  msg = original_formatter.call(severity, timestamp, progname, msg) if original_formatter && String === msg
  json_message = {}

  add_severity!(json_message, severity)
  json_message.merge!(Huyegger.stringify_keys(__context__))
  add_message!(json_message, msg)
  add_timestamp(json_message)

  "#{Huyegger.json_encoder.call(json_message)}\n"
end

#clear_context!Object



22
23
24
# File 'lib/huyegger/formatter.rb', line 22

def clear_context!
  __context__.clear
end

#context(context) ⇒ Object



26
27
28
# File 'lib/huyegger/formatter.rb', line 26

def context(context)
  __context__.merge!(context) unless context.nil?
end