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

Asked

Viewed 34 times

1

I have a custom class for logging and my Settings this way:

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 record.request.META returns nothing. This is the return of the parameter print record:

{  
   '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 do I get the IP, URL, and data I picked up before? I’m using Django 1.10.6 with Python 3.6

  • Got confused because of the new navigation bar?

  • I don’t understand....

  • Your question was in English. I thought I had confused the sites now that the layout is more similar (our layout changed today).

  • ah...I posted it too and translated here huahuahua

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.