-
-
Save mattknox/156977 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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