rails_api_logger 0.2.2 → 0.4.0

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: 95eaa68780ef406351f3ea4e90fdb2534ce01c1e8b4013011415c35a4159fb7b
4
- data.tar.gz: e36c39dc1b5408012b924f3c2dbeab3546c561f7536449e831410a42f6b615ad
3
+ metadata.gz: 6a4cfc6305629f5d93ede15587bb8abc1f9218fa0f07fab94f3e8a9547c11609
4
+ data.tar.gz: f1c3dd646b07d8774833769d9d8417b32dae2a3ab6a1aa1297b041b6fbdf552f
5
5
  SHA512:
6
- metadata.gz: 6f7c662cec7314c18f751956a6e38824333e179f49be588cb3d9e7cf966e2f03c818953ced27f75e0fd373434675738c709ae49ac6a53592d1d1f3f34d7fc261
7
- data.tar.gz: f1084faf61c871106de684e1a35c7da1e0def0c45f15f99fac249582010105b8d9de7f4cebef1b206d8344666e5129322d0f21612ac925d424b6581545a4b254
6
+ metadata.gz: cba867c8dce17d890b8c6cb3b81e92c80c0c98f85804a1ab61fba669681a31bbd0acd1b7cba4f677600e55666317a75fe30e2d0210c6f1a9e0f81e74cb691316
7
+ data.tar.gz: e45d3c7dd24c05003dd76a03c2b0f34de8563f2c65611f332a3af9ad7230af5502687b1d110f7b8ce9b1c7271cc9987440a6d127f09713d9e6e382d276f1ce3e
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ Gemfile.lock
data/CHANGELOG.md CHANGED
@@ -1 +1,21 @@
1
+ # 0.4.0
2
+ * Added `started_at`, `ended_at` and `duration` methods.
3
+
4
+ Migrate your tables with:
5
+
6
+ ```
7
+ add_column :inbound_request_logs, :started_at, :timestamp
8
+ add_column :inbound_request_logs, :ended_at, :timestamp
9
+ add_column :outbound_request_logs, :started_at, :timestamp
10
+ add_column :outbound_request_logs, :ended_at, :timestamp
11
+ ```
12
+
13
+
14
+ # 0.3.0
15
+ * Added `formatted_request_body` and `formatted_response_body` methods.
16
+
17
+ # 0.2.0
18
+ * Switch to a middleware solution
19
+
20
+ # 0.1.0
1
21
  * Initial release
data/Gemfile CHANGED
@@ -2,6 +2,3 @@ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in rails_api_logger.gemspec
4
4
  gemspec
5
-
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
data/README.md CHANGED
@@ -29,6 +29,7 @@ And then execute:
29
29
 
30
30
  ```bash
31
31
  bundle install
32
+ spring stop # if it's running. otherwise it does not see the new generator
32
33
  bundle exec rails generate rails_api_logger:install
33
34
  bundle exec rails db:migrate
34
35
  ```
@@ -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
@@ -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
@@ -4,7 +4,7 @@ module InboundRequestsLogger
4
4
  private
5
5
 
6
6
  def attach_inbound_request_loggable(loggable)
7
- request.env["inbound_request_log"].loggable = loggable if loggable&.persisted?
7
+ request.env["inbound_request_log"].update(loggable: loggable) if loggable&.persisted?
8
8
  end
9
9
  end
10
10
 
@@ -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(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
@@ -8,17 +8,45 @@ class RequestLog < ActiveRecord::Base
8
8
 
9
9
  scope :failed, -> { where("response_code not like '2%'") }
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
- body = (request.body.respond_to?(:read) ? request.body.read : request.body).dup.force_encoding("UTF-8")
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
@@ -1,3 +1,3 @@
1
1
  module RailsApiLogger
2
- VERSION = "0.2.2"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -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("railties", ">= 4.1.0")
29
- spec.add_dependency("activerecord", ">= 4.1.0")
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.2.2
4
+ version: 0.4.0
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-03-08 00:00:00.000000000 Z
11
+ date: 2021-06-10 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.1.4
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.2)
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.1-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