Pyramid (Python): overriding the default error

Task: When an error occurs in one of my views, I need to show my own view of the error. The type of error does not matter.

I tried to override all exceptions by registering a view for all exceptions, for example:

<view
    context="Exception"
    renderer="server_error.pt"
    />

He worked good . All exceptions showed my opinion. But the problem was that these errors stopped being logged . In the course, I can do something like logger.error (traceback) in my view with an error, but this is a dumb idea.

How can I register a submission for all errors without breaking the registration pyramid system.

+5
source share
3

python. 500 ( , 200, ).

:

, , development.ini/production.ini , , , , .....

[formatter_generic]
# format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
format = y u no work??!!?? %(message)s
# End logging configuration

from pyramid.view import view_config
from webob import Response
import logging
log = logging.getLogger(__name__)

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    raise ValueError("oops")
    return {'project':'tstLogError'}


@view_config(context=Exception)
def exception_view(context, request):
    log.error("The error was: %s" % context, exc_info=(context))
    return Response(status_int=500, body=str(context))

    from pyramid.view import view_config
    from webob import Response

:

serving on http://0.0.0.0:6543
y u no work??!!?? The error was: oops
Traceback (most recent call last):
  File "/home/twillis/projects/TestLogError/local/lib/python2.7/site-packages/pyramid/tweens.py", line 20, in excview_tween
    response = handler(request)
  File "/home/twillis/projects/TestLogError/local/lib/python2.7/site-packages/pyramid/router.py", line 164, in handle_request
    response = view_callable(context, request)
  File "/home/twillis/projects/TestLogError/local/lib/python2.7/site-packages/pyramid/config/views.py", line 333, in rendered_view
    result = view(context, request)
  File "/home/twillis/projects/TestLogError/local/lib/python2.7/site-packages/pyramid/config/views.py", line 471, in _requestonly_view
    response = view(request)
  File "/home/twillis/projects/TestLogError/tstLogError/tstlogerror/views.py", line 8, in my_view
    raise ValueError("oops")
ValueError: oops

browser screenshot

+2

( request.context.) , , , .

WSGI, wsgiref. WSGI, .

wsgiref:

def log_exception(self,exc_info):
    """Log the 'exc_info' tuple in the server log

    Subclasses may override to retarget the output or change its format.
    """
    try:
        from traceback import print_exception
        stderr = self.get_stderr()
        print_exception(
            exc_info[0], exc_info[1], exc_info[2],
            self.traceback_limit, stderr
        )
        stderr.flush()
    finally:
        exc_info = None

, , - -, Pyramid.

+2

pyramid_exclog .

, Exception, , , - , , .

In addition to the pyramid_exclog documentation , I also found Python. The configuration file format in the Python documentation is very useful, as the configuration is quite complicated to get right.

0
source

All Articles