Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """``MongoHandler`` - Python logging handler for MongoDB.
- Usage: ::
- import logging
- from mongohandler import MongoHandler
- logger = logging.getLogger("my-logger")
- logger.setLevel(logging.DEBUG)
- logger.addHandler(MongoHandler(db="big_brother", collection="logs"))
- logger.info("The object of power is power.")
- """
- import logging
- import pymongo
- class MongoHandler(logging.Handler):
- def __init__(self, level=0, hostname="localhost", port=27017, \
- db="global_logging", collection="log_data"):
- logging.Handler.__init__(self)
- self.setLevel(level)
- self.connection = pymongo.Connection(hostname)
- self.db = getattr(self.connection, db)
- self.collection = getattr(self.db, collection)
- def emit(self, record):
- log_entry = {
- 'levelname': record.levelname,
- 'levelno': record.levelno,
- 'name': record.name,
- 'msg': record.msg,
- 'created': record.created
- }
- self.collection.ensure_index("levelname")
- self.collection.ensure_index("levelno")
- self.collection.ensure_index("created")
- self.collection.insert(log_entry)
Add Comment
Please, Sign In to add comment