rails_api_logger 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.semaphore/semaphore.yml +2 -1
- data/README.md +2 -8
- data/lib/generators/rails_api_logger/install_generator.rb +1 -1
- data/lib/rails_api_logger/inbound_requests_logger_middleware.rb +1 -1
- data/lib/rails_api_logger/request_log.rb +13 -2
- data/lib/rails_api_logger.rb +14 -12
- data/rails_api_logger.gemspec +2 -3
- metadata +20 -8
- 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: 545d9a066433986966d131cd6f874fbe58607fe03f2aae947eec494815e2bc60
|
4
|
+
data.tar.gz: 8565825afc3af8704877c534df8e7f334a3d90ddedca4813879a89846e5b745c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1334657ab5a74509d0a5be48e344b439038207577c80e6ced192043b13fb6005804a2b1275c195b813f5993ee104d0e8ed6b2f8dc575b038a4b5728dcb31f4aa
|
7
|
+
data.tar.gz: 9ca93777e0035586de21dbfb19bac23e01b235a1556d78f824a88a2f191a61938e6fc12d44e4990a1de3b546a52d04006591641e4e053229d87d0e326b917316
|
data/.semaphore/semaphore.yml
CHANGED
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.
|
@@ -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}
|
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.5.0"
|
6
4
|
spec.authors = ["Alessandro Rodi"]
|
7
5
|
spec.email = ["alessandro.rodi@renuo.ch"]
|
8
6
|
|
@@ -28,6 +26,7 @@ Gem::Specification.new do |spec|
|
|
28
26
|
spec.add_dependency "railties", ">= 4.1.0"
|
29
27
|
spec.add_dependency "activerecord", ">= 4.1.0"
|
30
28
|
spec.add_dependency "nokogiri"
|
29
|
+
spec.add_dependency "zeitwerk", ">= 2.0.0"
|
31
30
|
|
32
31
|
spec.add_development_dependency "sqlite3", "~> 1.4.0"
|
33
32
|
spec.add_development_dependency "standard", "~> 0.13.0"
|
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.5.0
|
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-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: zeitwerk
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.0.0
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: sqlite3
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -135,9 +149,7 @@ files:
|
|
135
149
|
- lib/rails_api_logger/inbound_requests_logger.rb
|
136
150
|
- lib/rails_api_logger/inbound_requests_logger_middleware.rb
|
137
151
|
- lib/rails_api_logger/outbound_request_log.rb
|
138
|
-
- lib/rails_api_logger/rails_api_logger_rails_admin_configuration.rb
|
139
152
|
- lib/rails_api_logger/request_log.rb
|
140
|
-
- lib/rails_api_logger/version.rb
|
141
153
|
- rails_api_logger.gemspec
|
142
154
|
homepage: https://github.com/renuo/rails_api_logger
|
143
155
|
licenses:
|
@@ -146,7 +158,7 @@ metadata:
|
|
146
158
|
homepage_uri: https://github.com/renuo/rails_api_logger
|
147
159
|
source_code_uri: https://github.com/renuo/rails_api_logger
|
148
160
|
changelog_uri: https://github.com/renuo/rails_api_logger/CHANGELOG.md
|
149
|
-
post_install_message:
|
161
|
+
post_install_message:
|
150
162
|
rdoc_options: []
|
151
163
|
require_paths:
|
152
164
|
- lib
|
@@ -161,8 +173,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
173
|
- !ruby/object:Gem::Version
|
162
174
|
version: '0'
|
163
175
|
requirements: []
|
164
|
-
rubygems_version: 3.
|
165
|
-
signing_key:
|
176
|
+
rubygems_version: 3.1.4
|
177
|
+
signing_key:
|
166
178
|
specification_version: 4
|
167
179
|
summary: "Log API requests like a king \U0001F451"
|
168
180
|
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
|