使用 wsgi 将 stdout 和 stderr 重定向到 Django 中的文件
我试图让 django 在共享主机中与 wsgi 一起工作,所以我无法访问服务器日志,
我试图将输出重定向到我的 django.wsgi 脚本中的文件,如下所示:
saveout = sys.stdout
log_out = open('out.log', 'w')
sys.stdout = log_out
但这是我的错误(在我可以访问的唯一错误日志中)
[Thu Oct 07 19:18:08 2010] [error] [client <ip>] File "/home/myuser/public_html/django.wsgi", line 9, in <module>
[Thu Oct 07 19:18:08 2010] [error] [client <ip>] mod_wsgi (pid=30677): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
,这就是我能得到的所有错误,
第 9 行是尝试打开文件的错误。我尝试过创建之前的文件,为每个人提供写入权限,而没有之前创建的文件..什么也没有。 在应用程序中,我只收到 500 服务器错误。
有没有一种正确的方法可以在不访问 apache 日志的情况下获取此输出?
编辑:
使用绝对路径,我可以打开文件并写入它们,但是除非我最终关闭文件,否则我会得到空文件,有必要吗?我可以让文件保持打开状态吗?..
关于最初的问题,即使我使用了异常处理,在脚本末尾写入“完成确定”文件,然后关闭文件,但仍然收到错误:
[Thu Oct 07 13:33:07 2010] [error] [client <ip>] mod_wsgi (pid=32541): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
如果我故意引发错误,它被记录在文件中,所以当我得到“ok”时,我认为脚本工作正常,但结果仍然是该消息错误......没有线索
I'm trying to make django work with wsgi in a shared hosting, so I cannot access server logs,
I'm trying to redirect the output to a file in my django.wsgi script, like this:
saveout = sys.stdout
log_out = open('out.log', 'w')
sys.stdout = log_out
but this is the error that I have (in the only error log that I can access)
[Thu Oct 07 19:18:08 2010] [error] [client <ip>] File "/home/myuser/public_html/django.wsgi", line 9, in <module>
[Thu Oct 07 19:18:08 2010] [error] [client <ip>] mod_wsgi (pid=30677): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
and that is all the error I can get
Line 9 is the one that tries to open file. I've tried creating previously the file, giving writing access to everyone, without the file previously created.. and nothing.
In the app, I only get a 500 server error.
Is there a proper way to get this output without access to apache logs?
Edited:
Using absolute paths, I can open files and write to them, but I get empty files unless I close the files in the end, is it necessary? Could I leave the files open?..
Regarding to the initial problem, even though I used exception handling, write a "finish ok" to file in the end of my script, then close the file, but still get the error:
[Thu Oct 07 13:33:07 2010] [error] [client <ip>] mod_wsgi (pid=32541): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
If I intentionally raise an error, it is logged in the file, so as i get "ok" I think the script worked fine but the result is still that message error... no clues
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在 WSGI 应用程序中使用像
out.log
这样的相对路径名。 WSGI 不指定工作目录将指向哪里,并且 mod_wsgi 不会设置它,除非在守护程序模式下特别要求。使用基于应用程序根目录的绝对路径名。如果需要,您可以使用 os.path.dirname(__file__) 来获取包含当前脚本/模块的目录。
You can't use relative pathnames like
out.log
in a WSGI application. WSGI doesn't specify where the working directory will be pointing and mod_wsgi doesn't set it unless specifically asked to in daemon mode.Use an absolute pathname based on your application's root. You can use
os.path.dirname(__file__)
to get the directory containing the current script/module if needed.