Sha256: c762940448d0292ab31882162ffd496dab123dd3be3ad0f341519fc76b192e47

Contents?: true

Size: 894 Bytes

Versions: 4

Compression:

Stored size: 894 Bytes

Contents

require 'rest-core/event'
require 'rest-core/middleware'

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

  def call env
    start_time = Time.now
    flushed = flush(env)
    if env[ASYNC]
      app.call(flushed.merge(ASYNC => lambda{ |response|
        env[ASYNC].call(process(response, start_time))
      }))
    else
      process(app.call(flushed), start_time)
    end
  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, request_uri(response))
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rest-core-1.0.3 lib/rest-core/middleware/common_logger.rb
rest-core-1.0.2 lib/rest-core/middleware/common_logger.rb
rest-core-1.0.1 lib/rest-core/middleware/common_logger.rb
rest-core-1.0.0 lib/rest-core/middleware/common_logger.rb