rails_api_logger 0.7.0 → 0.8.1

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: b99069d517d5cd82e5e073121ebb94306335c052644115cc56765a0a3c166447
4
- data.tar.gz: bfac0a6e391ff5c1e29e481e313c47419a6b6f4835871e9f12624638b38d497e
3
+ metadata.gz: 6a49586120262a132a0601faa23ace3fb929b04f115b900a54e3d86f68f04de9
4
+ data.tar.gz: 48e8cb9dcd6172532b96ac823910965102277bb10ec39956ee006a8bdbfc71b4
5
5
  SHA512:
6
- metadata.gz: 3b97315feb5e4792465a85819c829d33733a854fe77a9af907935058eb468c44829f524a29fb6c969cef6efe342903bcd6384989720bd54180f274fbde2bb0c7
7
- data.tar.gz: 7139532155a71e2f9d068438164425acebb9f5bb6825f6fbdac0a5f0d0b0f58204dd824b7dcfd474776133b2c4e616a6b7834bd40a94f6500d845c885d45f326
6
+ metadata.gz: 4468f3a723f56fd098cefe32fac6b937f63270636ac06fbd316920586516917b380120532c527d6cbd54d3808278eff28a633ad61aae5a4741a89a214d0411e2
7
+ data.tar.gz: ba10ec2838b308faad8b368ced02945a39663a34839f25b096760af9da6b108d316cf82669fd5dca5f9e2a3bccb2487bf6cb94ef84abdab5dd1743e4af7acc96
@@ -3,7 +3,7 @@ name: main-deploy
3
3
  agent:
4
4
  machine:
5
5
  type: e1-standard-2
6
- os_image: ubuntu1804
6
+ os_image: ubuntu2004
7
7
 
8
8
  blocks:
9
9
  - name: main-deploy
@@ -3,7 +3,7 @@ name: rails_api_logger
3
3
  agent:
4
4
  machine:
5
5
  type: e1-standard-2
6
- os_image: ubuntu1804
6
+ os_image: ubuntu2004
7
7
  auto_cancel:
8
8
  running:
9
9
  when: "true"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.8.1
2
+ * Fix Rails 7.1 warnings.
3
+
4
+ # 0.8.0
5
+ * Add option skip_body to skip the body for request responses. Use this option when you don't want to persist the response body. `[Skipped]` will be persisted instead. This is not a breaking change.
6
+
1
7
  # 0.7.0
2
8
  * Fix an issue in the middleware where the request body was not read correctly if there were encoding issues.
3
9
  * Improved documentation about outboud request logging.
data/README.md CHANGED
@@ -103,8 +103,8 @@ end
103
103
  OutboundRequestLog.include(OutboundRequestLogTransactionPatch)
104
104
  ```
105
105
 
106
- another way to do the same is to start a new thread to log the request.
107
- See the [linked test for an example](https://github.com/renuo/rails_api_logger/blob/main/spec/outbound_request_log_spec.rb:15)
106
+ You can also log the request in a separate thread to provoke the checkout of a separate database connection.
107
+ Have a look at [this example here](https://github.com/renuo/rails_api_logger/blob/28d4ced88fea5a5f4fd72f5a1db42ad4734eb547/spec/outbound_request_log_spec.rb#L28-L30).
108
108
 
109
109
  ## Log Inbound Requests
110
110
 
@@ -1,8 +1,8 @@
1
1
  class RequestLog < ActiveRecord::Base
2
2
  self.abstract_class = true
3
3
 
4
- serialize :request_body, JSON
5
- serialize :response_body, JSON
4
+ serialize :request_body, coder: JSON
5
+ serialize :response_body, coder: JSON
6
6
 
7
7
  belongs_to :loggable, optional: true, polymorphic: true
8
8
 
@@ -22,15 +22,9 @@ class RequestLog < ActiveRecord::Base
22
22
  create(path: request.path, request_body: body, method: request.method, started_at: Time.current, loggable: loggable)
23
23
  end
24
24
 
25
- def from_response(response)
25
+ def from_response(response, skip_body: false)
26
26
  self.response_code = response.code
27
- body = response.body&.dup&.force_encoding("UTF-8")
28
- begin
29
- body = JSON.parse(body) if body.present?
30
- rescue JSON::ParserError
31
- body
32
- end
33
- self.response_body = body
27
+ self.response_body = skip_body ? "[Skipped]" : manipulate_body(response.body)
34
28
  self
35
29
  end
36
30
 
@@ -61,4 +55,16 @@ class RequestLog < ActiveRecord::Base
61
55
  return if started_at.nil? || ended_at.nil?
62
56
  ended_at - started_at
63
57
  end
58
+
59
+ private
60
+
61
+ def manipulate_body(body)
62
+ body_duplicate = body&.dup&.force_encoding("UTF-8")
63
+ begin
64
+ body_duplicate = JSON.parse(body_duplicate) if body_duplicate.present?
65
+ rescue JSON::ParserError
66
+ body_duplicate
67
+ end
68
+ body_duplicate
69
+ end
64
70
  end
@@ -10,14 +10,15 @@ loader.setup
10
10
  class RailsApiLogger
11
11
  class Error < StandardError; end
12
12
 
13
- def initialize(loggable = nil)
13
+ def initialize(loggable = nil, skip_body: false)
14
14
  @loggable = loggable
15
+ @skip_body = skip_body
15
16
  end
16
17
 
17
18
  def call(url, request)
18
19
  log = OutboundRequestLog.from_request(request, loggable: @loggable)
19
20
  yield.tap do |response|
20
- log.from_response(response)
21
+ log.from_response(response, skip_body: @skip_body)
21
22
  end
22
23
  rescue => e
23
24
  log.response_body = {error: e.message}
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "rails_api_logger"
3
- spec.version = "0.7.0"
3
+ spec.version = "0.8.1"
4
4
  spec.authors = ["Alessandro Rodi"]
5
5
  spec.email = ["alessandro.rodi@renuo.ch"]
6
6
 
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.7.0
4
+ version: 0.8.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: 2023-12-15 00:00:00.000000000 Z
11
+ date: 2024-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties