rails_api_logger 0.2.4 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +23 -0
- data/Gemfile +0 -3
- data/README.md +5 -0
- data/lib/generators/templates/create_rails_api_logger_table.rb.tt +4 -0
- data/lib/rails_api_logger/inbound_requests_logger_middleware.rb +1 -1
- data/lib/rails_api_logger/request_log.rb +32 -4
- data/lib/rails_api_logger/version.rb +1 -1
- data/lib/rails_api_logger.rb +2 -0
- data/rails_api_logger.gemspec +5 -2
- metadata +45 -4
- data/Gemfile.lock +0 -121
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz: '
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '00489e11faa84622f3557560cf9d989b1fb36f6c6d18fdd41f1f1784e59893dd'
|
4
|
+
data.tar.gz: 54f3202bd8446cce3a32c97a2da3b2f881d317207c511bf1d9ada9250ac751d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 254e3a0054438fa416561f5e34155529f4860d255cb01e80c5ea5a5a65bac694b35bc6b88199775b1fa11bc6532981f6e514b517f703140020c6b9618284ce1e
|
7
|
+
data.tar.gz: bf5537dfcb5eec1c675b9eb68bee42a8700fa756d1728666c67075a77a9f99baf0f573b5090e36dd6e345c0d5471b923fbcaddd8f689c257e89c496e858722cd
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1 +1,24 @@
|
|
1
|
+
# 0.4.1
|
2
|
+
* Fixed the `.failed` scope.
|
3
|
+
|
4
|
+
# 0.4.0
|
5
|
+
* Added `started_at`, `ended_at` and `duration` methods.
|
6
|
+
|
7
|
+
Migrate your tables with:
|
8
|
+
|
9
|
+
```
|
10
|
+
add_column :inbound_request_logs, :started_at, :timestamp
|
11
|
+
add_column :inbound_request_logs, :ended_at, :timestamp
|
12
|
+
add_column :outbound_request_logs, :started_at, :timestamp
|
13
|
+
add_column :outbound_request_logs, :ended_at, :timestamp
|
14
|
+
```
|
15
|
+
|
16
|
+
|
17
|
+
# 0.3.0
|
18
|
+
* Added `formatted_request_body` and `formatted_response_body` methods.
|
19
|
+
|
20
|
+
# 0.2.0
|
21
|
+
* Switch to a middleware solution
|
22
|
+
|
23
|
+
# 0.1.0
|
1
24
|
* Initial release
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Rails API Logger
|
2
2
|
|
3
|
+
The simplest way to log API requests of your Rails application in your database.
|
4
|
+
|
3
5
|
The Rails API logger gem introduces a set of tools to log and debug API requests.
|
4
6
|
It works on two sides:
|
5
7
|
|
@@ -16,6 +18,8 @@ This gem creates two database tables to log the following information:
|
|
16
18
|
* **request_body** what was included in the request body
|
17
19
|
* **response_body** what was included in the response body
|
18
20
|
* **response_code** the HTTP response code of the request
|
21
|
+
* **started_at** when the request started
|
22
|
+
* **ended_at** when the request finished
|
19
23
|
|
20
24
|
## Installation
|
21
25
|
|
@@ -29,6 +33,7 @@ And then execute:
|
|
29
33
|
|
30
34
|
```bash
|
31
35
|
bundle install
|
36
|
+
spring stop # if it's running. otherwise it does not see the new generator
|
32
37
|
bundle exec rails generate rails_api_logger:install
|
33
38
|
bundle exec rails db:migrate
|
34
39
|
```
|
@@ -6,6 +6,8 @@ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migr
|
|
6
6
|
t.text :request_body
|
7
7
|
t.text :response_body
|
8
8
|
t.integer :response_code
|
9
|
+
t.timestamp :started_at
|
10
|
+
t.timestamp :ended_at
|
9
11
|
t.references :loggable, index: true, polymorphic: true
|
10
12
|
t.timestamps null: false
|
11
13
|
end
|
@@ -16,6 +18,8 @@ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migr
|
|
16
18
|
t.text :request_body
|
17
19
|
t.text :response_body
|
18
20
|
t.integer :response_code
|
21
|
+
t.timestamp :started_at
|
22
|
+
t.timestamp :ended_at
|
19
23
|
t.references :loggable, index: true, polymorphic: true
|
20
24
|
t.timestamps null: false
|
21
25
|
end
|
@@ -17,7 +17,7 @@ class InboundRequestLoggerMiddleware
|
|
17
17
|
end
|
18
18
|
status, headers, body = @app.call(env)
|
19
19
|
if logging
|
20
|
-
@inbound_request_log.update_columns(response_body: parsed_body(body), response_code: status)
|
20
|
+
@inbound_request_log.update_columns(response_body: parsed_body(body), response_code: status, ended_at: Time.current)
|
21
21
|
end
|
22
22
|
[status, headers, body]
|
23
23
|
end
|
@@ -6,19 +6,47 @@ class RequestLog < ActiveRecord::Base
|
|
6
6
|
|
7
7
|
belongs_to :loggable, optional: true, polymorphic: true
|
8
8
|
|
9
|
-
scope :failed, -> { where(
|
9
|
+
scope :failed, -> { where.not(response_code: 200..299) }
|
10
10
|
|
11
|
-
validates :request_body, presence: true
|
12
11
|
validates :method, presence: true
|
13
12
|
validates :path, presence: true
|
14
13
|
|
15
14
|
def self.from_request(request)
|
16
|
-
|
15
|
+
request_body = (request.body.respond_to?(:read) ? request.body.read : request.body)
|
16
|
+
body = request_body ? request_body.dup.force_encoding("UTF-8") : nil
|
17
17
|
begin
|
18
18
|
body = JSON.parse(body) if body.present?
|
19
19
|
rescue JSON::ParserError
|
20
20
|
body
|
21
21
|
end
|
22
|
-
create(path: request.path, request_body: body, method: request.method)
|
22
|
+
create(path: request.path, request_body: body, method: request.method, started_at: Time.current)
|
23
|
+
end
|
24
|
+
|
25
|
+
def formatted_request_body
|
26
|
+
formatted_body(request_body)
|
27
|
+
end
|
28
|
+
|
29
|
+
def formatted_response_body
|
30
|
+
formatted_body(response_body)
|
31
|
+
end
|
32
|
+
|
33
|
+
def formatted_body(body)
|
34
|
+
if body.is_a?(Hash)
|
35
|
+
JSON.pretty_generate(body)
|
36
|
+
else
|
37
|
+
xml = Nokogiri::XML(body)
|
38
|
+
if xml.errors.any?
|
39
|
+
body
|
40
|
+
else
|
41
|
+
xml.to_xml(indent: 2)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
rescue
|
45
|
+
body
|
46
|
+
end
|
47
|
+
|
48
|
+
def duration
|
49
|
+
return if started_at.nil? || ended_at.nil?
|
50
|
+
ended_at - started_at
|
23
51
|
end
|
24
52
|
end
|
data/lib/rails_api_logger.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "active_record"
|
2
|
+
require "nokogiri"
|
2
3
|
require "rails_api_logger/version"
|
3
4
|
require "rails_api_logger/request_log"
|
4
5
|
require "rails_api_logger/inbound_request_log"
|
@@ -20,6 +21,7 @@ module RailsApiLogger
|
|
20
21
|
log.response_body = {error: e.message}
|
21
22
|
raise
|
22
23
|
ensure
|
24
|
+
log.ended_at = Time.current
|
23
25
|
log.save!
|
24
26
|
end
|
25
27
|
end
|
data/rails_api_logger.gemspec
CHANGED
@@ -25,9 +25,12 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
26
|
spec.require_paths = ["lib"]
|
27
27
|
|
28
|
-
spec.add_dependency
|
29
|
-
spec.add_dependency
|
28
|
+
spec.add_dependency "railties", ">= 4.1.0"
|
29
|
+
spec.add_dependency "activerecord", ">= 4.1.0"
|
30
|
+
spec.add_dependency "nokogiri"
|
30
31
|
|
31
32
|
spec.add_development_dependency "sqlite3", "~> 1.4.0"
|
32
33
|
spec.add_development_dependency "standard", "~> 0.13.0"
|
34
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
36
|
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.4.1
|
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-11-09 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: nokogiri
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: sqlite3
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +80,34 @@ dependencies:
|
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: 0.13.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '12.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '12.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
69
111
|
description: Log inbound and outbound API requests in your Rails application
|
70
112
|
email:
|
71
113
|
- alessandro.rodi@renuo.ch
|
@@ -81,7 +123,6 @@ files:
|
|
81
123
|
- CHANGELOG.md
|
82
124
|
- CODE_OF_CONDUCT.md
|
83
125
|
- Gemfile
|
84
|
-
- Gemfile.lock
|
85
126
|
- LICENSE.txt
|
86
127
|
- README.md
|
87
128
|
- Rakefile
|
@@ -120,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
161
|
- !ruby/object:Gem::Version
|
121
162
|
version: '0'
|
122
163
|
requirements: []
|
123
|
-
rubygems_version: 3.
|
164
|
+
rubygems_version: 3.2.16
|
124
165
|
signing_key:
|
125
166
|
specification_version: 4
|
126
167
|
summary: "Log API requests like a king \U0001F451"
|
data/Gemfile.lock
DELETED
@@ -1,121 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
rails_api_logger (0.2.4)
|
5
|
-
activerecord (>= 4.1.0)
|
6
|
-
railties (>= 4.1.0)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: https://rubygems.org/
|
10
|
-
specs:
|
11
|
-
actionpack (6.1.3)
|
12
|
-
actionview (= 6.1.3)
|
13
|
-
activesupport (= 6.1.3)
|
14
|
-
rack (~> 2.0, >= 2.0.9)
|
15
|
-
rack-test (>= 0.6.3)
|
16
|
-
rails-dom-testing (~> 2.0)
|
17
|
-
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
18
|
-
actionview (6.1.3)
|
19
|
-
activesupport (= 6.1.3)
|
20
|
-
builder (~> 3.1)
|
21
|
-
erubi (~> 1.4)
|
22
|
-
rails-dom-testing (~> 2.0)
|
23
|
-
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
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
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
|
-
i18n (>= 1.6, < 2)
|
32
|
-
minitest (>= 5.1)
|
33
|
-
tzinfo (~> 2.0)
|
34
|
-
zeitwerk (~> 2.3)
|
35
|
-
ast (2.4.2)
|
36
|
-
builder (3.2.4)
|
37
|
-
concurrent-ruby (1.1.8)
|
38
|
-
crass (1.0.6)
|
39
|
-
diff-lcs (1.4.4)
|
40
|
-
erubi (1.10.0)
|
41
|
-
i18n (1.8.9)
|
42
|
-
concurrent-ruby (~> 1.0)
|
43
|
-
loofah (2.9.0)
|
44
|
-
crass (~> 1.0.2)
|
45
|
-
nokogiri (>= 1.5.9)
|
46
|
-
method_source (1.0.0)
|
47
|
-
minitest (5.14.4)
|
48
|
-
nokogiri (1.11.2-x86_64-darwin)
|
49
|
-
racc (~> 1.4)
|
50
|
-
parallel (1.20.1)
|
51
|
-
parser (3.0.0.0)
|
52
|
-
ast (~> 2.4.1)
|
53
|
-
racc (1.5.2)
|
54
|
-
rack (2.2.3)
|
55
|
-
rack-test (1.1.0)
|
56
|
-
rack (>= 1.0, < 3)
|
57
|
-
rails-dom-testing (2.0.3)
|
58
|
-
activesupport (>= 4.2.0)
|
59
|
-
nokogiri (>= 1.6)
|
60
|
-
rails-html-sanitizer (1.3.0)
|
61
|
-
loofah (~> 2.3)
|
62
|
-
railties (6.1.3)
|
63
|
-
actionpack (= 6.1.3)
|
64
|
-
activesupport (= 6.1.3)
|
65
|
-
method_source
|
66
|
-
rake (>= 0.8.7)
|
67
|
-
thor (~> 1.0)
|
68
|
-
rainbow (3.0.0)
|
69
|
-
rake (12.3.3)
|
70
|
-
regexp_parser (2.0.3)
|
71
|
-
rexml (3.2.4)
|
72
|
-
rspec (3.10.0)
|
73
|
-
rspec-core (~> 3.10.0)
|
74
|
-
rspec-expectations (~> 3.10.0)
|
75
|
-
rspec-mocks (~> 3.10.0)
|
76
|
-
rspec-core (3.10.0)
|
77
|
-
rspec-support (~> 3.10.0)
|
78
|
-
rspec-expectations (3.10.0)
|
79
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
80
|
-
rspec-support (~> 3.10.0)
|
81
|
-
rspec-mocks (3.10.0)
|
82
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
83
|
-
rspec-support (~> 3.10.0)
|
84
|
-
rspec-support (3.10.0)
|
85
|
-
rubocop (1.10.0)
|
86
|
-
parallel (~> 1.10)
|
87
|
-
parser (>= 3.0.0.0)
|
88
|
-
rainbow (>= 2.2.2, < 4.0)
|
89
|
-
regexp_parser (>= 1.8, < 3.0)
|
90
|
-
rexml
|
91
|
-
rubocop-ast (>= 1.2.0, < 2.0)
|
92
|
-
ruby-progressbar (~> 1.7)
|
93
|
-
unicode-display_width (>= 1.4.0, < 3.0)
|
94
|
-
rubocop-ast (1.4.1)
|
95
|
-
parser (>= 2.7.1.5)
|
96
|
-
rubocop-performance (1.9.2)
|
97
|
-
rubocop (>= 0.90.0, < 2.0)
|
98
|
-
rubocop-ast (>= 0.4.0)
|
99
|
-
ruby-progressbar (1.11.0)
|
100
|
-
sqlite3 (1.4.2)
|
101
|
-
standard (0.13.0)
|
102
|
-
rubocop (= 1.10.0)
|
103
|
-
rubocop-performance (= 1.9.2)
|
104
|
-
thor (1.1.0)
|
105
|
-
tzinfo (2.0.4)
|
106
|
-
concurrent-ruby (~> 1.0)
|
107
|
-
unicode-display_width (2.0.0)
|
108
|
-
zeitwerk (2.4.2)
|
109
|
-
|
110
|
-
PLATFORMS
|
111
|
-
ruby
|
112
|
-
|
113
|
-
DEPENDENCIES
|
114
|
-
rails_api_logger!
|
115
|
-
rake (~> 12.0)
|
116
|
-
rspec (~> 3.0)
|
117
|
-
sqlite3 (~> 1.4.0)
|
118
|
-
standard (~> 0.13.0)
|
119
|
-
|
120
|
-
BUNDLED WITH
|
121
|
-
2.1.4
|