rails_api_logger 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +4 -2
- data/lib/rails_api_logger/inbound_requests_logger_middleware.rb +19 -7
- data/lib/rails_api_logger/request_log.rb +16 -10
- data/lib/rails_api_logger.rb +5 -4
- data/rails_api_logger.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f1fb1fae010aa6cf4bdda941fd6ad2ce3ac5583faa690d23680194d2f3eb991
|
4
|
+
data.tar.gz: 0dd85cd48972098af775d7a5feec4d17b8aaaa51641e9a4405244d8ccdd12644
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d310658b9bbae0d5406a8df1d7a85e11d161db8405ad970cb422fbd9ad38d2a95c251a8ba3f6acbe30089290b61d94ed8c8cd1b0e601fbbea6f8d2a111b7ae0
|
7
|
+
data.tar.gz: c3c29eaf30dbbb4548b3527ac7220193135a4ca0eb69b5f17b12e0ef88516b8aa91c1b8367dacd4a4ee5988c4722ba493c5e98a28ba7b5c61645db971c36bcdb
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# 0.9.0
|
2
|
+
* Add option skip_request_body to skip the request body. Use this option when you don't want to persist the request body. `[Skipped]` will be persisted instead.
|
3
|
+
* Add option skip_request_body_regexp to skip logging the body of requests matching a regexp.
|
4
|
+
* Renamed the option skip_body into skip_response_body. This is a breaking change!
|
5
|
+
* Renamed the option skip_body_regexp into skip_response_body_regexp. This is a breaking change!
|
6
|
+
|
1
7
|
# 0.8.1
|
2
8
|
* Fix Rails 7.1 warnings.
|
3
9
|
|
data/README.md
CHANGED
@@ -128,10 +128,12 @@ If you want to log only requests on a certain path, you can pass a regular expre
|
|
128
128
|
config.middleware.insert_before Rails::Rack::Logger, InboundRequestsLoggerMiddleware, path_regexp: /api/
|
129
129
|
```
|
130
130
|
|
131
|
-
If you want to skip logging the body of certain requests, you can pass a regular expression:
|
131
|
+
If you want to skip logging the response or request body of certain requests, you can pass a regular expression:
|
132
132
|
|
133
133
|
```ruby
|
134
|
-
config.middleware.insert_before Rails::Rack::Logger, InboundRequestsLoggerMiddleware,
|
134
|
+
config.middleware.insert_before Rails::Rack::Logger, InboundRequestsLoggerMiddleware,
|
135
|
+
skip_request_body_regexp: /api/books/,
|
136
|
+
skip_response_body_regexp: /api/letters/
|
135
137
|
```
|
136
138
|
|
137
139
|
|
@@ -1,24 +1,32 @@
|
|
1
1
|
class InboundRequestsLoggerMiddleware
|
2
|
-
attr_accessor :only_state_change, :path_regexp, :
|
2
|
+
attr_accessor :only_state_change, :path_regexp, :skip_request_body_regexp, :skip_response_body_regexp
|
3
3
|
|
4
|
-
def initialize(app, only_state_change: true,
|
4
|
+
def initialize(app, only_state_change: true,
|
5
|
+
path_regexp: /.*/,
|
6
|
+
skip_request_body_regexp: nil,
|
7
|
+
skip_response_body_regexp: nil)
|
5
8
|
@app = app
|
6
9
|
self.only_state_change = only_state_change
|
7
10
|
self.path_regexp = path_regexp
|
8
|
-
self.
|
11
|
+
self.skip_request_body_regexp = skip_request_body_regexp
|
12
|
+
self.skip_response_body_regexp = skip_response_body_regexp
|
9
13
|
end
|
10
14
|
|
11
15
|
def call(env)
|
12
16
|
request = ActionDispatch::Request.new(env)
|
13
17
|
logging = log?(env, request)
|
14
18
|
if logging
|
15
|
-
env["INBOUND_REQUEST_LOG"] = InboundRequestLog.from_request(request)
|
19
|
+
env["INBOUND_REQUEST_LOG"] = InboundRequestLog.from_request(request, skip_request_body: skip_request_body?(env))
|
16
20
|
request.body.rewind
|
17
21
|
end
|
18
22
|
status, headers, body = @app.call(env)
|
19
23
|
if logging
|
20
24
|
updates = {response_code: status, ended_at: Time.current}
|
21
|
-
updates[:response_body] =
|
25
|
+
updates[:response_body] = if skip_response_body?(env)
|
26
|
+
"[Skipped]"
|
27
|
+
else
|
28
|
+
parsed_body(body)
|
29
|
+
end
|
22
30
|
# this usually works. let's be optimistic.
|
23
31
|
begin
|
24
32
|
env["INBOUND_REQUEST_LOG"].update_columns(updates)
|
@@ -31,8 +39,12 @@ class InboundRequestsLoggerMiddleware
|
|
31
39
|
|
32
40
|
private
|
33
41
|
|
34
|
-
def
|
35
|
-
|
42
|
+
def skip_request_body?(env)
|
43
|
+
skip_request_body_regexp && env["PATH_INFO"] =~ skip_request_body_regexp
|
44
|
+
end
|
45
|
+
|
46
|
+
def skip_response_body?(env)
|
47
|
+
skip_response_body_regexp && env["PATH_INFO"] =~ skip_response_body_regexp
|
36
48
|
end
|
37
49
|
|
38
50
|
def log?(env, request)
|
@@ -11,20 +11,24 @@ class RequestLog < ActiveRecord::Base
|
|
11
11
|
validates :method, presence: true
|
12
12
|
validates :path, presence: true
|
13
13
|
|
14
|
-
def self.from_request(request, loggable: nil)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def self.from_request(request, loggable: nil, skip_request_body: false)
|
15
|
+
if skip_request_body
|
16
|
+
body = "[Skipped]"
|
17
|
+
else
|
18
|
+
request_body = (request.body.respond_to?(:read) ? request.body.read : request.body)
|
19
|
+
body = request_body&.dup&.force_encoding("UTF-8")
|
20
|
+
begin
|
21
|
+
body = JSON.parse(body) if body.present?
|
22
|
+
rescue JSON::ParserError
|
23
|
+
body
|
24
|
+
end
|
21
25
|
end
|
22
26
|
create(path: request.path, request_body: body, method: request.method, started_at: Time.current, loggable: loggable)
|
23
27
|
end
|
24
28
|
|
25
|
-
def from_response(response,
|
29
|
+
def from_response(response, skip_response_body: false)
|
26
30
|
self.response_code = response.code
|
27
|
-
self.response_body =
|
31
|
+
self.response_body = skip_response_body ? "[Skipped]" : manipulate_body(response.body)
|
28
32
|
self
|
29
33
|
end
|
30
34
|
|
@@ -37,7 +41,9 @@ class RequestLog < ActiveRecord::Base
|
|
37
41
|
end
|
38
42
|
|
39
43
|
def formatted_body(body)
|
40
|
-
if body.is_a?(
|
44
|
+
if body.is_a?(String) && body.blank?
|
45
|
+
""
|
46
|
+
elsif body.is_a?(Hash)
|
41
47
|
JSON.pretty_generate(body)
|
42
48
|
else
|
43
49
|
xml = Nokogiri::XML(body)
|
data/lib/rails_api_logger.rb
CHANGED
@@ -10,15 +10,16 @@ loader.setup
|
|
10
10
|
class RailsApiLogger
|
11
11
|
class Error < StandardError; end
|
12
12
|
|
13
|
-
def initialize(loggable = nil,
|
13
|
+
def initialize(loggable = nil, skip_request_body: false, skip_response_body: false)
|
14
14
|
@loggable = loggable
|
15
|
-
@
|
15
|
+
@skip_request_body = skip_request_body
|
16
|
+
@skip_response_body = skip_response_body
|
16
17
|
end
|
17
18
|
|
18
19
|
def call(url, request)
|
19
|
-
log = OutboundRequestLog.from_request(request, loggable: @loggable)
|
20
|
+
log = OutboundRequestLog.from_request(request, loggable: @loggable, skip_request_body: @skip_request_body)
|
20
21
|
yield.tap do |response|
|
21
|
-
log.from_response(response,
|
22
|
+
log.from_response(response, skip_response_body: @skip_response_body)
|
22
23
|
end
|
23
24
|
rescue => e
|
24
25
|
log.response_body = {error: e.message}
|
data/rails_api_logger.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_api_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Rodi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|