old_contents: import logging
  import getpass
  from datetime import datetime
  from socket import gethostname
  from pymongo.connection import Connection
  
  
  
  class MongoFormatter(logging.Formatter):
      def format(self, record):
          """Format exception object as a string"""
          data = record.__dict__.copy()
          data.update(
              username=getpass.getuser(),
              time=datetime.now(),
              host=gethostname(),
          )
          if 'exc_info' in data and data['exc_info']:
              data['exc_info'] = self.formatException(data['exc_info'])
          return data
      
  
  class MongoHandler(logging.Handler):
      """ Custom log handler
  
      Logs all messages to a mongo collection. This  handler is 
      designed to be used with the standard python logging mechanism.
      """
  
      @classmethod
      def to(cls, db, collection, host='localhost', port=None, level=logging.NOTSET):
          """ Create a handler for a given  """
          return cls(Connection(host, port)[db][collection], level)
  
      def __init__(self, collection, level=logging.NOTSET):
          """ Init log handler and store the collection handle """
          logging.Handler.__init__(self, level)
          self.collection = collection
          self.formatter = MongoFormatter()
  
      def emit(self,record):
          """ Store the record to the collection. Async insert """
          self.collection.save(self.format(record))
  
  
new_contents: import logging
  import getpass
  from datetime import datetime
  from socket import gethostname
  from pymongo.connection import Connection
  from bson import InvalidDocument
  
  
  class MongoFormatter(logging.Formatter):
      def format(self, record):
          """Format exception object as a string"""
          data = record.__dict__.copy()
          data.update(
              username=getpass.getuser(),
              time=datetime.now(),
              host=gethostname(),
              message=record.msg % record.args,
              args=tuple(unicode(arg) for arg in record.args)
          )
          if 'exc_info' in data and data['exc_info']:
              data['exc_info'] = self.formatException(data['exc_info'])
          return data
      
  
  class MongoHandler(logging.Handler):
      """ Custom log handler
  
      Logs all messages to a mongo collection. This  handler is 
      designed to be used with the standard python logging mechanism.
      """
  
      @classmethod
      def to(cls, db, collection, host='localhost', port=None, level=logging.NOTSET):
          """ Create a handler for a given  """
          return cls(Connection(host, port)[db][collection], level)
  
      def __init__(self, collection, level=logging.NOTSET):
          """ Init log handler and store the collection handle """
          logging.Handler.__init__(self, level)
          self.collection = collection
          self.formatter = MongoFormatter()
  
      def emit(self,record):
          """ Store the record to the collection. Async insert """
          try:
              self.collection.save(self.format(record))
          except InvalidDocument, e:
              logging.error("Unable to save log record: %s", e.message ,exc_info=True)
  
  
subject: Handle serialization errors