当我告诉它时,如何使用Django的记录器来记录回溯? [英] How do I use Django's logger to log a traceback when I tell it to?

查看:208
本文介绍了当我告诉它时,如何使用Django的记录器来记录回溯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try:
    print blah
except KeyError:
    traceback.print_exc()

我曾经像这样调试。我会打印到控制台。现在,我想记录一切,而不是打印,因为Apache不允许打印。所以,如何记录整个追溯?

I used to debug like this. I'd print to the console. Now, I want to log everything instead of print, since Apache doesn't allow printing. So, how do I log this entire traceback?

推荐答案

您可以使用python的日志记录机制:

You can use python's logging mechanism:

import logging
...

logger = logging.getLogger("blabla")
...

try:
    print blah # You can use logger.debug("blah") instead of print
except KeyError:
    logger.exception("An error occurred")

这将打印堆栈跟踪并使用apache。

This will print the stack trace and will work with apache.

这篇关于当我告诉它时,如何使用Django的记录器来记录回溯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