Sha256: 1126a232c748765a3022e493427523c467006b945c7b4ade8eb05a4f5e34d2d4

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

module TelphinApi
  # Faraday middleware for logging requests and responses.
  #
  # It's behaviour depends on the logging options in the configuration.
  class Logger < Faraday::Response::Middleware
    # Creates a middleware instance.
    # The logger is set from `:logger` configuration option.
    def initialize(app)
      super(app)
      @logger = TelphinApi.logger
    end
    
    # Logs the request if needed.
    # @param [Hash] env Request data.
    def call(env)
      if TelphinApi.log_requests?
        @logger.debug "#{env[:method].to_s.upcase} #{env[:url].to_s}"
        @logger.debug "body: #{env[:body].inspect}" unless env[:method] == :get
      end
      
      super
    end
    
    # Logs the response (successful or not) if needed.
    # @param [Hash] env Response data.
    def on_complete(env)
      if env[:body].error?
        @logger.warn env[:raw_body] if TelphinApi.log_errors?
      else
        @logger.debug env[:raw_body] if TelphinApi.log_responses?
      end
    end
  end
  
  Faraday::Response.register_middleware telphin_logger: Logger
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
telphin_api-1.0.4 lib/telphin_api/logger.rb
telphin_api-1.0.3 lib/telphin_api/logger.rb
telphin_api-1.0.2 lib/telphin_api/logger.rb
telphin_api-1.0.1 lib/telphin_api/logger.rb
telphin_api-1.0.0 lib/telphin_api/logger.rb