rails_api_logger 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fda8a1c76ae8f88f8753f6a24b0e794031f2fe699cf517b2bdc0dc4a9492047
4
- data.tar.gz: 6435707109046e2f3444290e8f1c18381a2bbd3b9558602198752c077de3f7ed
3
+ metadata.gz: 2583006d0378e6989e3da41641c44fd31c9ff89c705150389c3da33bb7d194da
4
+ data.tar.gz: 7c747c7058c67fa855ce16a92f97c9d4a6d87066e5cd96e6edecf6b08384e4d4
5
5
  SHA512:
6
- metadata.gz: d8660e1388c86679bc3be3004049b783e96eef9bf18c6c61fb4d885fdd9cf4f2408bb131adf35193652687a3f37b2a7411e3e7953eb919fc468ab99a165e147c
7
- data.tar.gz: 53bbe981627e011159acaf23468f1b1bf00b6171ae9f50fc6d577d3461937a871e9955f29e391fdb38027902fd6db42a6a9b20614d6da21d50bacbf0bb2cc980
6
+ metadata.gz: bc76a1c027e3e1271e9475bc54ac2a64a3bcab962d6ef67d20c0500bb221ece0af56085e982317bb17802d65b5117eba76c5956745899ac3a152a32d715edff3
7
+ data.tar.gz: b90b9f94f8e1a742ed084687151649e3a9bbcf962beeacdfad8c211c9dfcd3a1f31d31d3f26f016f07e40a6e67133e2d5d557f8a1c04c75ee258de076259b180
data/.gitignore CHANGED
@@ -9,8 +9,11 @@
9
9
  /spec/dummy/log
10
10
  /spec/dummy/tmp
