Class: RequestLog
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RequestLog
show all
- Defined in:
- lib/rails_api_logger/request_log.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.from_request(request, loggable: nil, skip_request_body: false) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/rails_api_logger/request_log.rb', line 14
def self.from_request(request, loggable: nil, skip_request_body: false)
if skip_request_body
body = "[Skipped]"
else
request_body = (request.body.respond_to?(:read) ? request.body.read : request.body)
body = request_body&.dup&.force_encoding("UTF-8")
begin
body = JSON.parse(body) if body.present?
rescue JSON::ParserError
body
end
end
create(path: request.path, request_body: body, method: request.method, started_at: Time.current, loggable: loggable)
end
|
Instance Method Details
#duration ⇒ Object
60
61
62
63
|
# File 'lib/rails_api_logger/request_log.rb', line 60
def duration
return if started_at.nil? || ended_at.nil?
ended_at - started_at
end
|
#formatted_body(body) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/rails_api_logger/request_log.rb', line 43
def formatted_body(body)
if body.is_a?(String) && body.blank?
""
elsif body.is_a?(Hash)
JSON.pretty_generate(body)
else
xml = Nokogiri::XML(body)
if xml.errors.any?
body
else
xml.to_xml(indent: 2)
end
end
rescue
body
end
|
#formatted_request_body ⇒ Object
35
36
37
|
# File 'lib/rails_api_logger/request_log.rb', line 35
def formatted_request_body
formatted_body(request_body)
end
|
#formatted_response_body ⇒ Object
39
40
41
|
# File 'lib/rails_api_logger/request_log.rb', line 39
def formatted_response_body
formatted_body(response_body)
end
|
#from_response(response, skip_response_body: false) ⇒ Object
29
30
31
32
33
|
# File 'lib/rails_api_logger/request_log.rb', line 29
def from_response(response, skip_response_body: false)
self.response_code = response.code
self.response_body = skip_response_body ? "[Skipped]" : manipulate_body(response.body)
self
end
|