Skip to content

Instantly share code, notes, and snippets.

@puentesarrin
Created April 27, 2013 19:11
Show Gist options
  • Save puentesarrin/5474242 to your computer and use it in GitHub Desktop.
Save puentesarrin/5474242 to your computer and use it in GitHub Desktop.
# -*- 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()
@abdelouahabb
Copy link

thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment