Skip to content

Commit

Permalink
Added log4mongo logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mapio committed Dec 4, 2011
1 parent f32c685 commit bb0de7f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: gunicorn nofussbm:app -w 3 -b "0.0.0.0:$PORT"
web: gunicorn nofussbm:app --logger-class nofussbm.logger.Logger --access-logfile=/dev/null --error-logfile=- -w 3 -b "0.0.0.0:$PORT"
20 changes: 10 additions & 10 deletions nofussbm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from smtplib import SMTP
from urlparse import parse_qs

from flask import Flask, make_response, request, g, redirect, url_for, json
from flask import Flask, make_response, request, g, redirect, url_for, json, abort

from pymongo import Connection
from pymongo.errors import OperationFailure, DuplicateKeyError
Expand Down Expand Up @@ -151,9 +151,9 @@ def list( ident ):
alias = g.db.aliases.find_one( { 'alias': ident }, { 'email': 1 } )
email = alias[ 'email' ]
except TypeError:
return '', 404
abort( 404 )
except OperationFailure:
return '', 500
abort( 500 )
query = { 'email': email }
if 'tags' in args:
tags = map( lambda _: _.strip(), args[ 'tags' ].split( ',' ) )
Expand All @@ -174,7 +174,7 @@ def list( ident ):
date = bm[ 'date-modified' ]
result.append( u'\t'.join( ( date.strftime( '%Y-%m-%d' ), bm[ 'url' ], bm[ 'title' ], u','.join( bm[ 'tags' ] ) ) ) )
except OperationFailure:
return '', 500
abort( 500 )
return textify( u'\n'.join( result ) )

@app.route( '/stats' )
Expand All @@ -183,7 +183,7 @@ def stats():
try:
result[ 'users' ] = g.db.bookmarks.group( { 'email': 1 }, None, { 'count': 0 }, 'function( o, p ){ p.count++; }' )
except OperationFailure:
return myjsonify( code = 500 )
abort( 500 )
return myjsonify( result )

# API "views"
Expand Down Expand Up @@ -220,7 +220,7 @@ def get():
m = m.groups()
skip = int( m[ 0 ] )
limit = int( m[ 2 ] ) - skip + 1 if m[ 2 ] else 0
if not m or limit < 0: return myjsonify( code = 416 )
if not m or limit < 0: abort( 416 )

query = { 'email': g.email }
if 'X-Nofussbm-query' in request.headers:
Expand All @@ -240,7 +240,7 @@ def get():
bm[ 'tags' ] = u','.join( bm[ 'tags' ] )
result.append( bm )
except OperationFailure:
return myjsonify( code = 500 )
abort( 500 )
return myjsonify( result, headers = { 'Content-Range': 'bookmarks {0}-{1}/{2}'.format( skip, skip + ( limit - 1 if limit else n ), n ), 'Accept-Ranges': 'bookmarks' } )

@app.route( API_PREFIX + '/', methods = [ 'PUT' ] )
Expand Down Expand Up @@ -287,7 +287,7 @@ def sendkey():
try:
send_mail( 'Massimo Santini <massimo.santini@gmail.com>', email, 'Your "No Fuss Bookmark" API key', 'Your key is {0}'.format( key ) )
except:
return '', 500
abort( 500 )
return ''

@app.route( API_PREFIX + '/setalias/<alias>', methods = [ 'POST' ] )
Expand Down Expand Up @@ -332,6 +332,6 @@ def delicious_import():
try:
_id = g.db.bookmarks.insert( bms, safe = True )
except OperationFailure:
return textify( 'error', 500 )
abort( 500 )
else:
return textify( 'success' )
return textify( 'success' )
23 changes: 23 additions & 0 deletions nofussbm/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from logging import INFO
from os import environ as ENV
from urlparse import urlparse

from log4mongo.handlers import MongoHandler, MongoFormatter

import gunicorn.glogging


MONGOLAB = urlparse( ENV[ 'MONGOLAB_URI' ] )


class Logger( gunicorn.glogging.Logger ):
def __init__( self, cfg ):
super( Logger, self ).__init__( cfg )
access_handler = MongoHandler( level = INFO, host = MONGOLAB.hostname, port = MONGOLAB.port, database_name = MONGOLAB.path[ 1: ], collection = 'access-logs', username = MONGOLAB.username, password = MONGOLAB.password )
error_handler = MongoHandler( level = INFO, host = MONGOLAB.hostname, port = MONGOLAB.port, database_name = MONGOLAB.path[ 1: ], collection = 'error-logs', username = MONGOLAB.username, password = MONGOLAB.password )
access_handler.setFormatter( MongoFormatter() )
error_handler.setFormatter( MongoFormatter() )
self.access_log.addHandler( access_handler )
self.access_log.setLevel( INFO )
self.error_log.addHandler( error_handler )
self.error_log.setLevel( INFO )
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Flask==0.8
gunicorn==0.13.4
pymongo==2.0.1
Flask
gunicorn
pymongo
log4mongo
2 changes: 1 addition & 1 deletion scripts/get_heroku_env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

heroku config --app nofussbm-old -s | egrep '(MONGOLAB_URI|SECRET_KEY|SENDGRID_PASSWORD|SENDGRID_USERNAME)' > .env
heroku config --app nofussbm -s | egrep '(MONGOLAB_URI|SECRET_KEY|SENDGRID_PASSWORD|SENDGRID_USERNAME)' > .env
20 changes: 20 additions & 0 deletions scripts/get_logs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python

from os import environ as ENV
from urlparse import urlparse

from pymongo import Connection


MONGOLAB_URI = ENV[ 'MONGOLAB_URI' ]


conn = Connection( MONGOLAB_URI )
db = conn[ urlparse( MONGOLAB_URI ).path[ 1: ] ]

print 'ACCESS\n' + 80 * '-'
for entry in db[ 'access-logs' ].find():
print entry['timestamp'].as_datetime(), entry['level'], entry['loggerName'], entry['message']
print 'ERROR\n' + 80 * '-'
for entry in db[ 'error-logs' ].find():
print entry['timestamp'].as_datetime(), entry['level'], entry['loggerName'], entry['message']

0 comments on commit bb0de7f

Please sign in to comment.