adding process_exception() to middleware; newbie needs help

699 views
Skip to first unread message

Fred

unread,
Oct 6, 2010, 10:42:52 AM10/6/10
to Django users
My django app is working great with Debug=True. But now I'm in
production and I want to email exceptions to me and print them to my
logfile. I've been googling and searching the docs for the last 2
hours and what I really need is a snippet that shows how to do it in
middleware/settings.py. I've never defined my own middleware and I'm
just treading water. I've combined everything into my settings.py
file to make it easier to explain, even if that may be bad design for
now. Here's as far as I've gotten; which just times out on even valid
requests:

----------------------------
settings.py---------------------------------------------------------------
partial


def init_logging():
formatter = logging.Formatter("%(asctime)s - %(name)s - %
(levelname)s - %(message)s")
filelogger = logging.getLogger()
filehandler = logging.FileHandler(LOGFILEPATH)
filelogger.setLevel(logging.DEBUG)
filehandler.setFormatter(formatter)
filelogger.addHandler(filehandler)


logInitDone=False
if not logInitDone:
logInitDone = True
init_logging()

logger = logging.getLogger("settings") #verified this logger does
work.

def process_exception(self, request, exception):
logger.debug(str(request))
logger.debug(str(exception))
return None


MIDDLEWARE_CLASSES = (
'process_exception', #if I comment this out, system works, with
it in response hangs even without error
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
# 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)

Daniel Roseman

unread,
Oct 6, 2010, 11:10:30 AM10/6/10
to Django users
Your middleware should be a class which implements the
process_exception method.

class LogMiddleware(object):
def process_exception(self, request, exception):
...etc...

Also, note that Django already emails on 500 errors (and optionally on
404s) to the users listed in the ADMINS setting. See
http://docs.djangoproject.com/en/1.2/howto/error-reporting/
--
DR

bruno desthuilliers

unread,
Oct 6, 2010, 11:16:23 AM10/6/10
to Django users
On 6 oct, 16:42, Fred <frse...@adventistcare.org> wrote:
> My django app is working great with Debug=True.  But now I'm in
> production and I want to email exceptions to me

That's the default behaviour if you correctly filled the relevant
parts of your settings.py (that is, mainly, the ADMINS, and all the
SMTP_xxx parts if your smtp server requires it).

> and print them to my
> logfile.

This requires a bit more work.

>  I've been googling and searching the docs for the last 2
> hours

???

http://docs.djangoproject.com/en/dev/topics/http/middleware/#writing-your-own-middleware
http://docs.djangoproject.com/en/dev/topics/http/middleware/#activating-middleware

1/ create a python module somewhere in your python path (ie : in your
project dir or in one of your apps)

2/ in that module, define a class with at least one of
'process_request', 'process_response' or 'process_exception' methods
(more details in the FineManual)

3/ in your settings.py, add the full qualified name (ie :
'yourapp.yourmodule.youclass') to the MIDDLEWARE_CLASSES entry


Which of these points do you have probems with ?

> and what I really need is a snippet that shows how to do it in
> middleware/settings.py.
> I've never defined my own middleware and I'm
> just treading water.  I've combined everything into my settings.py
> file to make it easier to explain, even if that may be bad design for
> now.

It's not "bad design", it just won't work.

>  Here's as far as I've gotten; which just times out on even valid
> requests:
>
> ----------------------------
> settings.py---------------------------------------------------------------
> partial
>
> def init_logging():
>     formatter   = logging.Formatter("%(asctime)s - %(name)s - %
> (levelname)s - %(message)s")
>     filelogger  = logging.getLogger()
>     filehandler = logging.FileHandler(LOGFILEPATH)
>     filelogger.setLevel(logging.DEBUG)
>     filehandler.setFormatter(formatter)
>     filelogger.addHandler(filehandler)
>
> logInitDone=False
> if not logInitDone:
>     logInitDone = True
>     init_logging()
>
> logger = logging.getLogger("settings")  #verified this logger does
> work.
>
> def process_exception(self, request, exception):
>     logger.debug(str(request))
>     logger.debug(str(exception))
>     return None


All The above has nothing to do in the settings. You need to define a
middleware *class* in a distinct module and make 'process_exception' a
method of this class.


> MIDDLEWARE_CLASSES = (
>     'process_exception',  #if I comment this out, system works, with
> it in response hangs even without error

this should be something like
'yourmiddlewaremodulename.yourmiddlewareclassname'

Sells, Fred

unread,
Oct 6, 2010, 3:02:23 PM10/6/10
to django...@googlegroups.com
Thanks to Daniel and Bruno, will try your suggestions soonest.


Reply all
Reply to author
Forward
0 new messages