11
11
  /spec/dummy/db/*.sqlite3*
12
+ /spec/dummy/db/schema.rb
13
+ /spec/dummy/db/api_logger_schema.rb
14
+ /spec/tmp/*
12
15
  .env
13
-
16
+ .byebug_history
14
17
  # rspec failure tracking
15
18
  .rspec_status
16
19
  Gemfile.lock
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.2.1
1
+ 3.2.9
@@ -2,8 +2,8 @@ version: v1.0
2
2
  name: main-deploy
3
3
  agent:
4
4
  machine:
5
- type: e1-standard-2
6
- os_image: ubuntu2004
5
+ type: f1-standard-2
6
+ os_image: ubuntu2204
7
7
 
8
8
  blocks:
9
9
  - name: main-deploy
@@ -2,8 +2,8 @@ version: "v1.0"
2
2
  name: rails_api_logger
3
3
  agent:
4
4
  machine:
5
- type: e1-standard-2
6
- os_image: ubuntu2004
5
+ type: f1-standard-2
6
+ os_image: ubuntu2204
7
7
  auto_cancel:
8
8
  running:
9
9
  when: "true"
@@ -23,61 +23,153 @@ blocks:
23
23
  - bundle install -j 4
24
24
 
25
25
  - cache store
26
+ env_vars:
27
+ - name: BUNDLE_GEMFILE
28
+ value: /home/semaphore/rails_api_logger/gemfiles/rails_8.0.gemfile
26
29
  jobs:
27
30
  - name: linter
28
31
  commands:
29
32
  - bundle exec standardrb
30
- - name: tests sqlite separate db
33
+ - name: tests postgres separate db and separate target | rails 8.0
31
34
  env_vars:
35
+ - name: SAME_DB
36
+ value: "false"
37
+ - name: SAME_TARGET
38
+ value: "false"
32
39
  - name: TARGET_DB
33
- value: sqlite
40
+ value: postgres
34
41
  - name: RAILS_ENV
35
42
  value: test
36
43
  commands:
44
+ - sem-service start postgres 14
45
+ - bin/rails db:create db:migrate
37
46
  - bundle exec rspec
38
- - name: tests postgres separate db
47
+ - name: tests postgres separate db and same target | rails 8.0
39
48
  env_vars:
49
+ - name: SAME_DB
50
+ value: "false"
51
+ - name: SAME_TARGET
52
+ value: "true"
40
53
  - name: TARGET_DB
41
54
  value: postgres
42
55
  - name: RAILS_ENV
43
56
  value: test
44
57
  commands:
45
58
  - sem-service start postgres 14
46
- - bin/rails db:create db:schema:load
59
+ - bin/rails db:create db:migrate
47
60
  - bundle exec rspec
48
- - name: tests sqlite same db
61
+ - name: tests postgres same db and separate target | rails 8.0
49
62
  env_vars:
50
63
  - name: SAME_DB
51
64
  value: "true"
52
65
  - name: TARGET_DB
53
- value: sqlite
66
+ value: postgres
54
67
  - name: RAILS_ENV
55
68
  value: test
56
69
  commands:
70
+ - sem-service start postgres 14
71
+ - bin/rails db:create db:migrate
57
72
  - bundle exec rspec
58
- - name: tests postgres same db
73
+ - name: tests postgres separate db and separate target | rails 6.1
59
74
  env_vars:
60
75
  - name: SAME_DB
61
- value: "true"
76
+ value: "false"
77
+ - name: SAME_TARGET
78
+ value: "false"
62
79
  - name: TARGET_DB
63
80
  value: postgres
64
81
  - name: RAILS_ENV
65
82
  value: test
83
+ - name: BUNDLE_GEMFILE
84
+ value: /home/semaphore/rails_api_logger/gemfiles/rails_6.1.gemfile
66
85
  commands:
67
86
  - sem-service start postgres 14
68
- - bin/rails db:create db:schema:load
87
+ - bin/rails db:create db:migrate
69
88
  - bundle exec rspec
70
- - name: tests postgres same target
89
+ - name: tests postgres separate db and separate target | rails 7.0
71
90
  env_vars:
91
+ - name: SAME_DB
92
+ value: "false"
72
93
  - name: SAME_TARGET
73
- value: "true"
94
+ value: "false"
95
+ - name: TARGET_DB
96
+ value: postgres
97
+ - name: RAILS_ENV
98
+ value: test
99
+ - name: BUNDLE_GEMFILE
100
+ value: /home/semaphore/rails_api_logger/gemfiles/rails_7.0.gemfile
101
+ commands:
102
+ - sem-service start postgres 14
103
+ - bin/rails db:create db:migrate
104
+ - bundle exec rspec
105
+ - name: tests postgres separate db and separate target | rails 7.1
106
+ env_vars:
107
+ - name: SAME_DB
108
+ value: "false"
109
+ - name: SAME_TARGET
110
+ value: "false"
111
+ - name: TARGET_DB
112
+ value: postgres
113
+ - name: RAILS_ENV
114
+ value: test
115
+ - name: BUNDLE_GEMFILE
116
+ value: /home/semaphore/rails_api_logger/gemfiles/rails_7.1.gemfile
117
+ commands:
118
+ - sem-service start postgres 14
119
+ - bin/rails db:create db:migrate
120
+ - bundle exec rspec
121
+ - name: tests postgres separate db and separate target | rails 7.2
122
+ env_vars:
123
+ - name: SAME_DB
124
+ value: "false"
125
+ - name: SAME_TARGET
126
+ value: "false"
74
127
  - name: TARGET_DB
75
128
  value: postgres
76
129
  - name: RAILS_ENV
77
130
  value: test
131
+ - name: BUNDLE_GEMFILE
132
+ value: /home/semaphore/rails_api_logger/gemfiles/rails_7.2.gemfile
78
133
  commands:
79
134
  - sem-service start postgres 14
80
- - bin/rails db:create db:schema:load
135
+ - bin/rails db:create db:migrate
136
+ - bundle exec rspec
137
+ - name: tests sqlite separate db and separate target | rails 8.0
138
+ env_vars:
139
+ - name: SAME_DB
140
+ value: "false"
141
+ - name: SAME_TARGET
142
+ value: "false"
143
+ - name: TARGET_DB
144
+ value: sqlite
145
+ - name: RAILS_ENV
146
+ value: test
147
+ commands:
148
+ - bin/rails db:create db:migrate
149
+ - bundle exec rspec
150
+ - name: tests sqlite same db | rails 8.0
151
+ env_vars:
152
+ - name: SAME_DB
153
+ value: "true"
154
+ - name: TARGET_DB
155
+ value: sqlite
156
+ - name: RAILS_ENV
157
+ value: test
158
+ commands:
159
+ - bin/rails db:create db:migrate
160
+ - bundle exec rspec
161
+ - name: tests webrick | rails 8.0
162
+ env_vars:
163
+ - name: SAME_DB
164
+ value: "true"
165
+ - name: TARGET_DB
166
+ value: sqlite
167
+ - name: RAILS_ENV
168
+ value: test
169
+ - name: BUNDLE_GEMFILE
170
+ value: /home/semaphore/rails_api_logger/gemfiles/rails_webrick_8.0.gemfile
171
+ commands:
172
+ - bin/rails db:create db:migrate
81
173
  - bundle exec rspec
82
174
  promotions:
83
175
  - name: main
data/.standard.yml ADDED
@@ -0,0 +1,2 @@
1
+ ignore:
2
+ - 'gemfiles/**/*'
data/Appraisals ADDED
@@ -0,0 +1,51 @@
1
+ appraise "rails-6.1" do
2
+ gem "concurrent-ruby", "1.3.4"
3
+ gem "rails", "~> 6.1.0"
4
+ gem "rspec-rails", "~> 6.1.0"
5
+ gem "sqlite3", "~> 1.7.3"
6
+ gem "ammeter", "~> 1.1"
7
+ gem "webmock", "~> 3.25.0"
8
+ end
9
+
10
+ appraise "rails-7.0" do
11
+ gem "concurrent-ruby", "1.3.4"
12
+ gem "rails", "~> 7.0.0"
13
+ gem "rspec-rails", "~> 7.1.0"
14
+ gem "sqlite3", "~> 1.7.3"
15
+ gem "ammeter", "~> 1.1"
16
+ gem "webmock", "~> 3.25.0"
17
+ end
18
+
19
+ appraise "rails-7.1" do
20
+ gem "rails", "~> 7.1.0"
21
+ gem "rspec-rails", "~> 7.1.0"
22
+ gem "sqlite3", "~> 1.7.3"
23
+ gem "ammeter", "~> 1.1"
24
+ gem "webmock", "~> 3.25.0"
25
+ end
26
+
27
+ appraise "rails-7.2" do
28
+ gem "rails", "~> 7.2.0"
29
+ gem "rspec-rails", "~> 7.1.0"
30
+ gem "sqlite3", "~> 1.7.3"
31
+ gem "ammeter", "~> 1.1"
32
+ gem "webmock", "~> 3.25.0"
33
+ end
34
+
35
+ appraise "rails-8.0" do
36
+ gem "rails", "~> 8.0.0"
37
+ gem "rspec-rails", "~> 7.1.0"
38
+ gem "sqlite3", "~> 2.1.0"
39
+ gem "ammeter", "~> 1.1"
40
+ gem "puma"
41
+ gem "webmock", "~> 3.25.0"
42
+ end
43
+
44
+ appraise "rails-webrick-8.0" do
45
+ gem "rails", "~> 8.0.0"
46
+ gem "rspec-rails", "~> 7.1.0"
47
+ gem "sqlite3", "~> 2.1.0"
48
+ gem "ammeter", "~> 1.1"
49
+ gem "webrick", "~> 1.9.1"
50
+ gem "webmock", "~> 3.25.0"
51
+ end
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # 0.11.0
2
+
3
+ * Fix stack trace to deep in webrick
4
+ * Fixes for Rails < 7.1
5
+ * Support custom primary key types in migration template
6
+
7
+ # 0.10.1
8
+
9
+ * Fix a bug introduced in 0.8.1 where we fixed Rails 7.1 warnings. This broke the serialization in Rails < 7.1.
10
+ We now configure the serializer differently if you are running Rails 7.0 or lower.
11
+ If you used the gem version 0.8.1 or above in combination with Rails 7.0 or lower this caused the serialization of
12
+ the request and response body into YML format instead of JSON. This is now fixed and tests run on different rails
13
+ versions as well.
14
+
1
15
  # 0.10.0
