Sha256: be21e69805133caa67586db42a807d6081c88e10e998dc41c1fc3c00b35ffcf7

Contents?: true

Size: 849 Bytes

Versions: 2

Compression:

Stored size: 849 Bytes

Contents

require 'rest-core/event'

module RestCore
  class CommonLogger
    def self.members; [:log_method]; end
    include Middleware

    def call env
      start_time = Time.now
      flushed = flush(env)
      app.call(flushed){ |response|
        yield(process(response, start_time))
      }
    rescue
      process(flushed, start_time)
      raise
    end

    def process response, start_time
      flush(log(response, log_request(start_time, response)))
    end

    def flush env
      return env if !log_method(env) || env[DRY]
      (env[LOG] || []).compact.
        each{ |obj| log_method(env).call("RestCore: #{obj}") }
      env.merge(LOG => [])
    end

    def log_request start_time, response
      Event::Requested.new(Time.now - start_time,
        "#{response[REQUEST_METHOD].to_s.upcase} #{request_uri(response)}")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest-core-4.0.1 lib/rest-core/middleware/common_logger.rb
rest-core-4.0.0 lib/rest-core/middleware/common_logger.rb