rails_api_logger 0.11.0 → 0.12.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: 2583006d0378e6989e3da41641c44fd31c9ff89c705150389c3da33bb7d194da
4
- data.tar.gz: 7c747c7058c67fa855ce16a92f97c9d4a6d87066e5cd96e6edecf6b08384e4d4
3
+ metadata.gz: 876eed123167d23e2eb7b6985b88633ac310e51fbc6322e0c0697bc391979173
4
+ data.tar.gz: 0abcc58ce8bd138c7497b30671283dec800740377aa11c83a6de79865720c82c
5
5
  SHA512:
6
- metadata.gz: bc76a1c027e3e1271e9475bc54ac2a64a3bcab962d6ef67d20c0500bb221ece0af56085e982317bb17802d65b5117eba76c5956745899ac3a152a32d715edff3
7
- data.tar.gz: b90b9f94f8e1a742ed084687151649e3a9bbcf962beeacdfad8c211c9dfcd3a1f31d31d3f26f016f07e40a6e67133e2d5d557f8a1c04c75ee258de076259b180
6
+ metadata.gz: dd4a44520b68a7b670c667d1d0783b2726f32603d4d738850e3da5da9abd7ed9799b7190cbe38e21efada2c642becccf215e7c90ede41d3613d74578a0ddf802
7
+ data.tar.gz: a130063a418cf202c6715bedac8d3c6298fa673272dd1153a6cbaf3e0d3e7a407cc667dab41109cada36ce6cb156e22d70729223ae5e5eb9c7fb8f18f61b85d2
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.2.9
1
+ 3.2.11
data/CHANGELOG.md CHANGED
@@ -1,6 +1,25 @@
1
+ # 0.12.0
2
+
3
+ * Add `client_reference` column to both `inbound_request_logs` and `outbound_request_logs`. This is a free-form
4
+ indexed string you can set on a log entry to correlate it with any external identifier (similar in spirit to
5
+ `loggable`, but without a polymorphic association).
6
+ * For outbound requests, pass it to the logger: `RailsApiLogger::Logger.new(client_reference: "abc-123")`.
7
+ * For inbound requests, call `attach_inbound_request_client_reference("abc-123")` from your controller.
8
+
9
+ To upgrade an existing app, run:
10
+
11
+ ```
12
+ bin/rails g rails_api_logger:upgrade_client_reference
13
+ bin/rails db:migrate
14
+ ```
15
+
16
+ # 0.11.1
17
+
18
+ * Remove deprecated "require_dependency"
19
+
1
20
  # 0.11.0
2
21
 
3
- * Fix stack trace to deep in webrick
22
+ * Fix stack trace too deep in webrick
4
23
  * Fixes for Rails < 7.1
5
24
  * Support custom primary key types in migration template
6
25
 
data/README.md CHANGED
@@ -174,6 +174,29 @@ to be able to access the inbound logs attached to the model.
174
174
 
175
175
  You also have `has_many_outbound_request_logs` and `has_many_request_logs` that includes both.
176
176
 
177
+ ### Client reference
178
+
179
+ Both `inbound_request_logs` and `outbound_request_logs` have a `client_reference` column: a free-form indexed
180
+ string you can set on a log entry to correlate it with any external identifier (e.g. a request id coming from an
181
+ upstream system, an idempotency key, a tenant slug). It is independent from the `loggable` polymorphic association.
182
+
183
+ For inbound requests, attach it from your controller:
184
+
185
+ ```ruby
186
+ def create
187
+ attach_inbound_request_client_reference(request.headers["X-Request-Id"])
188
+ # ...
189
+ end
190
+ ```
191
+
192
+ For outbound requests, pass it when instantiating the logger:
193
+
194
+ ```ruby
195
+ RailsApiLogger::Logger.new(client_reference: "ref-abc").call(uri, request) do
196
+ http.request(request)
197
+ end
198
+ ```
199
+
177
200
  ## RailsAdmin integration
178
201
 
179
202
  We provide here some code samples to integrate the models in [RailsAdmin](https://github.com/sferik/rails_admin).
@@ -7,4 +7,9 @@ module InboundRequestsLogger
7
7
  return unless request.env["INBOUND_REQUEST_LOG"].present?
8
8
  request.env["INBOUND_REQUEST_LOG"].update(loggable: loggable) if loggable&.persisted?
9
9
  end
10
+
11
+ def attach_inbound_request_client_reference(client_reference)
12
+ return unless request.env["INBOUND_REQUEST_LOG"].present?
13
+ request.env["INBOUND_REQUEST_LOG"].update(client_reference: client_reference)
14
+ end
10
15
  end
@@ -1,13 +1,15 @@
1
1
  module RailsApiLogger
2
2
  class Logger
3
- def initialize(loggable = nil, skip_request_body: false, skip_response_body: false)
3
+ def initialize(loggable = nil, client_reference: nil, skip_request_body: false, skip_response_body: false)
4
4
  @loggable = loggable
5
+ @client_reference = client_reference
5
6
  @skip_request_body = skip_request_body
6
7
  @skip_response_body = skip_response_body
7
8
  end
8
9
 
9
10
  def call(url, request)
10
- log = OutboundRequestLog.from_request(request, loggable: @loggable, skip_request_body: @skip_request_body)
11
+ log = OutboundRequestLog.from_request(request, loggable: @loggable, client_reference: @client_reference,
12
+ skip_request_body: @skip_request_body)
11
13
  yield.tap do |response|
12
14
  log.from_response(response, skip_response_body: @skip_response_body)
13
15
  end
@@ -19,7 +19,7 @@ module RailsApiLogger
19
19
  validates :method, presence: true
20
20
  validates :path, presence: true
21
21
 
22
- def self.from_request(request, loggable: nil, skip_request_body: false)
22
+ def self.from_request(request, loggable: nil, client_reference: nil, skip_request_body: false)
23
23
  if skip_request_body
24
24
  body = "[Skipped]"
25
25
  else
@@ -31,7 +31,8 @@ module RailsApiLogger
31
31
  body
32
32
  end
33
33
  end
34
- create(path: request.path, request_body: body, method: request.method, started_at: Time.current, loggable: loggable)
34
+ create(path: request.path, request_body: body, method: request.method, started_at: Time.current,
35
+ loggable: loggable, client_reference: client_reference)
35
36
  end
36
37
 
37
38
  def from_response(response, skip_response_body: false)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- rails_api_logger (0.10.1)
4
+ rails_api_logger (0.11.0)
5
5
  activejob (>= 6.0)
6
6
  activerecord (>= 6.0)
7
7
  nokogiri
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- rails_api_logger (0.10.1)
4
+ rails_api_logger (0.11.0)
5
5
  activejob (>= 6.0)
6
6
  activerecord (>= 6.0)
7
7
  nokogiri
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- rails_api_logger (0.10.1)
4
+ rails_api_logger (0.11.0)
5
5
  activejob (>= 6.0)
6
6
  activerecord (>= 6.0)
7
7
  nokogiri
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- rails_api_logger (0.10.1)
4
+ rails_api_logger (0.11.0)
5
5
  activejob (>= 6.0)
6
6
  activerecord (>= 6.0)
7
7
  nokogiri
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- rails_api_logger (0.10.1)
4
+ rails_api_logger (0.11.0)
5
5
  activejob (>= 6.0)
6
6
  activerecord (>= 6.0)
7
7
  nokogiri
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- rails_api_logger (0.10.1)
4
+ rails_api_logger (0.11.0)
5
5
  activejob (>= 6.0)
6
6
  activerecord (>= 6.0)
7
7
  nokogiri
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/base"
4
+ require "rails/generators/migration"
5
+
6
+ module RailsApiLogger
7
+ module Generators
8
+ class UpgradeClientReferenceGenerator < Rails::Generators::Base
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path("../../templates", __FILE__)
12
+
13
+ def self.next_migration_number(dirname)
14
+ next_migration_number = current_migration_number(dirname) + 1
15
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
16
+ end
17
+
18
+ desc "Add the client_reference column to the rails_api_logger tables."
19
+ def copy_migration
20
+ migration_template "add_client_reference_to_rails_api_logger_tables.rb",
21
+ "db/migrate/add_client_reference_to_rails_api_logger_tables.rb"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ class AddClientReferenceToRailsApiLoggerTables < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ def change
3
+ add_column :inbound_request_logs, :client_reference, :string
4
+ add_index :inbound_request_logs, :client_reference
5
+
6
+ add_column :outbound_request_logs, :client_reference, :string
7
+ add_index :outbound_request_logs, :client_reference
8
+ end
9
+ end
@@ -10,6 +10,7 @@ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migr
10
10
  t.timestamp :started_at
11
11
  t.timestamp :ended_at
12
12
  t.references :loggable, index: true, polymorphic: true<%= ", type: :#{pk_type}" if pk_type %>
13
+ t.string :client_reference, index: true
13
14
  t.timestamps null: false
14
15
  end
15
16
 
@@ -22,6 +23,7 @@ class CreateRailsApiLoggerTable < ActiveRecord::Migration[<%= ActiveRecord::Migr
22
23
  t.timestamp :started_at
23
24
  t.timestamp :ended_at
24
25
  t.references :loggable, index: true, polymorphic: true<%= ", type: :#{pk_type}" if pk_type %>
26
+ t.string :client_reference, index: true
25
27
  t.timestamps null: false
26
28
  end
27
29
  end
@@ -15,7 +15,7 @@ module RailsApiLogger
15
15
  end
16
16
 
17
17
  ActiveSupport.on_load(:action_controller) do
18
- require_dependency "inbound_requests_logger"
18
+ require_relative "../../app/controllers/inbound_requests_logger"
19
19
  ActionController::Base.include ::InboundRequestsLogger
20
20
  end
21
21
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsApiLogger
4
- VERSION = "0.11.0"
4
+ VERSION = "0.12.0"
5
5
  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.11.0
4
+ version: 0.12.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: 2025-10-29 00:00:00.000000000 Z
11
+ date: 2026-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -234,6 +234,8 @@ files:
234
234
  - gemfiles/rails_webrick_8.0.gemfile
235
235
  - gemfiles/rails_webrick_8.0.gemfile.lock
236
236
  - lib/generators/rails_api_logger/install_generator.rb
237
+ - lib/generators/rails_api_logger/upgrade_client_reference_generator.rb
238
+ - lib/generators/templates/add_client_reference_to_rails_api_logger_tables.rb.tt
237
239
  - lib/generators/templates/create_rails_api_logger_table.rb.tt
238
240
  - lib/rails_api_logger.rb
239
241
  - lib/rails_api_logger/engine.rb
@@ -261,7 +263,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
261
263
  - !ruby/object:Gem::Version
262
264
  version: '0'
263
265
  requirements: []
264
- rubygems_version: 3.4.6
266
+ rubygems_version: 3.4.19
265
267
  signing_key:
266
268
  specification_version: 4
267
269
  summary: "Log API requests like a king \U0001F451"