2
16
 
3
17
  **BREAKING CHANGES**
@@ -12,7 +26,7 @@ This version contains many breaking changes. Consider this when upgrading:
12
26
  * `InboundRequestsLoggerMiddleware` has been renamed to `RailsApiLogger::Middleware`
13
27
 
14
28
  > Do the changes above and then continue with the following steps if you want to connect rails_api_logger to a different
15
- database:
29
+ > database:
16
30
 
17
31
  * Specify a database called `api_logger`. [Check here](spec/dummy/config/database.yml) for an example.
18
32
  * Check that everything still works (also in production!) with the new configuration.
@@ -23,8 +37,8 @@ database:
23
37
  * Add the following line into `production.rb`:
24
38
  `config.rails_api_logger.connects_to = { database: { writing: :api_logger } }` if you want to point to a new database.
25
39
 
26
- > If you are not on SQLite you can point also `api_logger` database to the current database you have, so you benefit from
27
- isolated transactions but don't need to create a new database or migrate data.
40
+ > If you are not on SQLite you can point also `api_logger` database to the current database you have, so you benefit
41
+ > from isolated transactions but don't need to create a new database or migrate data.
28
42
 
29
43
  ### List of changes in this version:
30
44
 
data/README.md CHANGED
@@ -54,9 +54,14 @@ config.rails_api_logger.connects_to = { database: { writing: :api_logger } }
54
54
  and [configure a new database](spec/dummy/config/database.yml) accordingly.
