Last active
August 29, 2015 14:02
-
-
Save fernandoalmeida/ef03643fd8d924cc034a to your computer and use it in GitHub Desktop.
Costom Rails Log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/application.rb | |
module MyApp | |
class Application < Rails::Application | |
config.lograge.enabled = true | |
config.lograge.custom_options = lambda { |event| CustomLog::Subscriber.new(event).extract_data } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
include CustomLog::ControllerHelper | |
protected | |
def custom_log_data(payload) | |
payload[:custom_log_data] = { | |
user_id: current_user.try(:id), | |
account_id: current_account.try(:id) | |
} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/custom_log/controller_helper.rb | |
module CustomLog | |
module ControllerHelper | |
extend ActiveSupport::Concern | |
included do | |
def custom_log_data(payload) | |
payload[:custom_log_data] = {} | |
end | |
def validation_messages | |
saved_object.errors.to_json unless saved_object.errors.blank? | |
rescue | |
nil | |
end | |
def saved_object | |
instance_variable_get("@#{controller_name.singularize}") | |
end | |
def append_info_to_payload(payload) | |
super | |
payload[:request] = request | |
payload[:session] = session | |
payload[:validations] = validation_messages | |
custom_log_data(payload) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
method=POST path=/things format=html controller=things action=create status=200 duration=262.28 view=108.93 db=3.84 time=’2014-06-15 13:13:47 -0300' session_id=abcde12345 host=app01 request_id=a1b2c3d4e5 remote_ip=179.209.205.143 user_agent=’Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0' referer=/things/new params={“thing”=>{“name”=>”Teste”} flash={:error=>”Não foi possível gravar os dados”} validations={"name":["já está em uso"]} user_id=987 account_id=123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "lograge" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/log_backtrace.rb | |
# https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/notifications/instrumenter.rb | |
module ActiveSupport | |
module Notifications | |
class Instrumenter | |
attr_reader :id | |
def initialize(notifier) | |
@id = unique_id | |
@notifier = notifier | |
end | |
def instrument(name, payload={}) | |
started = Time.now | |
begin | |
yield | |
rescue Exception => e | |
payload[:exception] = [e.class.name, e.message] | |
payload[:backtrace] = e.backtrace | |
raise e | |
ensure | |
@notifier.publish(name, started, Time.now, @id, payload) | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/custom_log/subscriber.rb | |
module CustomLog | |
class Subscriber | |
attr_reader :event, :payload | |
attr_accessor :data | |
def initialize(event) | |
@event = event | |
@payload = event.payload | |
@data = { :time => %Q('#{event.time}') } | |
end | |
def extract_data | |
session_data | |
request_data | |
params_data | |
validations_data | |
exception_data | |
custom_data | |
data | |
end | |
private | |
def custom_data | |
custom = payload[:custom_log_data] | |
if custom.is_a?(Hash) | |
custom.each_pair { |key, value| data[key] = value } | |
end | |
end | |
def session_data | |
session = payload[:session] | |
if session | |
data.merge!({ :session_id => session[:session_id] }) | |
end | |
end | |
def request_data | |
request = payload[:request] | |
if request | |
data.merge!({ | |
:host => request.host, | |
:request_id => request.uuid, | |
:remote_ip => request.remote_ip, | |
:user_agent => %Q('#{request.user_agent}'), | |
:referer => request.referer, | |
:flash => request.flash.instance_variable_get('@flashes'), | |
}) | |
end | |
end | |
def params_data | |
internal_params = %w(controller action format utf8 authenticity_token commit) | |
params = payload[:params] | |
if params | |
data.merge!({ :params => params.except(*internal_params) }) | |
end | |
end | |
def validations_data | |
validations = payload[:validations] | |
if validations | |
data.merge!({ :validations => validations }) | |
end | |
end | |
def exception_data | |
exception = payload[:exception] | |
backtrace = payload[:backtrace] | |
if exception | |
data.merge!({ | |
:exception_class => exception.first, | |
:exception_message => exception.last, | |
:backtrace => %Q('#{Array(backtrace).to_json}'), | |
}) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment