class Logging::Layouts::Json

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method
# File lib/logging/layouts/json.rb, line 8
def initialize(opts = {})
  opts[:style] = 'json'
  super(opts)
end

Public Instance Methods

format(event) click to toggle source
# File lib/logging/layouts/json.rb, line 13
def format(event)
  result = {
    'logger' => event.logger,
    'timestamp' => iso8601_format(event.time),
    'level' => ::Logging::LNAMES[event.level]
  }
  result.merge!(log_hash(event))
  MultiJson.encode(result) << "\n"
end

Private Instance Methods

create_format_method() click to toggle source

original `create_format_method` overrides `format` method with meta-programming

# File lib/logging/layouts/json.rb, line 60
def create_format_method
end
format_exception(exception) click to toggle source
# File lib/logging/layouts/json.rb, line 49
def format_exception(exception)
  result = {
    'exception' => exception.class.name,
    'message' => format_obj(exception.message)
  }
  result['cause'] = format_exception(exception.cause) if exception.cause
  result['backtrace'] = exception.backtrace if @backtrace && exception.backtrace
  result
end
format_hash(obj) click to toggle source
# File lib/logging/layouts/json.rb, line 43
def format_hash(obj)
  obj.each_with_object({}) do |(key, value), result|
    result[key] = recursive_format(value)
  end
end
log_hash(event) click to toggle source
# File lib/logging/layouts/json.rb, line 25
def log_hash(event)
  formatted = recursive_format(event.data)
  formatted.is_a?(Hash) ? formatted : {'message' => formatted}
end
recursive_format(obj) click to toggle source
# File lib/logging/layouts/json.rb, line 30
def recursive_format(obj)
  case obj
  when Hash
    format_hash(obj)
  when Exception
    format_exception(obj)
  when Time
    iso8601_format(obj)
  else
    format_obj(obj)
  end
end