55
55
 
56
56
  > ⚠️ If you skip this step, rails_api_logger will use your primary database but a rollback will also rollback the
57
- > writing of logs
58
- > If you are not on SQLite you can point also `api_logger` to the same database! By doing so you can use a single
59
- > database but still guarantee the writing of logs in an isolated transaction.
57
+ > writing of logs.
58
+ >
59
+ > If you are not on SQLite you can simply point `api_logger` to the same database! By doing so you can use a single
60
+ > database but still guarantee the writing of logs in an isolated transaction:
61
+ > ```
62
+ > config.rails_api_logger.connects_to = { database: { writing: :primary } }
63
+ > ```
64
+ > This is probably what you want if you are using a single Postgres database.
60
65
 
61
66
  ## Log Outbound Requests
62
67
 
@@ -124,7 +129,7 @@ config.middleware.insert_before Rails::Rack::Logger, RailsApiLogger::Middleware,
124
129
  If you want to log only requests on a certain path, you can pass a regular expression:
125
130
 
126
131
  ```ruby
127
- config.middleware.insert_before Rails::Rack::Logger, RailsApiLogger::Middleware, path_regexp: /api/
132
+ config.middleware.insert_before Rails::Rack::Logger, RailsApiLogger::Middleware, path_regexp: %r{^/api}
128
133
  ```
129
134
 
130
135
  If you want to log only requests on a certain host, you can also use a regular expression:
@@ -200,11 +205,20 @@ This configuration will give you some nice views, and searches to work with the
200
205
  show do
201
206
  include_fields :loggable, :method, :path, :response_code
202
207
  field(:created_at)
208
+ field(:query_params) do
209
+ formatted_value do
210
+ params = bindings[:object].path.split("?").second
211
+ if params.present?
212
+ params = CGI.parse(params)
213
+ bindings[:view].tag.pre JSON.pretty_generate(params)
214
+ end
215
+ end
216
+ end
203
217
  field(:request_body) do
204
- formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].request_body)}</pre>".html_safe }
218
+ formatted_value { bindings[:view].tag.pre JSON.pretty_generate(bindings[:object].request_body) }
205
219
  end
206
220
  field(:response_body) do
207
- formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].response_body)}</pre>".html_safe }
221
+ formatted_value { bindings[:view].tag.pre JSON.pretty_generate(bindings[:object].response_body) }
208
222
  end
209
223
  end
210
224
  end
@@ -213,13 +227,56 @@ end
213
227
 
214
228
  ## Development
215
229
 
216
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
217
- also run `bin/console` for an interactive prompt that will allow you to experiment.
230
+ We use Appraisals to un on different Rails versions. This is a run example:
231
+
232
+ ```bash
233
+ export SAME_TARGET=false
234
+ export SAME_DB=false
235
+ export TARGET_DB=postgres
236
+ export BUNDLE_GEMFILE=/Users/alessandrorodi/RenuoWorkspace/rails_api_logger/gemfiles/rails_6.1.gemfile
237
+ bundle exec rails db:create db:migrate
238
+ bundle exec rspec
239
+ ```
240
+
241
+ These are the possible ENV variables:
242
+
243
+ * `SAME_TARGET` if true, the api_logger database is the same as the primary one. It will still use two separate
244
+ connection pools, but they'll point to the same database. This cannot be set to true if TARGET_DB is sqlite because
245
+ sqlite does not support multiple connection pools to the same database.
246
+ * `SAME_DB` if true, the api_logger uses the primary database. In this case SAME_TARGET is ignored.
247
+ * `TARGET_DB` the database to use. Can be `postgres`, `mysql`, or `sqlite`.
248
+ * `BUNDLE_GEMFILE` the gemfile to use. This is used to run the tests on different Rails versions.
249
+
250
+ Possible combinations:
251
+
252
+ | SAME_DB | SAME_TARGET | TARGET_DB | Description |
253
+ |---------|-------------|-----------|----------------------------------------------------------------------|
254
+ | false | false | postgres | Separate database, separate connection pool. |
255
+ | false | true | postgres | Same database, separate connection pool. |
256
+ | true | - | postgres | Same connection pool so the separate target is ignored. |
257
+ | false | false | sqlite | Separate database, separate connection pool. |
258
+ | false | true | sqlite | Not allowed. sqlite cannot have two connection pools to the same db. |
259
+ | true | - | sqlite | Same connection pool so the separate target is ignored. |
260
+
218
261
 
