Class: RestCore::CommonLogger

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/common_logger.rb

Constant Summary

Constant Summary

Constants included from Middleware

Middleware::UNRESERVED

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, TIMER, Universal, VERSION

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Middleware

contain_binary?, escape, #fail, #id, included, #log, percent_encode, request_uri, #run, string_keys

Methods included from RestCore

eagerload, id

Class Method Details

+ (Object) members



6
# File 'lib/rest-core/middleware/common_logger.rb', line 6

def self.members; [:log_method]; end

Instance Method Details

- (Object) call(env)



9
10
11
12
13
14
15
16
17
18
# File 'lib/rest-core/middleware/common_logger.rb', line 9

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

- (Object) flush(env)



24
25
26
27
28
29
# File 'lib/rest-core/middleware/common_logger.rb', line 24

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

- (Object) log_request(start_time, response)



31
32
33
34
# File 'lib/rest-core/middleware/common_logger.rb', line 31

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

- (Object) process(response, start_time)



20
21
22
# File 'lib/rest-core/middleware/common_logger.rb', line 20

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