Sha256: 3d9da2bd7e1a506e03cca825edc73a61a58118f4e28bacb9144666155cabc939

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

class Hyperion
  module Logger
    class << self
      attr_accessor :level
    end

    def logger
      rails_logger_available? ? Rails.logger : default_logger
    end

    def with_request_logging(route, uri, headers)
      log_request_start(route, uri, headers)
      start = Time.now
      begin
        yield
      ensure
        stop = Time.now
        log_request_end(((stop - start) * 1000).round)
      end
    end

    def log_stub(rule)
      mr = rule.mimic_route
      logger.debug "Stubbed #{mr.method.to_s.upcase} #{mr.path}"
      log_headers(rule.headers)
    end

    private

    def log_request_start(route, uri, headers)
      logger.debug "Requesting #{route.method.to_s.upcase} #{uri}"
      log_headers(headers)
    end

    def log_request_end(ms)
      logger.debug "Completed in #{ms}ms"
      logger.debug ''
    end

    def rails_logger_available?
      Kernel.const_defined?(:Rails) && !Rails.logger.nil?
    end

    def default_logger
      logger = ::Logger.new($stdout)
      logger.level = Hyperion::Logger.level || ::Logger::DEBUG
      logger
    end

    def log_headers(headers)
      headers.each_pair { |k, v| logger.debug "    #{k}: #{v}" unless k == 'Expect' }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hyperion_http-0.1.4 lib/hyperion/aux/logger.rb
hyperion_http-0.1.3 lib/hyperion/aux/logger.rb
hyperion_http-0.1.2 lib/hyperion/aux/logger.rb