module Carbonyte::Initializers::Lograge

This initializer setups lograge and automatically logs exceptions

Constants

LOG_TYPE

Log type, this allows to distinguish these logs from others

PARAMS_EXCEPTIONS

Items to be removed from `params` hash

Public Instance Methods

add_rescued_exception(opts, exc) click to toggle source

Adds the rescued exception (if any) to the Lograge event

# File lib/carbonyte/initializers/lograge.rb, line 48
def add_rescued_exception(opts, exc)
  return unless exc

  opts[:rescued_exception] = {
    name: exc.class.name,
    message: exc.message,
    backtrace: %('#{Array(exc.backtrace.first(10)).join("\n\t")}')
  }
end
custom_options(event) click to toggle source

Adds custom options to the Lograge event

# File lib/carbonyte/initializers/lograge.rb, line 35
def custom_options(event)
  opts = {
    type: LOG_TYPE,
    params: event.payload[:params].except(*PARAMS_EXCEPTIONS),
    correlation_id: RequestStore.store[:correlation_id],
    environment: Rails.env,
    pid: ::Process.pid
  }
  add_rescued_exception(opts, RequestStore.store[:rescued_exception])
  opts
end
custom_payload(controller) click to toggle source

Adds custom payload to the Lograge event

# File lib/carbonyte/initializers/lograge.rb, line 59
def custom_payload(controller)
  payload = {
    headers: parse_headers(controller.request.headers),
    remote_ip: controller.remote_ip
  }
  payload[:user_id] = controller.current_user&.id if controller.respond_to?(:current_user)
  payload
end
parse_headers(headers) click to toggle source

Parses headers returning only those starting with “HTTP”, but excluding cookies

# File lib/carbonyte/initializers/lograge.rb, line 69
def parse_headers(headers)
  headers.to_h.select { |k, _v| k.start_with?('HTTP') and k != 'HTTP_COOKIE' }
end