class Ezlog::LoggingLayout

Public Class Methods

new(context = {}, options = {}) click to toggle source
# File lib/ezlog/logging_layout.rb, line 6
def initialize(context = {}, options = {})
  @initial_context = context
  @level_formatter = options.fetch(:level_formatter, ->(numeric_level) { ::Logging::LNAMES[numeric_level] })
end

Public Instance Methods

format(event) click to toggle source
# File lib/ezlog/logging_layout.rb, line 11
def format(event)
  log_entry = basic_information_for event
  add_initial_context_to log_entry
  add_logging_context_to log_entry
  add_event_information_to log_entry, event
  ::Oj.dump(log_entry, mode: :json) + "\n"
end

Private Instance Methods

add_event_information_to(log_entry, event) click to toggle source
# File lib/ezlog/logging_layout.rb, line 39
def add_event_information_to(log_entry, event)
  log_entry.merge! hash_from(event.data)
end
add_initial_context_to(log_entry) click to toggle source
# File lib/ezlog/logging_layout.rb, line 31
def add_initial_context_to(log_entry)
  log_entry.merge! @initial_context
end
add_logging_context_to(log_entry) click to toggle source
# File lib/ezlog/logging_layout.rb, line 35
def add_logging_context_to(log_entry)
  log_entry.merge! ::Logging.mdc.context
end
basic_information_for(event) click to toggle source
# File lib/ezlog/logging_layout.rb, line 21
def basic_information_for(event)
  {
    'logger' => event.logger,
    'timestamp' => event.time.iso8601(3),
    'level' => @level_formatter.call(event.level),
    'hostname' => Socket.gethostname,
    'pid' => Process.pid
  }
end
exception_message_by(exception) click to toggle source
# File lib/ezlog/logging_layout.rb, line 54
def exception_message_by(exception)
  {
    'message' => exception.message,
    'error' => {
      'class' => exception.class.name,
      'message' => exception.message,
      'backtrace' => exception.backtrace&.first(20)
    }
  }
end
hash_from(obj) click to toggle source
# File lib/ezlog/logging_layout.rb, line 43
def hash_from(obj)
  case obj
  when Exception
    exception_message_by(obj)
  when Hash
    obj
  else
    { 'message' => obj }
  end
end