title: Python logging with context
body: <p>I have a logging setup in python application that logs into file and MongoDB. The setup looks like this:</p>
  
  <pre><code>[logger_root]
  handlers=myHandler,mongoHandler
  level=DEBUG
  qualname=myapp
  
  [handler_myHandler]
  class=handlers.RotatingFileHandler
  level=DEBUG
  formatter=myFormatter
  args=('myapp.log', 'a',20000000,10)
  
  [handler_mongoHandler]
  class=myapp.MongoLogger.MongoLogger
  level=INFO
  args=('log',)
  
  [formatter_myFormatter]
  format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
  </code></pre>
  
  <p>And the MongoLogger has emit() function like this:</p>
  
  <pre><code>def emit(self, record):
      logdata = record.__dict__
      try:
          if(self.data == None):
              self.initDb()
          self.logtable.insert(logdata)
      except:
         self.handleError(record)
  </code></pre>
  
  <p>The logging is done like this then:</p>
  
  <pre><code> logger.info("Processing account %s..." % account)
  </code></pre>
  
  <p>It works reasonably well, but now I have an additional requirement. I want it to have some context - i.e., to be able to define custom value - say, account name - so every log done inside the account processing would have the account name as part of the <code>record</code> passed to <code>emit</code> above and also available for the formatter in <code>myFormatter</code> string.</p>
  
  <p>Is it possible to do this with logging module? Is there maybe another, better way to do the same? </p>