Skip to content

Instantly share code, notes, and snippets.

@pboling
Forked from dblock/api_logger.rb
Last active Aug 29, 2015
Embed
What would you like to do?
require 'ext/grape_middleware_logger'
module TrumakerAPI
module Middleware
class ApiLogger < Grape::Middleware::Logger
def after
logger.info "[api] Requested#{request_log}" if !request_log.blank?
if Rails.env.development?
response_body = JSON.parse(response.body.first)
if response_body.is_a?(Hash)
logger.debug "[api] RespType: #{response_body['response_type']}" unless response_body['response_type'].blank?
logger.debug "[api] Response: #{response_body['response']}" unless response_body['response'].blank?
logger.debug "[api] Backtrace:\n#{response_body['backtrace'].join("\n")}" if response_body['backtrace'] && response_body['backtrace'].any?
end
end
super
end
private
def logger
@logger ||= Rails.logger
end
def current_user
@warden_user_for_log ||= begin
woo = env['warden'].instance_variable_get(:'@users')
woo[:user] if woo
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment