Class: CouchRest::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/middlewares/logger.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, db = nil) ⇒ Logger

Returns a new instance of Logger.



24
25
26
27
# File 'lib/couchrest/middlewares/logger.rb', line 24

def initialize(app, db=nil)
  @app = app
  @db = db
end

Class Method Details

.logObject



20
21
22
# File 'lib/couchrest/middlewares/logger.rb', line 20

def self.log
  Thread.current["couchrest.logger"] ||= {:queries => []}
end

.record(log_info) ⇒ Object



29
30
31
# File 'lib/couchrest/middlewares/logger.rb', line 29

def self.record(log_info)
  log[:queries] << log_info
end

Instance Method Details

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/couchrest/middlewares/logger.rb', line 41

def call(env)
  reset_log
  log['started_at'] = Time.now
  log['env'] = env
  log['url'] = 'http://' + env['HTTP_HOST'] + env['REQUEST_URI']
  response = @app.call(env)
  log['ended_at'] = Time.now
  log['duration'] = log['ended_at'] - log['started_at']
  # let's report the log in a different thread so we don't slow down the app
  @db ? Thread.new(@db, log){|db, rlog| db.save_doc(rlog);} : p(log.inspect)
  response
end

#logObject



33
34
35
# File 'lib/couchrest/middlewares/logger.rb', line 33

def log
  Thread.current["couchrest.logger"] ||= {:queries => []}
end

#reset_logObject



37
38
39
# File 'lib/couchrest/middlewares/logger.rb', line 37

def reset_log
  Thread.current["couchrest.logger"] = nil
end