-
-
Save sedovolosiy/fd0664b614bfd2cc55de0ffd54c71054 to your computer and use it in GitHub Desktop.
API logger with Grape under Rails
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
class ApiLogger < Grape::Middleware::Base | |
def before | |
Rails.logger.info "[api] Requested: #{request_log_data.to_json}\n" + | |
"[api] #{response_log_data[:description]} #{response_log_data[:source_file]}:#{response_log_data[:source_line]}" | |
end | |
private | |
def request_log_data | |
request_data = { | |
method: env['REQUEST_METHOD'], | |
path: env['PATH_INFO'], | |
query: env['QUERY_STRING'] | |
} | |
request_data[:user_id] = current_user.id if current_user | |
request_data | |
end | |
def response_log_data | |
{ | |
description: env['api.endpoint'].options[:route_options][:description], | |
source_file: env['api.endpoint'].block.source_location[0][(Rails.root.to_s.length+1)..-1], | |
source_line: env['api.endpoint'].block.source_location[1] | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment