faraday-logging-color_formatter 0.1.1 → 0.2.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: f4fb2e825f76429ca21a14707249222982943bfb5d9c29d2ccfeb43fe719a918
4
- data.tar.gz: 53d0dea6b30dba614ecd1d2babd6fdccd338d39032d3823d59fd96eaa8abd595
3
+ metadata.gz: 1253416d51b114229d35aa77e5ebd0529f5107919b41629dd292bfa43d23ce8f
4
+ data.tar.gz: cd4d8864d2ed7daf33dc5e1a2eb1efc5af2a2002c831bcaa741529c36fd1a290
5
5
  SHA512:
6
- metadata.gz: aab013179d7416163d496c558a084fa9712efe5d4208daffd639b05fef1564342c29e7409ff3e4a301828877615ffe833b54e429cee2674009165055761d08a4
7
- data.tar.gz: d3d58febcbba7a709d0ee95f087bd6fef65a7c49d600307760290b6ecb2cb37553b46fbca35c34b4d33deae62fe22a073bb7e9963c8c2504d93c7682b072d9c4
6
+ metadata.gz: fc451459e650541b1f3f8a1b4f4c4b48df34c4b86d5a10f941bdd8bbe196bc4b713f5e8a0213b463dd91447fde2e7aa6132bb8ba2868873716c44343f088d85c
7
+ data.tar.gz: 98440c5596cf19c8e346ebd80c2cda383f52618c2dc67ae1f5e06acd48b86dc5fb1ee1c6cb943b9c8086bc6dde527ce5d45ee9335a382e82ef8b0bfe5628906a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.2.0
4
+ #### 2023-01-19
5
+ * Allow to change log message indentation and request response messages using a new `prefix` option to the logger middleware.
6
+
3
7
  ## 0.1.1
4
8
  #### 2023-01-17
5
9
  * Gemspec homepage URI update to point to RubyGems.
data/README.md CHANGED
@@ -34,6 +34,12 @@ And then add the `Faraday::Logging::ColorFormatter` formatter to your logger mid
34
34
  conn.response :logger, ActiveSupport::Logger.new($stdout), formatter: Faraday::Logging::ColorFormatter
35
35
  end
36
36
 
37
+ If you want to replace the default `HTTP Request` and `HTTP Response` prefixes or adjust the indentation, pass in a `prefix` hash with your own values.
38
+
39
+ connection = Faraday.new(url: 'http://httpbingo.org') do |conn|
40
+ conn.response :logger, ActiveSupport::Logger.new($stdout), formatter: Faraday::Logging::ColorFormatter, prefix: { request: 'request', response: 'response', indent: 2 }
41
+ end
42
+
37
43
  ## Development
38
44
 
39
45
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests and `rake rubocop` to run the linters. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -8,11 +8,17 @@ module Faraday
8
8
  class Formatter < Faraday::Logging::Formatter
9
9
  ANSI_COLORS = { red: "\e[31m", green: "\e[32m", yellow: "\e[33m", blue: "\e[34m", magenta: "\e[35m", cyan: "\e[36m", reset: "\e[0m" }.freeze
10
10
 
11
+ def initialize(logger:, options:)
12
+ @prefix = { request: 'HTTP Request', response: 'HTTP Response', indent: 0 }.merge(options.delete(:prefix) || {})
13
+
14
+ super
15
+ end
16
+
11
17
  def request(env)
12
- http_request_term = in_color(:blue) { 'HTTP Request' }
18
+ http_request_term = prefix[:request].nil? || prefix[:request].strip.empty? ? nil : in_color(:blue) { "#{prefix[:request]} " }
13
19
  http_request_log = in_color(request_log_color(env.method)) { "#{env.method.upcase} #{apply_filters(env.url.to_s)}" }
14
20
 
15
- request_log = proc { "#{http_request_term} #{http_request_log}" }
21
+ request_log = proc { "#{' ' * prefix[:indent]}#{http_request_term}#{http_request_log}" }
16
22
  public_send(log_level, &request_log)
17
23
 
18
24
  log_headers(nil, env.request_headers) if log_headers?(:request)
@@ -20,10 +26,10 @@ module Faraday
20
26
  end
21
27
 
22
28
  def response(env)
23
- http_response_term = in_color(:blue) { 'HTTP Response' }
29
+ http_response_term = prefix[:response].nil? || prefix[:response].strip.empty? ? nil : in_color(:blue) { "#{prefix[:response]} " }
24
30
  http_response_log = in_color(response_log_color(env.status)) { "Status #{env.status}" }
25
31
 
26
- status = proc { "#{http_response_term} #{http_response_log}" }
32
+ status = proc { "#{' ' * prefix[:indent]}#{http_response_term}#{http_response_log}" }
27
33
  public_send(log_level, &status)
28
34
 
29
35
  log_headers(nil, env.response_headers) if log_headers?(:response)
@@ -32,6 +38,8 @@ module Faraday
32
38
 
33
39
  private
34
40
 
41
+ attr_reader :prefix
42
+
35
43
  def in_color(color)
36
44
  "#{ANSI_COLORS[color]}#{yield}#{ANSI_COLORS[:reset]}"
37
45
  end
@@ -3,7 +3,7 @@
3
3
  module Faraday
4
4
  module Logging
5
5
  module ColorFormatter
6
- VERSION = '0.1.1'
6
+ VERSION = '0.2.0'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-logging-color_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kobus Joubert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-17 00:00:00.000000000 Z
11
+ date: 2023-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday