9x0h6a
Last Updated: November 05, 2018
·
4.831K
· rafaelcgo

Better Rails Logging (user_id, remote_ip) with Lograge on Heroku

"Lograge is an attempt to bring sanity to Rails' noisy and unusable, unparsable and, in the context of running multiple processes and servers, unreadable default logging output. Rails' default approach to log everything is great during development, it's terrible when running it in production. It pretty much renders Rails logs useless to me."

LogRage gem: https://github.com/roidrage/lograge

production.rb

# Logging with LogRage
config.lograge.enabled = true
config.lograge.custom_options = lambda do |event|
  {remote_ip: event.payload[:remote_ip], user_id: event.payload[:user_id], params: event.payload[:params].except('controller', 'action', 'format', 'utf8')}
end

application_controller.rb

# Lograge method for adding extra info to Logging
def append_info_to_payload(payload)
  super
  payload[:remote_ip] = request.remote_ip
  payload[:user_id] = if current_user.present?
    current_user.try(:id)
  elsif current_admin.present?
    "admin(#{current_admin.try(:id)})"
  else
    :guest
  end
end