| 1 | package Plack::Middleware::AccessLog::Structured; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | $Plack::Middleware::AccessLog::Structured::VERSION = '0.002000'; | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use parent qw(Plack::Middleware); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Access log middleware which creates structured log messages | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Time::Moment; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Plack::Util::Accessor qw(logger callback extra_field); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Net::Domain qw(hostname hostfqdn); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | my ($class, $arg_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | my $self = $class->next::method($arg_ref); | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | ▶ | if (defined $self->callback() && ref $self->callback() ne 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->callback and ref $self->callback ne "CODE"
MC/DC100%2/2
defined $self->callback and ref $self->callback ne "CODE"
defined $self->callback
ref $self->callback ne "CODE"
Truth table100%3/3
A and B
A= defined $self->callbackB= ref $self->callback ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | croak("Passed 'callback' parameter must be a code reference"); | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | if (defined $self->extra_field() && ref $self->extra_field() ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->extra_field and ref $self->extra_field ne "HASH"
MC/DC100%2/2
defined $self->extra_field and ref $self->extra_field ne "HASH"
defined $self->extra_field
ref $self->extra_field ne "HASH"
Truth table100%3/3
A and B
A= defined $self->extra_fieldB= ref $self->extra_field ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | croak("Passed 'extra_field' parameter must be a hash reference"); | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 5 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 5 | my ($self, $env) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | my $t_before = Time::Moment->now_utc(); | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | my $res = $self->app->($env); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | return $self->response_cb($res, sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | my ($cb_res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | my $t_after = Time::Moment->now_utc(); | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | my $h = Plack::Util::headers($cb_res->[1]); | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | my $content_type = $h->get('Content-Type'); | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | my $log_entry = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | class => ref($self), | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Request data | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | remote_addr => $env->{REMOTE_ADDR}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | request_method => _safe($env->{REQUEST_METHOD}), | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | request_uri => _safe($env->{REQUEST_URI}), | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | server_protocol => $env->{SERVER_PROTOCOL}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | http_user_agent => _safe($env->{HTTP_USER_AGENT}), | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | http_host => $env->{HTTP_HOST} || $env->{SERVER_NAME}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | http_referer => $env->{HTTP_REFERER}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | remote_user => $env->{REMOTE_USER}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # Server information | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | pid => $$, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$env->{'HTTP_HOST'} || $env->{'SERVER_NAME'}
Plack::Util::content_length($cb_res->[2]) || $h->get('Content-Length')
MC/DC0%0/2
$env->{'HTTP_HOST'} || $env->{'SERVER_NAME'}
$env->{'HTTP_HOST'}
$env->{'SERVER_NAME'}
MC/DC0%0/2
Plack::Util::content_length($cb_res->[2]) || $h->get('Content-Length')
Plack::Util::content_length($cb_res->[2])
$h->get('Content-Length')
Truth table33%1/3
A || B
A= $env->{'HTTP_HOST'}B= $env->{'SERVER_NAME'}
Truth table33%1/3
A || B
A= Plack::Util::content_length($cb_res->[2])B= $h->get('Content-Length')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | hostfqdn => hostfqdn(), | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | hostname => hostname(), | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Response data | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | response_status => $cb_res->[0], | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | content_length => Plack::Util::content_length($cb_res->[2]) || $h->get('Content-Length'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | content_type => defined $content_type ? "$content_type" : undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Timing | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | request_duration => ( $t_before->delta_microseconds($t_after) / 1000 ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | date => $t_before->strftime('%FT%T%3fZ'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | epochtime => $t_before->strftime('%s.%3N'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | ▶ | if ($self->extra_field()) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | for my $env_field (keys %{$self->extra_field}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | $log_entry->{$self->extra_field()->{$env_field}} | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | = $env->{$env_field}; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | if ($self->callback()) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | $log_entry = $self->callback()->($env, $log_entry); | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $logger = $self->logger() || sub { $env->{'psgi.errors'}->print($_[0] . "\n") }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$self->logger || sub {
$env->{'psgi.errors'}->print($_[0] . "\n");
}
MC/DC0%0/2
$self->logger || sub {
$env->{'psgi.errors'}->print($_[0] . "\n");
}
$self->logger
sub {
$env->{'psgi.errors'}->print($_[0] . "\n");
}
Truth table33%1/3
A || B
A= $self->loggerB= sub {
$env->{'psgi.errors'}->print($_[0] . "\n");
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 5 | $logger->(encode_json($log_entry)); | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Taken from Plack::Middleware::AccessLog | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 15 | ▶ | sub _safe { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_safe: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 15 | my ($string) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $string =~ s/([^[:print:]])/"\\x" . unpack("H*", $1)/xeg | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | if defined $string; | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 15 | return $string; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||