Sha256: e3f44cdcd028647f9de3c0f742ec85d989443e103ab1611e088ba4eb841a7ba4

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require 'logger'

module FinAppsCore
  module Middleware
    class CustomLogger < Faraday::Response::Middleware
      extend Forwardable
      include FinAppsCore::Utils::ParameterFilter

      DEFAULT_OPTIONS = {bodies: false}.freeze

      def initialize(app, logger=nil, options={})
        super(app)
        @logger = logger || new_logger
        @options = DEFAULT_OPTIONS.merge(options)
      end

      def_delegators :@logger, :debug

      def call(env)
        debug "#{self.class.name}##{__method__} => URL: #{env.method.upcase} #{env.url}"
        debug "#{self.class.name}##{__method__} => Request Headers: #{dump env.request_headers}"
        super
      end

      def on_complete(env)
        debug "#{self.class.name}##{__method__} => Response Headers: #{dump env.response_headers}"
        debug "#{self.class.name}##{__method__} => Response Body: #{dump env.body}" if env.body
      end

      private

      def dump(value)
        s = skip_sensitive_data(value.is_a?(Array) ? value.to_h : value)
        s.nil? ? 'NO-CONTENT' : s.to_json
      end

      def new_logger
        logger = Logger.new(STDOUT)
        logger.level = FinAppsCore::REST::Defaults::DEFAULTS[:log_level]
        logger
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
finapps_core-3.0.3 lib/finapps_core/middleware/response/custom_logger.rb
finapps_core-3.0.1 lib/finapps_core/middleware/response/custom_logger.rb
finapps_core-2.1.2 lib/finapps_core/middleware/response/custom_logger.rb