rails_api_logger 0.4.1 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '00489e11faa84622f3557560cf9d989b1fb36f6c6d18fdd41f1f1784e59893dd'
4
- data.tar.gz: 54f3202bd8446cce3a32c97a2da3b2f881d317207c511bf1d9ada9250ac751d8
3
+ metadata.gz: 4b9cef8f177c929097fa995670c4a071e8527b843837eac867bbb75b71fab198
4
+ data.tar.gz: 58681f53d1a7d9eeb6c3b13646a245a9bd5cbc163a48f656f24900646d74edc1
5
5
  SHA512:
6
- metadata.gz: 254e3a0054438fa416561f5e34155529f4860d255cb01e80c5ea5a5a65bac694b35bc6b88199775b1fa11bc6532981f6e514b517f703140020c6b9618284ce1e
7
- data.tar.gz: bf5537dfcb5eec1c675b9eb68bee42a8700fa756d1728666c67075a77a9f99baf0f573b5090e36dd6e345c0d5471b923fbcaddd8f689c257e89c496e858722cd
6
+ metadata.gz: ba47e17a09c47b7f8796fe8600b0fd02399ff17c44290da3ed853de40227ece45c8991832ac187a7304072d7e73bc8dad86a749a673069624afe14ff6ea952dc
7
+ data.tar.gz: 74d30c8338898eff42ebc791ca120546e19054b6aba2f0960c75168f7d3d7aaf6faf297252b8ec658aea682776022363cfd08a70c976a9f1ba2e824982180b50
@@ -1,19 +1,18 @@
1
1
  version: v1.0
2
- name: master-deploy
2
+ name: main-deploy
3
3
  agent:
4
4
  machine:
5
5
  type: e1-standard-2
6
6
  os_image: ubuntu1804
7
7
 
8
8
  blocks:
9
- - name: master-deploy
9
+ - name: main-deploy
10
10
  task:
11
11
  secrets:
12
12
  - name: rubygems-deploy
13
13
  jobs:
14
- - name: master-deploy
14
+ - name: main-deploy
15
15
  commands:
16
16
  - checkout --use-cache
17
17
  - gem build rails_api_logger
18
- - chmod 0600 ~/.gem/credentials
19
18
  - gem push rails_api_logger-*.gem
@@ -19,7 +19,8 @@ blocks:
19
19
  commands:
20
20
  - checkout --use-cache
21
21
  - cache restore
22
- - bundle install --deployment -j 4 --path vendor/bundle
22
+ - bundle config set path 'vendor/bundle'
23
+ - bundle install -j 4
23
24
  - cache store
24
25
  jobs:
25
26
  - name: tests
@@ -27,5 +28,5 @@ blocks:
27
28
  - bundle exec standardrb
28
29
  - bundle exec rspec
29
30
  promotions:
30
- - name: master
31
- pipeline_file: master-deploy.yml
31
+ - name: main
32
+ pipeline_file: main-deploy.yml
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ # 0.6.1
2
+ * Fixes the loading of concern into controllers.
3
+
4
+ # 0.6.0
5
+ * Fixes an important concurrency issue by removing instance variables in the rack middleware.
6
+
7
+ # 0.5.0
8
+ * Started using Zeitwerk.
9
+ * Removed RailsAdmin specific code.
10
+ * Improved RailsApiLogger class.
11
+
1
12
  # 0.4.1
2
13
  * Fixed the `.failed` scope.
3
14
 
@@ -18,7 +29,7 @@ add_column :outbound_request_logs, :ended_at, :timestamp
18
29
  * Added `formatted_request_body` and `formatted_response_body` methods.
19
30
 
20
31
  # 0.2.0
21
- * Switch to a middleware solution
32
+ * Switch to a middleware solution.
22
33
 
23
34
  # 0.1.0
24
- * Initial release
35
+ * Initial release.
data/README.md CHANGED
@@ -72,9 +72,9 @@ You can also use the provided logger class to do that in a simpler and safer man
72
72
 
