-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaip_logger_for_grape.rb
50 lines (41 loc) · 1.24 KB
/
aip_logger_for_grape.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
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
def request_log_data_2
# Escape the ampersand in the POST data.
rack_input = env["rack.input"].gets
if rack_input.present?
rack_input = rack_input.gsub("&","%26")
params_data = Rack::Utils.parse_query(rack_input, "&")
else
params_data = nil
end
request_data = {
method: env['REQUEST_METHOD'],
path: env['PATH_INFO'],
query: env['QUERY_STRING'],
params: params_data
}
# request_data[:user_id] = current_user.id if current_user
request_data
end