Class: Fbe::Middleware::Formatter

Inherits:
Faraday::Logging::Formatter
  • Object
show all
Defined in:
lib/fbe/middleware/formatter.rb

Overview

Faraday logging formatter show verbose log for only error response

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2025 Zerocracy

License

MIT

Instance Method Summary collapse

Instance Method Details

#request(http) ⇒ Object

Log HTTP request.

Parameters:

  • http (Hash)

    The hash with data about HTTP request



19
20
21
# File 'lib/fbe/middleware/formatter.rb', line 19

def request(http)
  @req = http
end

#response(http) ⇒ Object

Log HTTP response.

Parameters:

  • http (Hash)

    The hash with data about HTTP response



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fbe/middleware/formatter.rb', line 26

def response(http)
  return if http.status < 400
  if http.status == 403 && http.response_headers['content-type'].start_with?('application/json')
    warn(
      [
        "#{@req.method.upcase} #{apply_filters(@req.url.to_s)}",
        '->',
        http.status,
        '/',
        JSON.parse(http.response_body)['message']
      ].join(' ')
    )
    return
  end
  error(
    [
      "#{@req.method.upcase} #{apply_filters(@req.url.to_s)} HTTP/1.1",
      shifted(apply_filters(dump_headers(@req.request_headers))),
      '',
      shifted(apply_filters(@req.request_body)),
      "HTTP/1.1 #{http.status}",
      shifted(apply_filters(dump_headers(http.response_headers))),
      '',
      shifted(apply_filters(http.response_body))
    ].join("\n")
  )
end