mirror of
https://github.com/samsonjs/grape_logging.git
synced 2026-04-16 12:35:47 +00:00
57 lines
No EOL
1.3 KiB
Ruby
57 lines
No EOL
1.3 KiB
Ruby
require 'grape/middleware/base'
|
|
|
|
module GrapeLogging
|
|
module Middleware
|
|
class RequestLogger < Grape::Middleware::Base
|
|
def before
|
|
start_time
|
|
|
|
@db_duration = 0
|
|
@subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
|
|
event = ActiveSupport::Notifications::Event.new(*args)
|
|
@db_duration += event.duration
|
|
end if defined?(ActiveRecord)
|
|
end
|
|
|
|
def after
|
|
stop_time
|
|
logger.info parameters
|
|
ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription
|
|
nil
|
|
end
|
|
|
|
protected
|
|
def parameters
|
|
{
|
|
path: request.path,
|
|
params: request.params.to_hash,
|
|
method: request.request_method,
|
|
total: total_runtime,
|
|
db: @db_duration.round(2),
|
|
status: response.status
|
|
}
|
|
end
|
|
|
|
private
|
|
def logger
|
|
@logger ||= @options[:logger] || Logger.new(STDOUT)
|
|
end
|
|
|
|
def request
|
|
@request ||= ::Rack::Request.new(env)
|
|
end
|
|
|
|
def total_runtime
|
|
((stop_time - start_time) * 1000).round(2)
|
|
end
|
|
|
|
def start_time
|
|
@start_time ||= Time.now
|
|
end
|
|
|
|
def stop_time
|
|
@stop_time ||= Time.now
|
|
end
|
|
end
|
|
end
|
|
end |