rails_api_logger 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -6
- data/README.md +13 -5
- data/lib/rails_api_logger.rb +1 -0
- data/lib/rails_api_logger/inbound_requests_logger.rb +1 -19
- data/lib/rails_api_logger/inbound_requests_logger_middleware.rb +47 -0
- data/lib/rails_api_logger/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b613afbb5fea833002c8cefd748428314e45df42344a41aacf0230aa7cdd9ffa
|
4
|
+
data.tar.gz: 28ed28bde3d2aacfeee818600fb336d06355b0acbfab26cddd2ba779585dcb90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e435ae3d44e5be4307ba6fa8239687ba2f95a6dba1841d857305cdb6f7c6d5471e07b9cab659e35ea432d9e76eb6f62658be24ee21582cef7cc52789aa5a6db
|
7
|
+
data.tar.gz: cb0e8be6fba1f7108a93e124f57c0e10998c8354da03759ce5607ca8b4dd0f4fcb3d1a83ee442303290792f03a15098610ce2188d452c494a8c76174d9d40fae
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rails_api_logger (0.
|
4
|
+
rails_api_logger (0.2.0)
|
5
5
|
activerecord (>= 4.1.0)
|
6
6
|
railties (>= 4.1.0)
|
7
7
|
|
@@ -38,16 +38,14 @@ GEM
|
|
38
38
|
crass (1.0.6)
|
39
39
|
diff-lcs (1.4.4)
|
40
40
|
erubi (1.10.0)
|
41
|
-
i18n (1.8.
|
41
|
+
i18n (1.8.9)
|
42
42
|
concurrent-ruby (~> 1.0)
|
43
43
|
loofah (2.9.0)
|
44
44
|
crass (~> 1.0.2)
|
45
45
|
nokogiri (>= 1.5.9)
|
46
46
|
method_source (1.0.0)
|
47
|
-
|
48
|
-
|
49
|
-
nokogiri (1.11.1)
|
50
|
-
mini_portile2 (~> 2.5.0)
|
47
|
+
minitest (5.14.4)
|
48
|
+
nokogiri (1.11.1-x86_64-darwin)
|
51
49
|
racc (~> 1.4)
|
52
50
|
parallel (1.20.1)
|
53
51
|
parser (3.0.0.0)
|
data/README.md
CHANGED
@@ -77,23 +77,31 @@ This will guarantee that the log is always persisted, even in case of errors.
|
|
77
77
|
## Log Inbound Requests
|
78
78
|
|
79
79
|
If you are exposing some API you might be interested in logging the requests you receive.
|
80
|
-
You can do so
|
80
|
+
You can do so by adding this middleware in `config/application.rb`
|
81
81
|
|
82
82
|
```ruby
|
83
|
-
|
83
|
+
config.middleware.insert_before Rails::Rack::Logger, InboundRequestLoggerMiddleware
|
84
84
|
```
|
85
85
|
|
86
|
-
|
86
|
+
this will by default only log requests that have an impact in your system (POST, PUT, and PATCH calls).
|
87
|
+
If you want to log all requests (also GET ones) use
|
87
88
|
|
88
89
|
```ruby
|
89
|
-
|
90
|
+
config.middleware.insert_before Rails::Rack::Logger, InboundRequestLoggerMiddleware, only_state_change: false
|
91
|
+
```
|
92
|
+
|
93
|
+
If you want to log only requests on a certain path, you can pass a regular expression:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
config.middleware.insert_before Rails::Rack::Logger, InboundRequestLoggerMiddleware, path_regexp: /api/
|
90
97
|
```
|
91
98
|
|
92
|
-
this will create a log entry only for POST, PUT, and PATCH calls.
|
93
99
|
|
94
100
|
In the implementation of your API, you can call any time `attach_inbound_request_loggable(model)`
|
95
101
|
to attach an already persisted model to the log record.
|
96
102
|
|
103
|
+
|
104
|
+
|
97
105
|
For example:
|
98
106
|
```ruby
|
99
107
|
|
data/lib/rails_api_logger.rb
CHANGED
@@ -4,6 +4,7 @@ require "rails_api_logger/request_log"
|
|
4
4
|
require "rails_api_logger/inbound_request_log"
|
5
5
|
require "rails_api_logger/outbound_request_log"
|
6
6
|
require "rails_api_logger/inbound_requests_logger"
|
7
|
+
require "rails_api_logger/inbound_requests_logger_middleware"
|
7
8
|
|
8
9
|
module RailsApiLogger
|
9
10
|
class Error < StandardError; end
|
@@ -3,29 +3,11 @@ module InboundRequestsLogger
|
|
3
3
|
|
4
4
|
private
|
5
5
|
|
6
|
-
def log_inbound_request
|
7
|
-
@inbound_request_log = InboundRequestLog.from_request(request)
|
8
|
-
yield
|
9
|
-
@inbound_request_log.update(response_body: JSON.parse(response.body), response_code: response.code)
|
10
|
-
end
|
11
|
-
|
12
|
-
def request_with_state_change?
|
13
|
-
request.post? || request.put? || request.patch? || request.delete?
|
14
|
-
end
|
15
|
-
|
16
|
-
def request_without_body?
|
17
|
-
request.get? || request.head? || request.options? || request.delete?
|
18
|
-
end
|
19
|
-
|
20
6
|
def attach_inbound_request_loggable(loggable)
|
21
|
-
|
7
|
+
request.env["inbound_request_log"].loggable = loggable if loggable&.persisted?
|
22
8
|
end
|
23
9
|
end
|
24
10
|
|
25
11
|
ActiveSupport.on_load(:action_controller) do
|
26
12
|
include InboundRequestsLogger
|
27
13
|
end
|
28
|
-
|
29
|
-
ActiveSupport.on_load(:action_controller_api) do
|
30
|
-
include InboundRequestsLogger
|
31
|
-
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class InboundRequestLoggerMiddleware
|
2
|
+
attr_accessor :only_state_change, :path_regexp
|
3
|
+
|
4
|
+
def initialize(app, only_state_change: true, path_regexp: /.*/)
|
5
|
+
@app = app
|
6
|
+
self.only_state_change = only_state_change
|
7
|
+
self.path_regexp = path_regexp
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
request = ActionDispatch::Request.new(env)
|
12
|
+
if log?(request)
|
13
|
+
@inbound_request_log = InboundRequestLog.from_request(request)
|
14
|
+
env["inbound_request_log"] = @inbound_request_log
|
15
|
+
request.body.rewind
|
16
|
+
end
|
17
|
+
status, headers, body = @app.call(env)
|
18
|
+
if log?(request)
|
19
|
+
@inbound_request_log.update(response_body: parsed_body(body), response_code: status)
|
20
|
+
end
|
21
|
+
[status, headers, body]
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def log?(request)
|
27
|
+
request.path =~ path_regexp && (!only_state_change || request_with_state_change?(request))
|
28
|
+
end
|
29
|
+
|
30
|
+
def parsed_body(body)
|
31
|
+
return unless body.present?
|
32
|
+
|
33
|
+
if body.respond_to?(:body)
|
34
|
+
JSON.parse(body.body)
|
35
|
+
elsif body.respond_to?(:[])
|
36
|
+
JSON.parse(body[0])
|
37
|
+
else
|
38
|
+
body
|
39
|
+
end
|
40
|
+
rescue JSON::ParserError
|
41
|
+
body
|
42
|
+
end
|
43
|
+
|
44
|
+
def request_with_state_change?(request)
|
45
|
+
request.post? || request.put? || request.patch? || request.delete?
|
46
|
+
end
|
47
|
+
end
|
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.2.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: 2021-02
|
11
|
+
date: 2021-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/rails_api_logger.rb
|
93
93
|
- lib/rails_api_logger/inbound_request_log.rb
|
94
94
|
- lib/rails_api_logger/inbound_requests_logger.rb
|
95
|
+
- lib/rails_api_logger/inbound_requests_logger_middleware.rb
|
95
96
|
- lib/rails_api_logger/outbound_request_log.rb
|
96
97
|
- lib/rails_api_logger/rails_api_logger_rails_admin_configuration.rb
|
97
98
|
- lib/rails_api_logger/request_log.rb
|