Class: ApiHammer::Faraday::RequestLogger
- Includes:
- Term::ANSIColor
- Defined in:
- lib/api_hammer/faraday/request_logger.rb
Overview
Faraday middleware for logging.
two lines:
- an info line, colored prettily to show a brief summary of the request and response
- a debug line of json to record all relevant info. this is a lot of stuff jammed into one line, not pretty, but informative.
Instance Method Summary collapse
- #call(request_env) ⇒ Object
-
#initialize(app, logger, options = {}) ⇒ RequestLogger
constructor
A new instance of RequestLogger.
Constructor Details
#initialize(app, logger, options = {}) ⇒ RequestLogger
Returns a new instance of RequestLogger.
25 26 27 28 29 |
# File 'lib/api_hammer/faraday/request_logger.rb', line 25 def initialize(app, logger, ={}) @app = app @logger = logger @options = end |
Instance Method Details
#call(request_env) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/api_hammer/faraday/request_logger.rb', line 31 def call(request_env) began_at = Time.now = Thread.current[:activesupport_tagged_logging_tags] = .dup if && .any? request_body = request_env[:body].dup if request_env[:body] @app.call(request_env).on_complete do |response_env| now = Time.now status_color = case response_env.status.to_i when 200..299 :intense_green when 400..499 :intense_yellow when 500..599 :intense_red else :white end status_s = bold(send(status_color, response_env.status.to_s)) data = { 'request' => { 'method' => request_env[:method], 'uri' => request_env[:url].normalize.to_s, 'headers' => request_env.request_headers, 'body' => request_body, }.reject{|k,v| v.nil? }, 'response' => { 'status' => response_env.status, 'headers' => response_env.response_headers, 'body' => response_env.body, }.reject{|k,v| v.nil? }, 'processing' => { 'began_at' => began_at.utc.to_i, 'duration' => now - began_at, 'activesupport_tagged_logging_tags' => @log_tags, }.reject{|k,v| v.nil? }, } json_data = JSON.dump(data) dolog = proc do now_s = now.strftime('%Y-%m-%d %H:%M:%S %Z') @logger.info "#{bold(intense_magenta('>'))} #{status_s} : #{bold(intense_magenta(request_env[:method].to_s.upcase))} #{intense_magenta(request_env[:url].normalize.to_s)} @ #{intense_magenta(now_s)}" @logger.info json_data end # reapply log tags from the request if they are not applied if @logger.respond_to?(:tagged) && && Thread.current[:activesupport_tagged_logging_tags] != @logger.tagged(, &dolog) else dolog.call end end end |