简体   繁体   English

在网站上记录python错误?

[英]Logging python errors on website?

I'm new to cgi and python, so I've been making quite a few mistakes. 我是cgi和python的新手,所以我犯了很多错误。 The problem is that if something goes wrong with the script, I just get a 500 error on the webpage. 问题是,如果脚本出错,我只会在网页上看到500错误。 The only way I can see what caused the error is by executing the page via ssh, but the page involves file uploads, so I can't test that part. 我可以看到导致错误的唯一方法是通过ssh执行页面,但是该页面涉及文件上传,因此我无法测试该部分。

Is there a way I can output Python errors to a file? 有什么方法可以将Python错误输出到文件吗?

there are a couple of options, use the logging module as directed, you can tail the server's error log, and you can enable cgitb with import cgitb; cgitb.enable() 有两个选项,可以按照指示使用日志记录模块,可以跟踪服务器的错误日志,还可以使用import cgitb; cgitb.enable()启用import cgitb; cgitb.enable() import cgitb; cgitb.enable()

Depending on exactly where the error occurs, the error will show up in different places, so checking all three, and using print statements and exception blocks helps to debug your code. 根据错误发生的确切位置,错误将显示在不同的位置,因此检查所有三个位置,并使用print语句和异常块有助于调试代码。

With file uploads, I've found I have to explicitly state enctype="multipart/form-data" in the form tag or it breaks, often quietly. 通过文件上传,我发现我必须在form标记中明确声明enctype="multipart/form-data" ,否则它通常会安静地中断。

Yes there is. 就在这里。 Python has a logging module which allows you to do just what you want. Python有一个日志记录模块,它允许您执行所需的操作。 Here you can read the documentation, it is so easy to understand. 在这里您可以阅读文档,它很容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM