作者: , 出处:IT专家网社区, 责任编辑: 叶江,
2007-06-21 11:35
本文将演示如何使用XMLHttpRequest来添加一些以前不可能的、行不通的特性,它同时还减少了错误,提高了产品质量……
现在你已经知道如何把日志记录器集成到HTML页面中了,剩余的工作就是定义一种接收和转换消息的方法了。我选择使用最底层的通用命名方法,在Perl中建立了一个CGI脚本,这个脚本使用了我喜欢的一些模块,它使用XML::Simple来分析post数据,使用CGI::Carp把结果直接导入到httpd错误日志,这样可以节约系统管理员的时间,因为他不需要查看另外一个日志了。这个脚本还包含了很多良好的示例,它们适当地记录了不同的成功和失败条件。
| use CGI; use CGI::Carp qw(set_progname); use XML::Simple; my $request = CGI->new(); my $method = $request->request_method(); # 方法必须是POST if ($method eq ’POST’) { eval { my $content_type = $request->content_type(); if ($content_type eq ’text/xml’) { print $request->header(-status =>’415 Unsupported Media Type’, -type => ’text/xml’); croak "Invalid content type: $content_type\n"; } # 如果方法是POST,内容既不是URL编码也不是多部分形式, #那么整个post会被填充到一个参数中:POSTDATA。 my $error_xml = $request->param(’POSTDATA’); my $ref = XML::Simple::XMLin($error_xml); my ($name, $msg, $location) =($ref->{’name’}, $ref->{’message’}, ’’); $location = $ref->{’location’} if (defined($ref->{’location’})); # 改变日志中的名字 set_progname(’Client-side error’); my $remote_host = $request->remote_host(); carp "name: [$name], msg: [$msg], location: [$location]"; }; if ($@) { print $request->header(-status => ’500 Internal server error’,-type => ’text/xml’); croak "Error while logging: $@"; } else { # 这部分响应代码表明操作成功了,但是客户端不应该期待任何内容 print $request->header(-status => ’204 No content’,-type => ’text/xml’); } } else { print $request->header(-status => ’405 Method not supported’,-type => ’text/xml’); croak "Unsupported method: $method"; } |
已经完成了!现在,当某些难以理解的JavaScript进入系统的时候,你就可以期待着自己的日志监视器开始闪红灯,你的客户端开发人员在深夜接到电话了。

