将CGI错误输出从STDERR重定向到文件(python AND perl) [英] Redirecting CGI error output from STDERR to a file (python AND perl)

查看:146
本文介绍了将CGI错误输出从STDERR重定向到文件(python AND perl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将网站移至Hostmonster,并询问服务器日志的位置,以便我可以自动对其进行扫描以查找CGI错误。有人告诉我,很抱歉,但您访问的任何文件都没有cgi错误。

I'm moving a website to Hostmonster and asked where the server log is located so I can automatically scan it for CGI errors. I was told, "We're sorry, but we do not have cgi errors go to any files that you have access to."

由于组织原因,我陷入了困境使用Hostmonster和这个糟糕的策略,因此,作为一种解决方法,我想我可能会修改CGI脚本以将STDERR重定向到自定义日志文件。

For organizational reasons I'm stuck with Hostmonster and this awful policy, so as a workaround I thought maybe I'd modify the CGI scripts to redirect STDERR to a custom log file.

我有很多脚本(269),因此我需要在Python和Perl中都提供一种简单的方法来将STDERR重定向到自定义日志文件。

I have a lot of scripts (269) so I need an easy way in both Python and Perl to redirect STDERR to a custom log file.

如果要对文件锁定进行显式或隐式处理,很棒,因为理论上如果一个以上的脚本同时失败,共享的CGI错误日志文件可以被多个脚本一次写入。

Something that accounts for file locking either explicitly or implicitly would be great, since a shared CGI error log file could theoretically be written to by more than one script at once if more than one script fails at the same time.

(I想要使用共享的错误日志,以便我可以每晚通过电子邮件将其内容发送给自己,然后将其存档或删除。)

(I want to use a shared error log so I can email its contents to myself nightly and then archive or delete it.)

我知道我可能必须修改每个文件(grrr ),这就是为什么我要寻找仅需几行代码的优雅内容。谢谢。

I know I may have to modify each file (grrr), that's why I'm looking for something elegant that will only be a few lines of code. Thanks.

推荐答案

我最终使用的解决方案类似于以下内容,位于我所有脚本的顶部附近:

The solution I finally went with was similar to the following, near the top of all my scripts:

Perl:

open(STDERR,">>","/path/to/my/cgi-error.log")
    or die "Could not redirect STDERR: $OS_ERROR";

Python:

sys.stderr = open("/path/to/my/cgi-error.log", "a")

显然,在Perl中,不需要重新打开STDERR句柄。

Apparently in Perl you don't need to close the STDERR handle before reopening it.

通常我会最好关闭它练习,但是正如我在问题中说的那样,我有269个脚本,并且我正在尝试最小化更改。 (另外,听起来似乎很可怕,只是重新打开打开的文件句柄。)

Normally I would close it anyway as a best practice, but as I said in the question, I have 269 scripts and I'm trying to minimize the changes. (Plus it seems more Perlish to just re-open the open filehandle, as awful as that sounds.)

如果将来有人遇到类似的情况,这就是我要立即更新所有脚本:

In case anyone else has something similar in the future, here's what I'm going to do for updating all my scripts at once:

Perl:

find . -type f -name "*.pl" -exec perl -pi.bak -e 's%/usr/bin/perl%/usr/bin/perl\nopen(STDERR,">>","/path/to/my/cgi-error.log")\n    or die "Could not redirect STDERR: \$OS_ERROR";%' {} \;

Python:

find . -type f -name "*.py" -exec perl -pi.bak -e 's%^(import os, sys.*)%$1\nsys.stderr = open("/path/to/my/cgi-error.log", "a")%' {} \;

我发布这些命令的原因是,我花了很多句法按摩才能获得这些命令起作用(例如,将不能更改为不能,将#!/ usr / bin / perl更改为仅/ usr / bin / perl,以便外壳程序不会将!解释为历史字符,请使用$ OS_ERROR而不是$!等)

The reason I'm posting these commands is that it took me quite a lot of syntactical massaging to get those commands to work (e.g., changing Couldn't to Could not, changing #!/usr/bin/perl to just /usr/bin/perl so the shell wouldn't interpret ! as a history character, using $OS_ERROR instead of $!, etc.)

感谢所有发表评论的人。由于没有人同时回答Perl和Python,因此我无法真正接受任何给出的答案,但是我确实投票给那些引导我朝着正确方向的人。再次感谢!

Thanks to everyone who commented. Since no one answered for both Perl and Python I couldn't really "accept" any of the given answers, but I did give votes to the ones which led me in the right direction. Thanks again!

这篇关于将CGI错误输出从STDERR重定向到文件(python AND perl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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