Login

ReportBug() with tons of debug in mail

Author:
sleepycal
Posted:
October 1, 2010
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

This basically takes the debug you get from setting debug=True, but instead, pipes it into an email and sends it over to you.

I have extracted this out of our de framework, it should work, but some modifications may be necessary.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#######################################################################################
# Designed and Coded by Cal Leeming for Simplicity Media Ltd
# cal.leeming [at] simplicitymedialtd.co.uk
#######################################################################################

def ReportBug(request=None, serno=None):
    try:
        from webapp.de.djangologging import middleware
        from django.core.mail import mail_admins
        import sys
        import traceback
        import os

        # Mail the admins with the error
        exc_info = sys.exc_info()

        if exc_info:
            _file, _line, _func, _line = traceback.extract_tb(exc_info[2])[0]
            _file = os.path.basename(_file)

        else:
            _file, _line, _func, _line = (None, None, None, None)


        # Check if we have a serno
        if not serno:
            from hashlib import md5
            import random
            serno = md5()
            serno.update(str(random.random()))
            serno = serno.hexdigest()

        import sys
        from django.views.debug import ExceptionReporter
        from django.http import HttpRequest
        from django.conf import settings
        from django.core.mail import EmailMultiAlternatives
        if not request:
            h = HttpRequest()
            h.META['SERVER_NAME'] = 'FAKE'
            h.META['SERVER_PORT'] = '80'
        else:
            h = request
            
        exp = sys.exc_info()
        t = ExceptionReporter(h, *exp)

        if request:
            subject = "[Django] [%s] Exception: %s"%(request.META['REMOTE_ADDR'], str(exp[1]))
        else:
            subject = "[Django] Exception: %s"%(str(exp[1]))

        from_email = settings.SERVER_EMAIL
        to = map(lambda x: x[1], settings.ADMINS)

        text_content = 'Traceback:\n%s\n\n' % ('\n'.join(traceback.format_exception(*exc_info)),)
        html_content = t.get_traceback_html()
        msg = EmailMultiAlternatives(subject, text_content, from_email, to)
        msg.attach_alternative(html_content, "text/html")
        msg.send()
        print "Message sent"

        # Mail the admins with the error
        exc_info = sys.exc_info()
        subject = 'Uncaught exception'
        try:
            request_repr = repr(request)
        except:
            request_repr = 'Request repr() unavailable'
        import traceback
        message = 'Traceback:\n%s\n\nRequest:\n%s' % (
            '\n'.join(traceback.format_exception(*exc_info)),
            request_repr,
        )
        print "------------------- traceback -------------------"
        print message
        print "-------------------------------------------------"
        print ""
        print ""

    except Exception, e:
        try:
            # Mail the admins with the error
            exc_info = sys.exc_info()
            subject = 'Uncaught exception'
            try:
                request_repr = repr(request)
            except:
                request_repr = 'Request repr() unavailable'
            import traceback
            message = 'Traceback:\n%s\n\nRequest:\n%s' % (
                                                          '\n'.join(traceback.format_exception(*exc_info)),
                                                          request_repr,
                                                          )
            mail_admins(subject, message, fail_silently=True)

            print "------------------- traceback (oops) -------------------"
            print message
            print "--------------------------------------------------------"
            print ""
            print ""
            
        except Exception, e:
            mail_admins("SERIOUS ERROR", "Not sure what happened.. %s"%str(e), fail_silently=True)

# Example Usage
try:
    raise Exception, "test"
except Exception, e:
    ReportBug()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Archatas (on October 25, 2010):

Can you tell me how to install it?

#

sleepycal (on October 27, 2010):

Hi Archatas,

Feel free to email me on [email protected] with any questions.

Cal

#

Please login first before commenting.