如何让Django将调试信息打印到控制台? [英] How can I get Django to print the debug information to the console?

查看:3799
本文介绍了如何让Django将调试信息打印到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用urlib命中我的应用程序不是一个浏览器,所以我发现错误时看不到调试屏幕。将正常调试信息发送到控制台或文件的最佳方法是什么?

I'm using urlib to hit my app not a browser so I can't see the debug screen when an error occurs. What's the best way to send the normal debug info to the console or a file?

编辑:我已经捕获了页面的输出,但是当我打印到屏幕上填满了无用的html。我可以得到错误吗?

I'm already catching the output of the page but when I print that to the screen it's filled with tons of useless html. can I get just the error?

推荐答案

更新 - 2016



这个帖子还是得到命中。

Update - 2016

This post is still getting hits.

我建议使用下面的一种使用内置日志记录系统的方法。

I recommend using one of the approaches below that use the built in logging system.

以同样的方式将异常传送到管理员电子邮件处理程序,您可以将数据发送到控制台记录器,文件记录器或其他任何地方(单独或另外)对于电子邮件处理程序。)

In the same way exceptions are piped to the admin email handler, you can send the data to a console logger, file logger, or anywhere else (solely, or in addition to the email handler.)

最简单的方法是将调试模式设置为OFF让django给你发送错误,因为这个字面上需要5秒钟:
http://docs.djangoproject.com/en/dev/howto/error-reporting/

Well, the easiest way is to set debug mode OFF and let django email you the error, since that literally takes 5 seconds: http://docs.djangoproject.com/en/dev/howto/error-reporting/

否则,我会写一个记录日志的中间件例外,但如果您想要堆栈跟踪,那么它有点疼痛。 这是文档。

Otherwise, I'd write a middleware that logs the exception, but it's a little more of a pain if you want the stack trace. Here's the documentation.

import traceback
import sys
from __future__ import print_function

class ProcessExceptionMiddleware(object):
    def process_exception(self, request, exception):
        # Just print the exception object to stdout
        print(exception)

        # Print the familiar Python-style traceback to stderr
        traceback.print_exc()

        # Write the traceback to a file or similar
        myfile.write(''.join(traceback.format_exception(*sys.exc_info())))

这篇关于如何让Django将调试信息打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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