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 +4 -4
- data/.semaphore/{master-deploy.yml → main-deploy.yml} +3 -4
- data/.semaphore/semaphore.yml +4 -3
- data/CHANGELOG.md +13 -2
- data/README.md +4 -10
- data/lib/generators/rails_api_logger/install_generator.rb +1 -1
- data/lib/rails_api_logger/inbound_requests_logger.rb +1 -5
- data/lib/rails_api_logger/inbound_requests_logger_middleware.rb +6 -5
- data/lib/rails_api_logger/request_log.rb +13 -2
- data/lib/rails_api_logger.rb +18 -12
- data/rails_api_logger.gemspec +4 -3
- metadata +49 -9
- data/lib/rails_api_logger/rails_api_logger_rails_admin_configuration.rb +0 -33
- data/lib/rails_api_logger/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b9cef8f177c929097fa995670c4a071e8527b843837eac867bbb75b71fab198
|
4
|
+
data.tar.gz: 58681f53d1a7d9eeb6c3b13646a245a9bd5cbc163a48f656f24900646d74edc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba47e17a09c47b7f8796fe8600b0fd02399ff17c44290da3ed853de40227ece45c8991832ac187a7304072d7e73bc8dad86a749a673069624afe14ff6ea952dc
|
7
|
+
data.tar.gz: 74d30c8338898eff42ebc791ca120546e19054b6aba2f0960c75168f7d3d7aaf6faf297252b8ec658aea682776022363cfd08a70c976a9f1ba2e824982180b50
|
@@ -1,19 +1,18 @@
|
|
1
1
|
version: v1.0
|
2
|
-
name:
|
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:
|
9
|
+
- name: main-deploy
|
10
10
|
task:
|
11
11
|
secrets:
|
12
12
|
- name: rubygems-deploy
|
13
13
|
jobs:
|
14
|
-
- name:
|
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
|
data/.semaphore/semaphore.yml
CHANGED
@@ -19,7 +19,8 @@ blocks:
|
|
19
19
|
commands:
|
20
20
|
- checkout --use-cache
|
21
21
|
- cache restore
|
22
|
-
- 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:
|
31
|
-
pipeline_file:
|
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.
|
75
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
76
76
|
request = Net::HTTP::Get.new(uri)
|
77
|
-
response = RailsApiLogger.call(uri, http
|
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/
|
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/
|
192
|
+
expected to follow the [code of conduct](https://github.com/renuo/rails_api_logger/blob/main/CODE_OF_CONDUCT.md).
|
@@ -4,10 +4,6 @@ module InboundRequestsLogger
|
|
4
4
|
private
|
5
5
|
|
6
6
|
def attach_inbound_request_loggable(loggable)
|
7
|
-
request.env["
|
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
|
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
|
-
|
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
|
-
|
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["
|
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
|
data/lib/rails_api_logger.rb
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
require "active_record"
|
2
2
|
require "nokogiri"
|
3
|
-
require "
|
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
|
-
|
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
|
14
|
-
|
13
|
+
def initialize(loggable = nil)
|
14
|
+
@loggable = loggable
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
data/rails_api_logger.gemspec
CHANGED
@@ -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 =
|
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
|
+
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:
|
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/
|
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.
|
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
|