Hi all,

I recently upgraded my site to Mezzanine 3.0.6 and Django 1.6.1

Previously I was getting error reports sent to Errorstack using
https://bitbucket.org/carljm/django-errorstack with a modification of my own to make use of Django's logging feature. See below for the code.

Mostly the errors I saw every now and then were malware robots trying to get to certain common PHP pages using the site's IP address, and Django rejecting these due to my ALLOWED_HOSTS settings only including the domain name (and not the IP address). All good.

Since the upgrade I'm getting error reports directly emailed from Django
and with the error message in the subject line and
"No stack trace available

 Request repr() unavailable."
in the message body.

From what I can tell of the Django documentation, the Logging mechanism hasn't changed in 1.6.1 (compared to 1.5.x) but for some reason it doesn't seem like it's using my logger any more, and just using the default Django one.

Any suggestions? It's hard to replicate this locally as it's difficult to replicate the generation of errors (unless anyone has some suggestions on that too!)

Thanks,

Seeya. Danny.

My code:
===========================
# in local_settings.py

if not DEBUG:
    LOGGING = {
        'version' : 1,
        'disable_existing_loggers': False,
        'filters': {
            'require_debug_false': {
                '()': 'django.utils.log.RequireDebugFalse',
            },
        },
        'handlers' : {
            'myerrorstack' : {
                'level' : 'ERROR',
                'filters' : ['require_debug_false'],
                'class' : 'handlers.MyErrorStackHandler',
            },
        },
        'loggers' : {
            'django.request': {
                'handlers' : ['myerrorstack'],
                'level': 'ERROR',
                'propagate': False,
            },
        },
    }

==============================================================
# in handlers.py

import logging
import logging.handlers
import traceback
from errorstack import settings as errorstack_settings
from django.conf import settings
from django.views.debug import get_exception_reporter_filter


if errorstack_settings.STACK_KEY is None:
    raise ImproperlyConfigured('ErrorStackHandler requires the '
                               'ERRORSTACK_STACK_KEY setting.')

# This code modified from django-errorstack
# and borrowing stuff from django.utils.log.AdminEmailHandler
class MyErrorStackHandler(logging.handlers.HTTPHandler):
    def __init__(self):
        logging.handlers.HTTPHandler.__init__(self,
            "www.errorstack.com",
            "/submit?_s=%s&_r=json" % errorstack_settings.STACK_KEY,
        "POST")

    def mapLogRecord(self, record):
        """ Define the values submitted to ErrorStack.com. """
keys = ['name', 'msg', 'levelname', 'module', 'pathname', 'funcName', 'lineno', 'args', 'exc_text', 'threadName', 'thread', 'process',
                'asctime']
        info = {}
        for key in keys:
            info[key] = record.__dict__.get(key, '')
        return info

    def emit(self, record):
        try:
            request = record.request
            subject = '%s (%s IP): %s' % (
                record.levelname,
                (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
                 and 'internal' or 'EXTERNAL'),
                record.getMessage()
            )
            filter = get_exception_reporter_filter(request)
            request_repr = filter.get_request_repr(request)
        except Exception:
            subject = '%s: %s' % (
                record.levelname,
                record.getMessage()
            )
            request = None
            request_repr = "Request repr() unavailable."
        #
        # subject = self.format_subject(subject)

        if record.exc_info:
            exc_info = record.exc_info
stack_trace = '\n'.join(traceback.format_exception(*record.exc_info))
        else:
            exc_info = (None, record.getMessage(), None)
            stack_trace = 'No stack trace available'

        message = "%s\n\n%s" % (stack_trace, request_repr)
        #reporter = ExceptionReporter(request, is_email=True, *exc_info)
#html_message = self.include_html and reporter.get_traceback_html() or None #mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)
        message = subject + "\n\n" + message
# Create a new record, overriding the message with our verbose traceback
        newrecord = record.__dict__
        newrecord['msg'] = message
        newrecord = logging.makeLogRecord(newrecord)
        # Call the baseclass emit to send the HTTP post to errorstack
        logging.handlers.HTTPHandler.emit(self, newrecord)

===================================

--
Email: [email protected]

--
You received this message because you are subscribed to the Google Groups "Mezzanine 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to