73
73
  ```ruby
74
74
  uri = URI('http://example.com/some_path?query=string')
75
- http = Net::HTTP.start(uri.host, uri.port)
75
+ http = Net::HTTP.new(uri.host, uri.port)
76
76
  request = Net::HTTP::Get.new(uri)
77
- response = RailsApiLogger.call(uri, http, request)
77
+ response = RailsApiLogger.new.call(uri, request) { http.start { |http| http.request(request) } }
78
78
  ```
79
79
 
80
80
  This will guarantee that the log is always persisted, even in case of errors.
@@ -170,12 +170,6 @@ This configuration will give you some nice views, and searches to work with the
170
170
  end
171
171
  ```
172
172
 
173
- you can also add this configuration directly in `rails_admin.rb` with
174
-
175
- ```ruby
176
- include RailsApiLoggerRailsAdminConfiguration
177
- ```
178
-
179
173
  ## Development
180
174
 
181
175
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -186,7 +180,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
186
180
 
187
181
  Bug reports and pull requests are welcome on GitHub at https://github.com/renuo/rails_api_logger.
188
182
  This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
189
- the [code of conduct](https://github.com/renuo/rails_api_logger/blob/master/CODE_OF_CONDUCT.md).
183
+ the [code of conduct](https://github.com/renuo/rails_api_logger/blob/main/CODE_OF_CONDUCT.md).
190
184
 
191
185
  ## License
192
186
 
@@ -195,4 +189,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
195
189
  ## Code of Conduct
196
190
 
197
191
  Everyone interacting in the RailsApiLogger project's codebases, issue trackers, chat rooms and mailing lists is
198
- expected to follow the [code of conduct](https://github.com/renuo/rails_api_logger/blob/master/CODE_OF_CONDUCT.md).
192
+ expected to follow the [code of conduct](https://github.com/renuo/rails_api_logger/blob/main/CODE_OF_CONDUCT.md).
@@ -3,7 +3,7 @@
3
3
  require "rails/generators/base"
4
4
  require "rails/generators/migration"
5
5
 
6
- module RailsApiLogger
6
+ class RailsApiLogger
7
7
  module Generators
8
8
  class InstallGenerator < Rails::Generators::Base
9
9
  include Rails::Generators::Migration
@@ -4,10 +4,6 @@ module InboundRequestsLogger
4
4
  private
5
5
 
6
6
  def attach_inbound_request_loggable(loggable)
7
- request.env["inbound_request_log"].update(loggable: loggable) if loggable&.persisted?
7
+ request.env["INBOUND_REQUEST_LOG"].update(loggable: loggable) if loggable&.persisted?
8
8
  end
9
9
  end
10
-
11
- ActiveSupport.on_load(:action_controller) do
12
- include InboundRequestsLogger
13
- end
@@ -1,4 +1,4 @@
1
- class InboundRequestLoggerMiddleware
1
+ class InboundRequestsLoggerMiddleware
2
2
  attr_accessor :only_state_change, :path_regexp
3
3
 
4
4
  def initialize(app, only_state_change: true, path_regexp: /.*/)
@@ -11,13 +11,14 @@ class InboundRequestLoggerMiddleware
11
11
  request = ActionDispatch::Request.new(env)
12
12
  logging = log?(env, request)
13
13
  if logging
14
- @inbound_request_log = InboundRequestLog.from_request(request)
15
- env["inbound_request_log"] = @inbound_request_log
14
+ env["INBOUND_REQUEST_LOG"] = InboundRequestLog.from_request(request)
16
15
  request.body.rewind
17
16
  end
18
17
  status, headers, body = @app.call(env)
19
18
  if logging
20
- @inbound_request_log.update_columns(response_body: parsed_body(body), response_code: status, ended_at: Time.current)
19
+ env["INBOUND_REQUEST_LOG"].update_columns(response_body: parsed_body(body),
20
+ response_code: status,
21
+ ended_at: Time.current)
21
22
  end
22
23
  [status, headers, body]
23
24
  end
@@ -25,7 +26,7 @@ class InboundRequestLoggerMiddleware
25
26
  private
26
27
 
27
28
  def log?(env, request)
28
- env["REQUEST_URI"] =~ path_regexp && (!only_state_change || request_with_state_change?(request))
29
+ env["PATH_INFO"] =~ path_regexp && (!only_state_change || request_with_state_change?(request))
29
30
  end
30
31
 
31
32
  def parsed_body(body)
@@ -11,7 +11,7 @@ class RequestLog < ActiveRecord::Base
11
11
  validates :method, presence: true
12
12
  validates :path, presence: true
13
13
 
14
- def self.from_request(request)
14
+ def self.from_request(request, loggable: nil)
15
15
  request_body = (request.body.respond_to?(:read) ? request.body.read : request.body)
16
16
  body = request_body ? request_body.dup.force_encoding("UTF-8") : nil
17
17
  begin
@@ -19,7 +19,18 @@ class RequestLog < ActiveRecord::Base
19
19
  rescue JSON::ParserError
20
20
  body
21
21
  end
22
- create(path: request.path, request_body: body, method: request.method, started_at: Time.current)
22
+ create(path: request.path, request_body: body, method: request.method, started_at: Time.current, loggable: loggable)
23
+ end
24
+
25
+ def from_response(response)
26
+ self.response_code = response.code
27
+ body = response.body ? response.body.dup.force_encoding("UTF-8") : nil
28
+ begin
29
+ body = JSON.parse(body) if body.present?
30
+ rescue JSON::ParserError
31
+ body
32
+ end
33
+ self.response_body = body
23
34
  end
24
35
 
25
36
  def formatted_request_body
@@ -1,21 +1,23 @@
1
1
  require "active_record"
2
2
  require "nokogiri"
3
- require "rails_api_logger/version"
4
- require "rails_api_logger/request_log"
5
- require "rails_api_logger/inbound_request_log"
6
- require "rails_api_logger/outbound_request_log"
7
- require "rails_api_logger/inbound_requests_logger"
8
- require "rails_api_logger/inbound_requests_logger_middleware"
3
+ require "zeitwerk"
9
4
 
10
- module RailsApiLogger
5
+ loader = Zeitwerk::Loader.for_gem
6
+ loader.collapse("#{__dir__}/rails_api_logger")
7
+ loader.do_not_eager_load("#{__dir__}/generators")
8
+ loader.setup
9
+
10
+ class RailsApiLogger
11
11
  class Error < StandardError; end
12
12
 
13
- def self.call(uri, http, request)
14
- log = OutboundRequestLog.from_request(request)
13
+ def initialize(loggable = nil)
14
+ @loggable = loggable
15
+ end
15
16
 
16
- http.request(request).tap do |response|
17
- log.response_code = response.code
18
- log.response_body = response.body
17
+ def call(url, request)
18
+ log = OutboundRequestLog.from_request(request, loggable: @loggable)
19
+ yield.tap do |response|
20
+ log.from_response(response)
19
21
  end
20
22
  rescue => e
21
23
  log.response_body = {error: e.message}
@@ -25,3 +27,7 @@ module RailsApiLogger
25
27
  log.save!
26
28
  end
27
29
  end
30
+
31
+ ActiveSupport.on_load(:action_controller) do
32
+ include InboundRequestsLogger
33
+ end
@@ -1,8 +1,6 @@
1
- require_relative "lib/rails_api_logger/version"
2
-
3
1
  Gem::Specification.new do |spec|
4
2
  spec.name = "rails_api_logger"
5
- spec.version = RailsApiLogger::VERSION
3
+ spec.version = "0.6.1"
6
4
  spec.authors = ["Alessandro Rodi"]
7
5
  spec.email = ["alessandro.rodi@renuo.ch"]
8
6
 
@@ -27,10 +25,13 @@ Gem::Specification.new do |spec|
27
25
 
28
26
  spec.add_dependency "railties", ">= 4.1.0"
29
27
  spec.add_dependency "activerecord", ">= 4.1.0"
28
+ spec.add_dependency "actionpack", ">= 4.1.0"
30
29
  spec.add_dependency "nokogiri"
30
+ spec.add_dependency "zeitwerk", ">= 2.0.0"
31
31
 
32
32
  spec.add_development_dependency "sqlite3", "~> 1.4.0"
33
33
  spec.add_development_dependency "standard", "~> 0.13.0"
34
34
  spec.add_development_dependency "rake", "~> 12.0"
35
35
  spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "rack"
36
37
  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.1
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Rodi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-09 00:00:00.000000000 Z
11
+ date: 2022-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 4.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 4.1.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: nokogiri
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: zeitwerk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: sqlite3
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +136,20 @@ dependencies:
108
136
  - - "~>"
109
137
  - !ruby/object:Gem::Version
110
138
  version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rack
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
111
153
  description: Log inbound and outbound API requests in your Rails application
112
154
  email:
113
155
  - alessandro.rodi@renuo.ch
@@ -118,7 +160,7 @@ files:
118
160
  - ".gitignore"
119
161
  - ".rspec"
120
162
  - ".ruby-version"
121
- - ".semaphore/master-deploy.yml"
163
+ - ".semaphore/main-deploy.yml"
122
164
  - ".semaphore/semaphore.yml"
123
165
  - CHANGELOG.md
124
166
  - CODE_OF_CONDUCT.md
@@ -135,9 +177,7 @@ files:
135
177
  - lib/rails_api_logger/inbound_requests_logger.rb
136
178
  - lib/rails_api_logger/inbound_requests_logger_middleware.rb
137
179
  - lib/rails_api_logger/outbound_request_log.rb
138
- - lib/rails_api_logger/rails_api_logger_rails_admin_configuration.rb
139
180
  - lib/rails_api_logger/request_log.rb
140
- - lib/rails_api_logger/version.rb
141
181
  - rails_api_logger.gemspec
142
182
  homepage: https://github.com/renuo/rails_api_logger
143
183
  licenses:
@@ -146,7 +186,7 @@ metadata:
146
186
  homepage_uri: https://github.com/renuo/rails_api_logger
147
187
  source_code_uri: https://github.com/renuo/rails_api_logger
148
188
  changelog_uri: https://github.com/renuo/rails_api_logger/CHANGELOG.md
149
- post_install_message:
189
+ post_install_message:
150
190
  rdoc_options: []
151
191
  require_paths:
152
192
  - lib
@@ -161,8 +201,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
201
  - !ruby/object:Gem::Version
162
202
  version: '0'
163
203
  requirements: []
164
- rubygems_version: 3.2.16
165
- signing_key:
204
+ rubygems_version: 3.1.4
205
+ signing_key:
166
206
  specification_version: 4
167
207
  summary: "Log API requests like a king \U0001F451"
168
208
  test_files: []
@@ -1,33 +0,0 @@
1
- %w[InboundRequestLog OutboundRequestLog].each do |logging_model|
2
- config.model logging_model do
3
- list do
4
- filters %i[method path response_code request_body response_body created_at]
5
- scopes [nil, :failed]
6
-
7
- include_fields :method, :path, :response_code, :created_at
8
-
9
- field :request_body, :string do
10
- visible false
11
- searchable true
12
- filterable true
13
- end
14
-
15
- field :response_body, :string do
16
- visible false
17
- searchable true
18
- filterable true
19
- end
20
- end
21
-
22
- show do
23
- include_fields :loggable, :method, :path, :response_code
24
- field(:created_at)
25
- field(:request_body) do
26
- formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].request_body)}</pre>".html_safe }
27
- end
28
- field(:response_body) do
29
- formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].response_body)}</pre>".html_safe }
30
- end
31
- end
32
- end
33
- end
@@ -1,3 +0,0 @@
1
- module RailsApiLogger
2
- VERSION = "0.4.1"
3
- end