Class: SimpleJsonLogFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_json_log_formatter.rb,
lib/simple_json_log_formatter/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SimpleJsonLogFormatter

Returns a new instance of SimpleJsonLogFormatter.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • time_key (String) — default: default: time
  • severity_key (String) — default: default: severity
  • progname_key (String) — default: default: progname
  • message_key (String) — default: default: message
  • datetime_format (String) — default: default: %FT%T%:z


15
16
17
18
19
20
21
22
# File 'lib/simple_json_log_formatter.rb', line 15

def initialize(opts={})
  @opts = opts.map {|k, v| [k.to_sym, v] }.to_h
  @opts[:time_key] = :time unless @opts.has_key?(:time_key)
  @opts[:severity_key] = :severity unless @opts.has_key?(:severity_key)
  @opts[:progname_key] = :progname unless @opts.has_key?(:progname_key)
  @opts[:message_key] ||= :message
  @opts[:datetime_format] = "%FT%T%:z" if !@opts.has_key?(:datetime_format) || @opts[:datetime_format].nil?
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



7
8
9
# File 'lib/simple_json_log_formatter.rb', line 7

def opts
  @opts
end

Instance Method Details

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



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

def call(severity, time, progname, msg)
  event = {}
  event[@opts[:time_key]] = time.strftime(@opts[:datetime_format]) if @opts[:time_key]
  event[@opts[:severity_key]] = severity if @opts[:severity_key]
  event[@opts[:progname_key]] = progname if @opts[:progname_key]
  event[@opts[:message_key]] = format_message(msg)
  "#{event.to_json}\n"
end