219
262
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
220
263
  version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
221
264
  push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
222
265
 
266
+ ### Start dummy app
267
+
268
+ You can spin up the dummy rails app with the following command:
269
+
270
+ ```bash
271
+ TARGET_DB=sqlite BUNDLE_GEMFILE=gemfiles/rails_8.0.gemfile bin/rails server
272
+ ```
273
+
274
+ and invoke the API endpoints. Here are some examples:
275
+
276
+ ```bash
277
+ curl -X POST http://localhost:3000/api/books -d '{"book": {"name": "John Doe"}}' -H "Content-Type: application/json"
278
+ ```
279
+
223
280
  ## Contributing
224
281
 
225
282
  Bug reports and pull requests are welcome on GitHub at https://github.com/renuo/rails_api_logger.
@@ -20,7 +20,7 @@ module RailsApiLogger
20
20
  logging = log?(env, request)
21
21
  if logging
22
22
  env["INBOUND_REQUEST_LOG"] = InboundRequestLog.from_request(request, skip_request_body: skip_request_body?(env))
23
- request.body.rewind if request.body.respond_to?(:read)
23
+ request.body.rewind if request.body.respond_to?(:rewind)
24
24
  end
25
25
  status, headers, body = @app.call(env)
26
26
  if logging
@@ -61,16 +61,17 @@ module RailsApiLogger
61
61
 
62
62
  def parsed_body(body)
63
63
  return unless body.present?
64
-
65
- if body.respond_to?(:to_ary)
66
- JSON.parse(body.to_ary[0])
64
+ parsable_body = if body.respond_to?(:to_ary)
65
+ body.to_ary[0]
67
66
  elsif body.respond_to?(:body)
68
- JSON.parse(body.body)
67
+ body.body
69
68
  else
70
69
  body
71
70
  end
71
+
72
+ JSON.parse(parsable_body)
72
73
  rescue JSON::ParserError, ArgumentError
73
- body
74
+ parsable_body
74
75
  end
75
76
 
76
77
  def request_with_state_change?(request)
@@ -4,8 +4,13 @@ module RailsApiLogger
4
4
 
5
5
  connects_to(**RailsApiLogger.connects_to) if RailsApiLogger.connects_to
6
6
 
7
- serialize :request_body, coder: JSON
8
- serialize :response_body, coder: JSON
7
+ if Gem::Version.new(Rails.version) >= Gem::Version.new("7.1")
8
+ serialize :request_body, coder: JSON
9
+ serialize :response_body, coder: JSON
10
+ else
11
+ serialize :request_body, JSON
12
+ serialize :response_body, JSON
13
+ end
9
14
 
10
15
  belongs_to :loggable, optional: true, polymorphic: true
11
16
 
data/bin/rails CHANGED
@@ -10,7 +10,12 @@ APP_PATH = File.expand_path('../spec/dummy/config/application', __dir__)
10
10
 
11
11
  # Set up gems listed in the Gemfile.
12
12
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
13
- require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
13
+ if File.exist?(ENV['BUNDLE_GEMFILE'])
14
+ require 'bundler/setup'
15
+ else
16
+ puts "Gemfile not found."
17
+ exit 1
18
+ end
14
19
 
15
20
  require 'rails/all'
16
21
  require 'rails/engine/commands'
@@ -0,0 +1,12 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "concurrent-ruby", "1.3.4"
6
+ gem "rails", "~> 6.1.0"
7
+ gem "rspec-rails", "~> 6.1.0"
8
+ gem "sqlite3", "~> 1.7.3"
9
+ gem "ammeter", "~> 1.1"
10
+ gem "webmock", "~> 3.25.0"
11
+
12
+ gemspec path: "../"