Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jugyo
Last active November 16, 2023 17:25
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jugyo/300e93d6624375fe4ed8674451df4fe0 to your computer and use it in GitHub Desktop.
Save jugyo/300e93d6624375fe4ed8674451df4fe0 to your computer and use it in GitHub Desktop.
Rack middleware for logging all rails request
# Add below into config/application.rb:
#
# config.middleware.use 'RequestLogger'
#
class RequestLogger
def initialize app
@app = app
end
def call(env)
request = ActionDispatch::Request.new env
started_on = Time.now
begin
status, _, _ = response = @app.call(env)
log(env, status, started_on, Time.now)
rescue Exception => exception
status = determine_status_code_from_exception(exception)
log(env, status, started_on, Time.now, exception)
raise exception
end
response
end
def log(env, status, started_on, ended_on, exception = nil)
url = env['REQUEST_URI']
path = env['PATH_INFO']
user = try_current_user(env)
time_spent = ended_on - started_on
user_agent = env['HTTP_USER_AGENT']
ip = env['action_dispatch.remote_ip'].calculate_ip
request_method = env['REQUEST_METHOD']
http_host = env['HTTP_HOST']
RequestLog.push(
status: status,
url: url,
path: path,
user_id: user.try(:id),
time_spent: time_spent,
user_agent: user_agent,
ip: ip,
request_method: request_method,
http_host: http_host,
error_type: exception&.class&.name,
error_message: exception&.message
)
rescue Exception => exception
Rails.logger.error(exception.message)
end
def determine_status_code_from_exception(exception)
exception_wrapper = ActionDispatch::ExceptionWrapper.new(nil, exception)
exception_wrapper.status_code
rescue
500
end
def try_current_user(env)
controller = env['action_controller.instance']
return unless controller.respond_to?(:current_user, true)
return unless [-1, 0].include?(controller.method(:current_user).arity)
controller.__send__(:current_user)
end
end
@grassiricardo
Copy link

Hello, how could you use this middleware?

@squeedee
Copy link

squeedee commented Apr 6, 2020

config.middleware.use 'RequestLogger'

@akestner
Copy link

What is the RequestLog class?

@rslhdyt
Copy link

rslhdyt commented Apr 20, 2021

@akestner I think that is for ActiveRecord model

@giovapanasiti
Copy link

Does this support multi threading?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment