Opened 7 years ago

Last modified 20 months ago

#29186 new Bug

"django.request" logging breaks "logging.handlers.SocketHandler" — at Version 6

Reported by: direx Owned by: HyunTae Hwang
Component: Core (Other) Version: 2.0
Severity: Normal Keywords:
Cc: Anvesh Mishra Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Roy Smith)

Just wondering what the status of this is. I get occasional errors like the following in my log file. Looks like it's this same issue. I'm running Django 3.1.8.

--- Logging error ---
Traceback (most recent call last):
  File "/data/project/spi-tools-dev/python-distros/Python-3.7.3-install/lib/python3.7/logging/handlers.py", line 630, in emit
    s = self.makePickle(record)
  File "/data/project/spi-tools-dev/python-distros/Python-3.7.3-install/lib/python3.7/logging/handlers.py", line 602, in makePickle
    s = pickle.dumps(d, 1)
  File "/data/project/spi-tools-dev/python-distros/Python-3.7.3-install/lib/python3.7/copyreg.py", line 65, in _reduce_ex
    raise TypeError("can't pickle %s objects" % base.__name__)
TypeError: can't pickle _Input objects
Call stack:
  File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
    response = self.get_response(request)
  File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 136, in get_response
    request=request,
  File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/utils/log.py", line 230, in log_response
    exc_info=exc_info,

Change History (6)

comment:1 by Tim Graham, 7 years ago

Component: UncategorizedCore (Other)
Triage Stage: UnreviewedAccepted

I'm not sure what the best solution is, but I don't think removing request from extra is acceptable as that would be backwards incompatible for logging handlers using that information.

comment:2 by direx, 7 years ago

Just for the record: A custom SocketHandler which works around this problem could look like this:

# -*- coding: utf-8 -*-
from logging.handlers import SocketHandler as _SocketHandler

class DjangoSocketHandler(_SocketHandler):
    def emit(self, record):
        if hasattr(record, 'request'):
            record.request = None
        return super().emit(record)

I don't know if you guys want to ship this as a workaround and update the documentation accordingly. This could also be a documentation-only fix where this code is added as an example for socket logging.

On the other hand an actual fix would be nice of course. I know this is not an easy task and since the majority of people probably won't be using socket logging, a documented and supported workaround (such as the code above) might be sufficient.

comment:3 by HyunTae Hwang, 6 years ago

Owner: changed from nobody to HyunTae Hwang
Status: newassigned

comment:4 by HyunTae Hwang, 6 years ago

I made a Pull Request. https://github.com/django/django/pull/10758

Respect to direx and Tim, I think django framework has to replace the SocketHandler to its own SocketHandler that replace the WSGIRequest object with pickle-able dictionary.

Django framework should respect the design philosophy of Python. So it’s unfair changing SocketHandler.

And it’s unfair blocking SocketHandler or removing request object when it passed into handlers.

I wonder this approach is okay. Please check this PR.

Last edited 6 years ago by HyunTae Hwang (previous) (diff)

comment:5 by Tim Graham, 6 years ago

Has patch: set
Patch needs improvement: set

comment:6 by Roy Smith, 3 years ago

Description: modified (diff)

Just wondering what the status of this is. I get occasional errors like the following in my log file. Looks like it's this same issue. I'm running Django 3.1.8.

--- Logging error ---
Traceback (most recent call last):
  File "/data/project/spi-tools-dev/python-distros/Python-3.7.3-install/lib/python3.7/logging/handlers.py", line 630, in emit
    s = self.makePickle(record)
  File "/data/project/spi-tools-dev/python-distros/Python-3.7.3-install/lib/python3.7/logging/handlers.py", line 602, in makePickle
    s = pickle.dumps(d, 1)
  File "/data/project/spi-tools-dev/python-distros/Python-3.7.3-install/lib/python3.7/copyreg.py", line 65, in _reduce_ex
    raise TypeError("can't pickle %s objects" % base.__name__)
TypeError: can't pickle _Input objects
Call stack:
  File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
    response = self.get_response(request)
  File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 136, in get_response
    request=request,
  File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/utils/log.py", line 230, in log_response
    exc_info=exc_info,
Note: See TracTickets for help on using tickets.
Back to Top