Logging request.META does not work with Django Channels + Asgi_redis

1

I have a custom class for logging and my settings look like this:

base.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s|%(asctime)s|%(module)s|%(process)d|%(thread)d|%(message)s',
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s|%(message)s'
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'WARNING',
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console':{
            'level':'WARNING',
            'class':'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'db':{
            'level': 'WARNING',
            'class': 'apps.monitor.loggers.MyDbLogHandler',

        }
    },
    'loggers': {
        'django': {
            'handlers': ['db'],
            'level': 'WARNING',
            }
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        }
}

apps.monitor.loggers.py

import logging
import datetime
import settings

class MyDbLogHandler(logging.Handler): # Inherit from logging.Handler
    def __init__(self):
        # run the regular Handler __init__
        logging.Handler.__init__(self)

    def emit(self, record):
        # instantiate the model
        try:
            #NOTE: need to import this here otherwise it causes a circular reference and doesn't work
            #  i.e. settings imports loggers imports models imports settings...
            from apps.monitor.models import SystemErrorLog

            ip = record.request.META['REMOTE_ADDR'] # during development
            if 'HTTP_X_FORWARDED_FOR' in record.request.META: # load balancer
                ip = record.request.META['HTTP_X_FORWARDED_FOR']

            logEntry = SystemErrorLog(
                level=record.levelname,
                url=record.args[0],
                get=record.request.META['QUERY_STRING'],
                agent=record.request.META['HTTP_USER_AGENT'],
                status=getattr(record, 'status_code', None),
                ip=ip,
                module=record.module,
                process=record.process,
                thread=record.thread,
                message=record.message,
                timestamp=datetime.datetime.now(),
                serialized=record.__dict__
            )
            logEntry.save()
        except:
            pass

        return

It was working fine until I installed Django Channels. Now the record.request.META returns nothing else. This is the return of the print of the record parameter:

{  
   'name':'django.request',
   'msg':'Not Found: %s',
   'args':('/',
   ),
   'levelname':'WARNING',
   'levelno':30,
   'pathname':'/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py',
   'filename':'base.py',
   'module':'base',
   'exc_info':None,
   'exc_text':None,
   'stack_info':None,
   'lineno':152,
   'funcName':'get_response',
   'created':1496330289.80838,
   'msecs':808.3798885345459,
   'relativeCreated':132383.33797454834,
   'thread':123145345765376,
   'threadName':'Thread-5',
   'processName':'MainProcess',
   'process':1056,
   'status_code':404,
   'request':   <WSGIRequest:GET '/'>
}

The return of record.request :

<socket.socket fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8888), raddr=('127.0.0.1', 65285)>

How to get the ip, the url and the data I got before? I'm using Django 1.10.6 with Python 3.6

    
asked by anonymous 01.06.2017 / 21:42

0 answers