rails_api_logger 0.1.1 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +18 -20
- 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 +48 -0
- data/lib/rails_api_logger/request_log.rb +1 -1
- 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: '058d907ef19e1e657d9b65e5c0eca5cc926df01496fc3213e4c057c1607bcb21'
|
4
|
+
data.tar.gz: a6434d726dde60f4dadc4db01f64c5a60d27934588f7c971bad6b5dc67728b72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69fac1e76d28e62ea3d4d03f6373cccb4179bb735bc5b7ae915c9448fc172315215ce943ebeea91352d66a120e4956b79f7612c238134d3898f4b771c0018ef6
|
7
|
+
data.tar.gz: e73da9b67ee4af50ae219519cf38bdb28083de938f77df56b754708eabb135e4ad66013ff52304862a9e93b14ad3d9a8293c019dc67c40799e9cb827607ca45f
|
data/Gemfile.lock
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rails_api_logger (0.
|
4
|
+
rails_api_logger (0.2.4)
|
5
5
|
activerecord (>= 4.1.0)
|
6
6
|
railties (>= 4.1.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actionpack (6.1.
|
12
|
-
actionview (= 6.1.
|
13
|
-
activesupport (= 6.1.
|
11
|
+
actionpack (6.1.3)
|
12
|
+
actionview (= 6.1.3)
|
13
|
+
activesupport (= 6.1.3)
|
14
14
|
rack (~> 2.0, >= 2.0.9)
|
15
15
|
rack-test (>= 0.6.3)
|
16
16
|
rails-dom-testing (~> 2.0)
|
17
17
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
18
|
-
actionview (6.1.
|
19
|
-
activesupport (= 6.1.
|
18
|
+
actionview (6.1.3)
|
19
|
+
activesupport (= 6.1.3)
|
20
20
|
builder (~> 3.1)
|
21
21
|
erubi (~> 1.4)
|
22
22
|
rails-dom-testing (~> 2.0)
|
23
23
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
24
|
-
activemodel (6.1.
|
25
|
-
activesupport (= 6.1.
|
26
|
-
activerecord (6.1.
|
27
|
-
activemodel (= 6.1.
|
28
|
-
activesupport (= 6.1.
|
29
|
-
activesupport (6.1.
|
24
|
+
activemodel (6.1.3)
|
25
|
+
activesupport (= 6.1.3)
|
26
|
+
activerecord (6.1.3)
|
27
|
+
activemodel (= 6.1.3)
|
28
|
+
activesupport (= 6.1.3)
|
29
|
+
activesupport (6.1.3)
|
30
30
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
31
|
i18n (>= 1.6, < 2)
|
32
32
|
minitest (>= 5.1)
|
@@ -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.2-x86_64-darwin)
|
51
49
|
racc (~> 1.4)
|
52
50
|
parallel (1.20.1)
|
53
51
|
parser (3.0.0.0)
|
@@ -61,9 +59,9 @@ GEM
|
|
61
59
|
nokogiri (>= 1.6)
|
62
60
|
rails-html-sanitizer (1.3.0)
|
63
61
|
loofah (~> 2.3)
|
64
|
-
railties (6.1.
|
65
|
-
actionpack (= 6.1.
|
66
|
-
activesupport (= 6.1.
|
62
|
+
railties (6.1.3)
|
63
|
+
actionpack (= 6.1.3)
|
64
|
+
activesupport (= 6.1.3)
|
67
65
|
method_source
|
68
66
|
rake (>= 0.8.7)
|
69
67
|
thor (~> 1.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"].update(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,48 @@
|
|
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
|
+
logging = log?(env, request)
|
13
|
+
if logging
|
14
|
+
@inbound_request_log = InboundRequestLog.from_request(request)
|
15
|
+
env["inbound_request_log"] = @inbound_request_log
|
16
|
+
request.body.rewind
|
17
|
+
end
|
18
|
+
status, headers, body = @app.call(env)
|
19
|
+
if logging
|
20
|
+
@inbound_request_log.update_columns(response_body: parsed_body(body), response_code: status)
|
21
|
+
end
|
22
|
+
[status, headers, body]
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def log?(env, request)
|
28
|
+
env["REQUEST_URI"] =~ path_regexp && (!only_state_change || request_with_state_change?(request))
|
29
|
+
end
|
30
|
+
|
31
|
+
def parsed_body(body)
|
32
|
+
return unless body.present?
|
33
|
+
|
34
|
+
if body.respond_to?(:body)
|
35
|
+
JSON.parse(body.body)
|
36
|
+
elsif body.respond_to?(:[])
|
37
|
+
JSON.parse(body[0])
|
38
|
+
else
|
39
|
+
body
|
40
|
+
end
|
41
|
+
rescue JSON::ParserError
|
42
|
+
body
|
43
|
+
end
|
44
|
+
|
45
|
+
def request_with_state_change?(request)
|
46
|
+
request.post? || request.put? || request.patch? || request.delete?
|
47
|
+
end
|
48
|
+
end
|
@@ -13,7 +13,7 @@ class RequestLog < ActiveRecord::Base
|
|
13
13
|
validates :path, presence: true
|
14
14
|
|
15
15
|
def self.from_request(request)
|
16
|
-
body = (request.body.respond_to?(:read) ? request.body.read : request.body).force_encoding("UTF-8")
|
16
|
+
body = (request.body.respond_to?(:read) ? request.body.read : request.body).dup.force_encoding("UTF-8")
|
17
17
|
begin
|
18
18
|
body = JSON.parse(body) if body.present?
|
19
19
|
rescue JSON::ParserError
|
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.4
|
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-
|
11
|
+
date: 2021-03-11 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
|