title: Retrieving the request object in a custom Django logging handler
body: <p>I have a custom Logging handler that I want to handle all logging message levels (INFO, WARN, DEBUG, ERROR, etc.) and send that to a data analytics server. For each message, the data will include fields on the record and on the original request object.</p>
  
  <p>The problem is that I have not seen the request object attached to any of the records. I found on the official documentation that only django.request messages have the request object attached to the record, but no mention of what specifically django.request messages are. (<a href="https://docs.djangoproject.com/en/1.9/topics/logging/#django-request" rel="noreferrer">https://docs.djangoproject.com/en/1.9/topics/logging/#django-request</a>).</p>
  
  <p>What are django.request messages? How/When are they fired?
  How can I reroute every logging message to have the request object on it so that my handler can attach that data that will be sent to a proxy server?</p>
  
  <p>----handler----</p>
  
  <pre><code>class LogHandler(logging.Handler):
      request = None
  
      def __init__(self, request=None):
          logging.Handler.__init__(self)
  
      def parse_record_to_json(self, record):
          import json
  
          created = datetime.datetime.fromtimestamp(record.created)
          return {
              'timestamp': created.strftime('%m/%d/%Y %H:%M:%S'),
              'method': record.funcName,
              'level': record.levelname,
              'line': record.lineno,
              'module': record.module,
              'message': record.getMessage(),
              'path': record.pathname,
          }
  
      def emit(self, record):
          user_id = None
          try:
              self.request = record.request
              if self.request.user.is_authenticated():
                  user_id = self.request.user.id
          except:
              print "this must not be a django.request message"
              self.request = None
  
          from .event import SendEvent
          json_record = self.parse_record_to_json(record)
          level = json_record.pop('level', None)
  
          SendEvent(key="server_log",
                      name=level,
                      request=self.request,
                      obj=json_record,
                      user=user_id)
  </code></pre>
  
  <p>-----settings.py-----</p>
  
  <pre><code>LOGGING = {
      'version': 1,
      'disable_existing_loggers': False,
      'formatters': {
          'standard': {
              'format': '%(levelname)s  %(name)s  %(asctime)s %(filename)s:%(lineno)s] %(message)s',
          },
      },
      'handlers': {
          'console': {
              'class': 'logging.StreamHandler',
              'formatter': 'standard',
          },
          'null': {
              'level': 'DEBUG',
              'class': 'django.utils.log.NullHandler',
          },
          'splunk':{
              'class':'proj.common.handlers.LogHandler',
  
          }
      },
      # 'root': {
      #     'handlers': ['console', 'loghandler',],
      #     'level': 'INFO',
      #     'formatter':'standard',
      # },
      'loggers': {
          'django':{
              'handlers':['console'],
              'level':'INFO',
              'formatter':'standard',
          },
          'py.warnings':{
              'handlers': ['null',],
              'propagate': False,
          },
          'django.request':{
              'handlers':['console','loghandler'],
              'propogate':False,
          },
      }
  }
  </code></pre>