Sha256: 8277467720c7f6bb8f2a6cc26ba6a155173b18c81af7e49f7ebe110400282f39

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

module HttpEventLogger

  class Logger

    private

    DEFAULT_LOGGER   = ::Logger.new($stdout).freeze
    DEFAULT_SEVERITY = ::Logger::Severity::DEBUG.freeze

    public

    def initialize(options={})
      @logger            = options[:logger]            || DEFAULT_LOGGER
      @severity          = options[:severity]          || DEFAULT_SEVERITY
      @blacklist_pattern = options[:blacklist_pattern] || nil
    end

    def logs?(uri)
      @blacklist_pattern.nil? || @blacklist_pattern !~ uri
    end

    def connected(connection)
      log "[#{connection.uri}] Connected"
    end

    def sent(request)
      log "[#{request.base_uri}] Sent - #{request.method} #{request.uri}"
      log "[#{request.base_uri}] Sent - Headers: #{request.headers}"
      log "[#{request.base_uri}] Sent - Body: #{request.body}" if request.body.present?
    end

    def received(response)
      log "[#{response.base_uri}] Benchmark: #{response.time_taken_in_seconds} seconds"
      log "[#{response.base_uri}] Received - Status: #{response.status}"
      log "[#{response.base_uri}] Received - Headers: #{response.headers}"
      log "[#{response.base_uri}] Received - Body:#{response.body_with_formatting}"
    end

    private

    def log(message)
      @logger.add(@severity) { message }
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
http_event_logger-0.1.0 ./lib/http_event_logger/logger.rb
http_event_logger-0.1.0.rc4 ./lib/http_event_logger/logger.rb
http_event_logger-0.1.0.rc3 ./lib/http_event_logger/logger.rb
http_event_logger-0.1.0.rc2 ./lib/http_event_logger/logger.rb
http_event_logger-0.1.0.rc1 ./lib/http_event_logger/logger.rb