Created
April 27, 2013 19:11
-
-
Save puentesarrin/5474242 to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 *-* | |
import logging | |
import motor | |
import pymongo | |
from mongolog.handlers import MongoHandler | |
from tornado import ioloop, options, web | |
from tornado.options import options as opts | |
class HomeHandler(web.RequestHandler): | |
def get(self): | |
logging.info('Hello world') | |
self.write('Hello world') | |
def define_options(): | |
options.define('port', default=8888, type=int, help='Tornado server port') | |
options.define('db_uri', default='mongodb://localhost:27017', type=str, | |
help='MongoDB database URI', group='Database') | |
options.define('db_name', default='mydb', type=str, | |
help='MongoDB database name', group='Database') | |
options.define('log_col_name', default='log', type=str, | |
help='MongoDB collection name for logging', group='Logging') | |
def config_mongolog(c): | |
#Using the same configuration from MotorClient | |
#c_sync = c.sync_client() | |
#Using the recommended way by Jesse | |
c_sync = pymongo.MongoClient(opts.db_uri, w=0) | |
col = c_sync[opts.db_name][opts.log_col_name] | |
logging.getLogger().addHandler(MongoHandler.to(collection=col)) | |
if __name__ == '__main__': | |
define_options() | |
options.parse_command_line() | |
c = motor.MotorClient(opts.db_uri).open_sync() | |
config_mongolog(c) | |
app = web.Application([(r'/', HomeHandler)], db=c[opts.db_name]) | |
app.listen(opts.port) | |
logging.info('Starting up') | |
ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you :)