rails_api_logger 0.7.0 → 0.8.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: b99069d517d5cd82e5e073121ebb94306335c052644115cc56765a0a3c166447
4
- data.tar.gz: bfac0a6e391ff5c1e29e481e313c47419a6b6f4835871e9f12624638b38d497e
3
+ metadata.gz: bab320d5ee753c378838802e7213986a2359270f4f2a7ee89d3a1b2838bee6b3
4
+ data.tar.gz: 55c1d432d0df101d1c3b1a4ecd2ce44a3ad2dbd4916ef53a284a5d39ce98ed1e
5
5
  SHA512:
6
- metadata.gz: 3b97315feb5e4792465a85819c829d33733a854fe77a9af907935058eb468c44829f524a29fb6c969cef6efe342903bcd6384989720bd54180f274fbde2bb0c7
7
- data.tar.gz: 7139532155a71e2f9d068438164425acebb9f5bb6825f6fbdac0a5f0d0b0f58204dd824b7dcfd474776133b2c4e616a6b7834bd40a94f6500d845c885d45f326
6
+ metadata.gz: f4c6e644ae4091d0a814227208f669dd992e86a6b77f27c419fb3b6420ba247816196f2cf3e601ddf5f00b4fac3b3ce026b41e5b0cb3b21713302984a1aa90ee
7
+ data.tar.gz: 770630cb4413b5732fb7326d8a387be4937df558e04660b80920b5252f789376ecef515bc10d874eccf21c1b41235643f4bfcc32c6b8a15d9e095a5477532a1e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 0.8.0
2
+ * 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.
3
+
1
4
  # 0.7.0
2
5
  * Fix an issue in the middleware where the request body was not read correctly if there were encoding issues.
3
6
  * Improved documentation about outboud request logging.
@@ -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.0"
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.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: 2023-12-15 00:00:00.000000000 Z
11
+ date: 2024-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties