librelist archives

« back to archive

Logging exceptions

Logging exceptions

From:
Lucas Vickers
Date:
2013-04-30 @ 19:13
I have the following error handler in my flask app.py


        @app.errorhandler(Exception)
        def internal_exception_handler(error=""):
            current_app.logger.error("Exception occured: " + str(error))
            message = {
            'status': 500,
            'message': 'Internal Server Error: ' + str(error)}
            resp = jsonify(message)
            resp.status_code = 500

            return render_template('error.html', error="Sorry, there was a
server error.")


Which is fantastic since I can only give users nice stylized error pages,
but unfortunately I'm getting next to none of the stack trace.

Is there a good practice within flask to both print the entire trace into
the current_app.logger but also render a template on the frontend?

thanks

Re: Logging exceptions

From:
Lucas Vickers
Date:
2013-04-30 @ 19:15
ps I just noticed the response message isn't needed, but that's kinda
beside the point :)


On Tue, Apr 30, 2013 at 3:13 PM, Lucas Vickers <lucas@localprojects.net>wrote:

> I have the following error handler in my flask app.py
>
>
>         @app.errorhandler(Exception)
>         def internal_exception_handler(error=""):
>             current_app.logger.error("Exception occured: " + str(error))
>             message = {
>             'status': 500,
>             'message': 'Internal Server Error: ' + str(error)}
>             resp = jsonify(message)
>             resp.status_code = 500
>
>             return render_template('error.html', error="Sorry, there was a
> server error.")
>
>
> Which is fantastic since I can only give users nice stylized error pages,
> but unfortunately I'm getting next to none of the stack trace.
>
> Is there a good practice within flask to both print the entire trace into
> the current_app.logger but also render a template on the frontend?
>
> thanks
>

Re: [flask] Re: Logging exceptions

From:
Struggling Developer
Date:
2013-04-30 @ 19:29
The following line creates a response with the error message:

resp = jsonify(message)
resp.status_code = 500

The next line throws all that work away, and renders a a random teplate
(without even sending the message as a parameter):

return render_template('error.html', error="Sorry, there was a server
error.")

Replace the "return render_template" by "return resp" and you should see
something closer to what you expected.

However, observe that the message will be in a json response, which might
not qualify as a "nice stylized" error page.

Perhaps you should instead send "message" as a parameter to the template,
and then render it in your template code.

This would be achieved by:

return render_template('error.html', message = message)

I hope that helped.

Alex



On Tue, Apr 30, 2013 at 3:15 PM, Lucas Vickers <lucas@localprojects.net>wrote:

> ps I just noticed the response message isn't needed, but that's kinda
> beside the point :)
>
>
> On Tue, Apr 30, 2013 at 3:13 PM, Lucas Vickers <lucas@localprojects.net>wrote:
>
>> I have the following error handler in my flask app.py
>>
>>
>>         @app.errorhandler(Exception)
>>         def internal_exception_handler(error=""):
>>             current_app.logger.error("Exception occured: " + str(error))
>>             message = {
>>             'status': 500,
>>             'message': 'Internal Server Error: ' + str(error)}
>>             resp = jsonify(message)
>>             resp.status_code = 500
>>
>>             return render_template('error.html', error="Sorry, there was
>> a server error.")
>>
>>
>> Which is fantastic since I can only give users nice stylized error pages,
>> but unfortunately I'm getting next to none of the stack trace.
>>
>> Is there a good practice within flask to both print the entire trace into
>> the current_app.logger but also render a template on the frontend?
>>
>> thanks
>>
>
>

Re: [flask] Re: Logging exceptions

From:
Lucas Vickers
Date:
2013-04-30 @ 19:43
That last message was totally misleading and completely my fault.  I sent
it out mid change w/o noticing that part of the code.


I want to display a nice simple template for the user, so I want something
more like

        @app.errorhandler(Exception)
        def internal_exception_handler(error=""):
            current_app.logger.error("Exception occured: " + str(error))

            return render_template('error.html', error="Sorry, there was a
server error.")

I'm trying to figure out how I can get full trace out of the Exception
passed down from the error handler so that I can internally have a log that
includes the original exception and a traceback, all while only rendering a
happy error page for the user.

so how can I take the error and get the stacktrace and original exception.
 This is almost a Python question not a Flask question -- but I'm also
curious if anyone has a more clever way to do this.

thanks



On Tue, Apr 30, 2013 at 3:29 PM, Struggling Developer <
strugglingdeveloper@gmail.com> wrote:

> The following line creates a response with the error message:
>
> resp = jsonify(message)
> resp.status_code = 500
>
> The next line throws all that work away, and renders a a random teplate
> (without even sending the message as a parameter):
>
> return render_template('error.html', error="Sorry, there was a server
> error.")
>
> Replace the "return render_template" by "return resp" and you should see
> something closer to what you expected.
>
> However, observe that the message will be in a json response, which might
> not qualify as a "nice stylized" error page.
>
> Perhaps you should instead send "message" as a parameter to the template,
> and then render it in your template code.
>
> This would be achieved by:
>
> return render_template('error.html', message = message)
>
> I hope that helped.
>
> Alex
>
>
>
> On Tue, Apr 30, 2013 at 3:15 PM, Lucas Vickers <lucas@localprojects.net>wrote:
>
>> ps I just noticed the response message isn't needed, but that's kinda
>> beside the point :)
>>
>>
>> On Tue, Apr 30, 2013 at 3:13 PM, Lucas Vickers <lucas@localprojects.net>wrote:
>>
>>> I have the following error handler in my flask app.py
>>>
>>>
>>>         @app.errorhandler(Exception)
>>>         def internal_exception_handler(error=""):
>>>             current_app.logger.error("Exception occured: " + str(error))
>>>             message = {
>>>             'status': 500,
>>>             'message': 'Internal Server Error: ' + str(error)}
>>>             resp = jsonify(message)
>>>             resp.status_code = 500
>>>
>>>             return render_template('error.html', error="Sorry, there was
>>> a server error.")
>>>
>>>
>>> Which is fantastic since I can only give users nice stylized error pages,
>>> but unfortunately I'm getting next to none of the stack trace.
>>>
>>> Is there a good practice within flask to both print the entire trace
>>> into the current_app.logger but also render a template on the frontend?
>>>
>>> thanks
>>>
>>
>>
>

Re: [flask] Re: Logging exceptions

From:
Alex Ogier
Date:
2013-04-30 @ 19:59
Take a look at the traceback[1] library in Python and the sys.exc_info()
function[2] which can be used to extract the last exception raised and
format its traceback.

[1]: http://docs.python.org/2/library/traceback.html
[2]: http://docs.python.org/2/library/sys.html#sys.exc_info

Best,
Alex Ogier


On Tue, Apr 30, 2013 at 3:43 PM, Lucas Vickers <lucas@localprojects.net>wrote:

> That last message was totally misleading and completely my fault.  I sent
> it out mid change w/o noticing that part of the code.
>
>
> I want to display a nice simple template for the user, so I want something
> more like
>
>         @app.errorhandler(Exception)
>         def internal_exception_handler(error=""):
>             current_app.logger.error("Exception occured: " + str(error))
>
>             return render_template('error.html', error="Sorry, there was a
> server error.")
>
> I'm trying to figure out how I can get full trace out of the Exception
> passed down from the error handler so that I can internally have a log that
> includes the original exception and a traceback, all while only rendering a
> happy error page for the user.
>
> so how can I take the error and get the stacktrace and original exception.
>  This is almost a Python question not a Flask question -- but I'm also
> curious if anyone has a more clever way to do this.
>
> thanks
>
>
>
> On Tue, Apr 30, 2013 at 3:29 PM, Struggling Developer <
> strugglingdeveloper@gmail.com> wrote:
>
>> The following line creates a response with the error message:
>>
>> resp = jsonify(message)
>> resp.status_code = 500
>>
>> The next line throws all that work away, and renders a a random teplate
>> (without even sending the message as a parameter):
>>
>> return render_template('error.html', error="Sorry, there was a server
>> error.")
>>
>> Replace the "return render_template" by "return resp" and you should see
>> something closer to what you expected.
>>
>> However, observe that the message will be in a json response, which might
>> not qualify as a "nice stylized" error page.
>>
>> Perhaps you should instead send "message" as a parameter to the template,
>> and then render it in your template code.
>>
>> This would be achieved by:
>>
>> return render_template('error.html', message = message)
>>
>> I hope that helped.
>>
>> Alex
>>
>>
>>
>> On Tue, Apr 30, 2013 at 3:15 PM, Lucas Vickers <lucas@localprojects.net>wrote:
>>
>>> ps I just noticed the response message isn't needed, but that's kinda
>>> beside the point :)
>>>
>>>
>>> On Tue, Apr 30, 2013 at 3:13 PM, Lucas Vickers <lucas@localprojects.net>wrote:
>>>
>>>> I have the following error handler in my flask app.py
>>>>
>>>>
>>>>         @app.errorhandler(Exception)
>>>>         def internal_exception_handler(error=""):
>>>>             current_app.logger.error("Exception occured: " + str(error))
>>>>             message = {
>>>>             'status': 500,
>>>>             'message': 'Internal Server Error: ' + str(error)}
>>>>             resp = jsonify(message)
>>>>             resp.status_code = 500
>>>>
>>>>             return render_template('error.html', error="Sorry, there
>>>> was a server error.")
>>>>
>>>>
>>>> Which is fantastic since I can only give users nice stylized error
>>>> pages,
>>>> but unfortunately I'm getting next to none of the stack trace.
>>>>
>>>> Is there a good practice within flask to both print the entire trace
>>>> into the current_app.logger but also render a template on the frontend?
>>>>
>>>> thanks
>>>>
>>>
>>>
>>
>

Re: [flask] Re: Logging exceptions

From:
Steven Kryskalla
Date:
2013-04-30 @ 19:54
On Tue, Apr 30, 2013 at 12:43 PM, Lucas Vickers <lucas@localprojects.net> wrote:
> I'm trying to figure out how I can get full trace out of the Exception
> passed down from the error handler so that I can internally have a log that
> includes the original exception and a traceback, all while only rendering a
> happy error page for the user.
>
> so how can I take the error and get the stacktrace and original exception.
> This is almost a Python question not a Flask question -- but I'm also
> curious if anyone has a more clever way to do this.

Try this:

current_app.logger.exception(error)

More info on logging:

http://docs.python.org/2/library/logging.html

-Steve

Re: [flask] Re: Logging exceptions

From:
Lucas Vickers
Date:
2013-04-30 @ 21:45
current_app.logger.exception(error)

is exactly what I was looking for.  Thanks


On Tue, Apr 30, 2013 at 3:54 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Tue, Apr 30, 2013 at 12:43 PM, Lucas Vickers <lucas@localprojects.net>
> wrote:
> > I'm trying to figure out how I can get full trace out of the Exception
> > passed down from the error handler so that I can internally have a log
> that
> > includes the original exception and a traceback, all while only
> rendering a
> > happy error page for the user.
> >
> > so how can I take the error and get the stacktrace and original
> exception.
> > This is almost a Python question not a Flask question -- but I'm also
> > curious if anyone has a more clever way to do this.
>
> Try this:
>
> current_app.logger.exception(error)
>
> More info on logging:
>
> http://docs.python.org/2/library/logging.html
>
> -Steve
>