Class: SimpleJsonLogFormatter
- Inherits:
-
Object
- Object
- SimpleJsonLogFormatter
- 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
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #call(severity, time, progname, msg) ⇒ Object
-
#initialize(opts = {}) ⇒ SimpleJsonLogFormatter
constructor
A new instance of SimpleJsonLogFormatter.
Constructor Details
#initialize(opts = {}) ⇒ SimpleJsonLogFormatter
Returns a new instance of SimpleJsonLogFormatter.
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
#opts ⇒ Object (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]] = (msg) "#{event.to_json}\n" end |