Sha256: 1d65e601640804e8916e22ddf1b62f728b7284925cb604d59f8d4cea9f6d6de5

Contents?: true

Size: 1.44 KB

Versions: 15

Compression:

Stored size: 1.44 KB

Contents

require 'logger'

class LogicalModel
  module SafeLog

    def self.included(base)
      base.send(:include, InstanceMethods)
      base.send(:extend, ClassMethods)
    end

    module InstanceMethods
      def log_ok(response)
        self.class.log_ok(response)
      end

      def log_failed(response)
        self.class.log_failed(response)
      end

    end

    module ClassMethods
      attr_accessor :log_path

      def log_path
        @log_path ||= "log/logical_model.log"
      end

      def log_ok(response)
        self.logger.info("LogicalModel Log: #{response.code} #{mask_api_key(response.effective_url)} in #{response.time}s")
        self.logger.debug("LogicalModel Log RESPONSE: #{response.body}")
      end

      def log_failed(response)
        begin
          error_message = ActiveSupport::JSON.decode(response.body)["message"]
        rescue => e
          error_message = "error"
        end
        msg = "LogicalModel Log: #{response.code} #{mask_api_key(response.effective_url)} in #{response.time}s FAILED: #{error_message}"
        self.logger.warn(msg)
        self.logger.debug("LogicalModel Log RESPONSE: #{response.body}")
      end

      def logger
        @logger ||= Logger.new(self.log_path || "log/logical_model.log")
      end

      # Filters api_key
      # @return [String]
      def mask_api_key(str)
        if use_api_key && str
          str = str.gsub(api_key,'[SECRET]')
        end
        str
      end

    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
logical_model-0.6.3 lib/logical_model/safe_log.rb
logical_model-0.6.2 lib/logical_model/safe_log.rb
logical_model-0.6.1 lib/logical_model/safe_log.rb
logical_model-0.6.0 lib/logical_model/safe_log.rb
logical_model-0.5.13 lib/logical_model/safe_log.rb
logical_model-0.5.12 lib/logical_model/safe_log.rb
logical_model-0.5.11 lib/logical_model/safe_log.rb
logical_model-0.5.10 lib/logical_model/safe_log.rb
logical_model-0.5.8 lib/logical_model/safe_log.rb
logical_model-0.5.7 lib/logical_model/safe_log.rb
logical_model-0.5.5 lib/logical_model/safe_log.rb
logical_model-0.5.4 lib/logical_model/safe_log.rb
logical_model-0.5.3 lib/logical_model/safe_log.rb
logical_model-0.5.2 lib/logical_model/safe_log.rb
logical_model-0.5.1 lib/logical_model/safe_log.rb