SOAP::Lite 记录传输请求/响应以及自定义标识符

huangapple go评论62阅读模式
英文:

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');

huangapple
  • 本文由 发表于 2023年2月19日 01:46:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定