英文:
SOAP::Lite log transport request/response with custom identifier
问题
在上面的示例中,您想要将事务标识123abc
传递给您的日志记录器。要实现这一点,您可以在调用getWords
函数时将事务标识作为额外的参数传递,并在log_transport
函数中进行处理。以下是修改的代码部分,以传递事务标识:
getWords(444, '123abc');
然后,在log_transport
函数中,您可以通过检查HTTP::Request
对象的内容来提取事务标识,如下所示:
sub log_transport {
my ($in) = @_;
if (ref($in) eq "HTTP::Request") {
my $content = $in->content;
my $txn_id;
if ($content =~ /txn_id=(\w+)/) {
$txn_id = $1;
}
say STDERR Dumper(ref($in), $content);
say STDERR "Transaction ID: $txn_id" if defined $txn_id;
}
elsif (ref($in) eq "HTTP::Response") {
say STDERR Dumper(ref($in), $in->content);
}
}
这样,您可以在log_transport
函数中提取事务标识并进行记录,而不必使用$soap->outputxml(1)->call($method => @params)
。
英文:
I would like to log SOAP::Lite transport request/response contents using a custom identifier (e.g. a transaction-id or txn_id
in my example below):
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use SOAP::Lite +trace => [ transport => \&log_transport, ];
sub log_transport {
my ($in) = @_;
if (ref($in) eq "HTTP::Request") {
# INSERT INTO logs ( txn_id, request ) VALUES ( $tnx_id, $in->content )
say STDERR Dumper(ref($in), $in->content);
}
elsif (ref($in) eq "HTTP::Response") {
# UPDATE logs SET response = '$in->content' WHERE txn_id = $tnx_id
say STDERR Dumper(ref($in), $in->content);
}
}
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $soap = SOAP::Lite->proxy('https://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords');
$soap->serializer()->register_ns('http://www.dataaccess.com/webservicesserver/' ,"ns");
sub getWords
{
my ($number, $txn_id) = @_ ;
my $method = SOAP::Data->name("ns:NumberToWords");
my @params = ( SOAP::Data->name("ubiNum" => $number) );
my $response = $soap->call($method => @params);
if (!$response->fault) {
say STDOUT "NumberToWords = " . $response->result;
}
else {
say STDERR "error: " . (defined $response->faultstring? $response->faultstring : $soap->transport->status);
}
}
getWords(444, '123abc');
In my example above, how can I pass the transaction-id 123abc
to my logger?
P.S. I do not wish to use:
$soap->outputxml(1)->call($method => @params)
答案1
得分: 1
以下是代码的翻译部分:
它似乎[`SOAP::Trace`](https://metacpan.org/dist/SOAP-Lite/view/lib/SOAP/Trace.pod)传输回调不支持额外的参数传递。作为一种解决方法,您可以使用在外部范围中声明的词法变量,如下所示:
use strict;
use warnings;
use Data::Dumper;
my $TXN_ID;
use SOAP::Lite +trace => [ transport => \&log_transport, ];
sub log_transport {
my ($in) = @_;
say STDERR "记录事务ID:$TXN_ID:";
if (ref($in) eq "HTTP::Request") {
# 插入到日志中(txn_id, request)的值为($tnx_id, $in->content)
say STDERR Dumper(ref($in), $in->content);
}
elsif (ref($in) eq "HTTP::Response") {
# 更新日志,将响应设置为'$in->content',其中txn_id为$tnx_id
say STDERR Dumper(ref($in), $in->content);
}
}
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $soap = SOAP::Lite->proxy('https://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords');
$soap->serializer()->register_ns('http://www.dataaccess.com/webservicesserver/' ,"ns");
sub getWords
{
my ($number, $txn_id) = @_ ;
$TXN_ID = $txn_id;
my $method = SOAP::Data->name("ns:NumberToWords");
my @params = ( SOAP::Data->name("ubiNum" => $number) );
my $response = $soap->call($method => @params);
if (!$response->fault) {
say STDOUT "NumberToWords = " . $response->result;
}
else {
say STDERR "错误: " . (defined $response->faultstring? $response->faultstring : $soap->transport->status);
}
}
getWords(444, '123abc');
请注意,这是您提供的代码的翻译版本,不包含任何其他内容。
英文:
It does not seem like the SOAP::Trace
transport callback supports extra argument passing. As a workaround you could use a lexical variable declared in the outer scope like this:
use strict;
use warnings;
use Data::Dumper;
my $TXN_ID;
use SOAP::Lite +trace => [ transport => \&log_transport, ];
sub log_transport {
my ($in) = @_;
say STDERR "Logging transaction id: $TXN_ID:";
if (ref($in) eq "HTTP::Request") {
# INSERT INTO logs ( txn_id, request ) VALUES ( $tnx_id, $in->content )
say STDERR Dumper(ref($in), $in->content);
}
elsif (ref($in) eq "HTTP::Response") {
# UPDATE logs SET response = '$in->content' WHERE txn_id = $tnx_id
say STDERR Dumper(ref($in), $in->content);
}
}
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $soap = SOAP::Lite->proxy('https://www.dataaccess.com/webservicesserver/NumberConversion.wso?op=NumberToWords');
$soap->serializer()->register_ns('http://www.dataaccess.com/webservicesserver/' ,"ns");
sub getWords
{
my ($number, $txn_id) = @_ ;
$TXN_ID = $txn_id;
my $method = SOAP::Data->name("ns:NumberToWords");
my @params = ( SOAP::Data->name("ubiNum" => $number) );
my $response = $soap->call($method => @params);
if (!$response->fault) {
say STDOUT "NumberToWords = " . $response->result;
}
else {
say STDERR "error: " . (defined $response->faultstring? $response->faultstring : $soap->transport->status);
}
}
getWords(444, '123abc');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论