rails_api_logger 0.1.1 → 0.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 436fe06f5a60eb0cfe6e341c526958b5d2e568ce44eb5e0d45fc704d8cc0151c
4
- data.tar.gz: b633e7b6ff50358f91f6096adaa0d693f5e39144316af68e6b821f06bf0858cc
3
+ metadata.gz: '058d907ef19e1e657d9b65e5c0eca5cc926df01496fc3213e4c057c1607bcb21'
4
+ data.tar.gz: a6434d726dde60f4dadc4db01f64c5a60d27934588f7c971bad6b5dc67728b72
5
5
  SHA512:
6
- metadata.gz: e17c8a4a95a6ef87c497257e5da27ed41b81fe9fede8b44767eeadaf8335ab68a63d15d6a27436dac72da6a3316b670927c72b3d7cb60a32a73361c8148e7654
7
- data.tar.gz: 914a5cd684eac764b416441f1e1f1a78318e3e0eb68a842620a4f4a7f867d94aebe06fea37faceb49a8a4aa25b349748ef1c14bdae9bcb1e71972a6a69c05195
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.1.1)
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.1)
12
- actionview (= 6.1.1)
13
- activesupport (= 6.1.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.1)
19
- activesupport (= 6.1.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.1)
25
- activesupport (= 6.1.1)
26
- activerecord (6.1.1)
27
- activemodel (= 6.1.1)
28
- activesupport (= 6.1.1)
29
- activesupport (6.1.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.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
- mini_portile2 (2.5.0)
48
- minitest (5.14.3)
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.1)
65
- actionpack (= 6.1.1)
66
- activesupport (= 6.1.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 in your controller by adding:
80
+ You can do so by adding this middleware in `config/application.rb`
81
81
 
82
82
  ```ruby
83
- around_action :log_inbound_request
83
+ config.middleware.insert_before Rails::Rack::Logger, InboundRequestLoggerMiddleware
84
84
  ```
85
85
 
86
- you can also log only requests that have an impact in your system with:
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
- around_action :log_inbound_request, if: :request_with_state_change?
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
 
@@ -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
- @inbound_request_log.loggable = loggable if loggable&.persisted?
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
@@ -1,3 +1,3 @@
1
1
  module RailsApiLogger
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.4"
3
3
  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.1.1
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-02-22 00:00:00.000000000 Z
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