Skip to content

Instantly share code, notes, and snippets.

@mattknox
Forked from mattetti/gist:155437
Created July 28, 2009 06:26
Show Gist options
  • Save mattknox/156977 to your computer and use it in GitHub Desktop.
Save mattknox/156977 to your computer and use it in GitHub Desktop.
module CouchRest
class Logger
def initialize(app, db=nil)
@app = app
@db = db
end
def call(env)
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
end
end
# in your app (i.e: rack.rb)
#
# define a database to log our requests
couch = CouchRest.new("http://mattetti.couch.io")
LOG_DB = couch.database!('couchrest-logger')
use CouchRest::Logger, LOG_DB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment