Sha256: d9ea3938c402c0bcb2cecacf117a6237941fb67629111b0cba44c957f6a9c0b0

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 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}"
        if env[:body] && log_body?(:request)
          debug "#{self.class.name}##{__method__} => Request Response: #{dump env[:body]}"
        end
        super
      end

      def on_complete(env)
        debug "#{self.class.name}##{__method__} => Response Headers: #{dump env.response_headers}"
        if env.body && log_body?(:response)
          debug "#{self.class.name}##{__method__} => Response Body: #{dump env.body}"
        end
      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 log_body?(type)
        case @options[:bodies]
        when Hash then
          @options[:bodies][type]
        else
          @options[:bodies]
        end
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
finapps_core-3.0.6 lib/finapps_core/middleware/response/custom_logger.rb
finapps_core-3.0.5 lib/finapps_core/middleware/response/custom_logger.rb