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'