Sha256: a275f08c1396fe774378642856ee0bdd0dfa44f888fb41fb3fea098039139797

Contents?: true

Size: 830 Bytes

Versions: 31

Compression:

Stored size: 830 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)
    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[RC::REQUEST_METHOD].to_s.upcase} #{request_uri(response)}")
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
rest-core-3.6.0 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.92 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.91 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.9 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.8 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.7 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.6 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.5 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.4 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.3 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.2 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.1 lib/rest-core/middleware/common_logger.rb
rest-core-3.5.0 lib/rest-core/middleware/common_logger.rb
rest-core-3.4.1 lib/rest-core/middleware/common_logger.rb
rest-core-3.4.0 lib/rest-core/middleware/common_logger.rb
rest-core-3.3.3 lib/rest-core/middleware/common_logger.rb
rest-core-3.3.2 lib/rest-core/middleware/common_logger.rb
rest-core-3.3.1 lib/rest-core/middleware/common_logger.rb
rest-core-3.3.0 lib/rest-core/middleware/common_logger.rb
rest-core-3.2.0 lib/rest-core/middleware/common_logger.rb