| 1 | package PAGI::Middleware::AccessLog; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::AccessLog::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | PAGI::Middleware::AccessLog - Request logging middleware | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $app = builder { | ||||||||||||||||||||||||||||
| 19 | enable 'AccessLog', | ||||||||||||||||||||||||||||
| 20 | logger => sub { print STDERR @_ }, | ||||||||||||||||||||||||||||
| 21 | format => 'combined'; | ||||||||||||||||||||||||||||
| 22 | $my_app; | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | PAGI::Middleware::AccessLog logs HTTP requests in configurable formats. | ||||||||||||||||||||||||||||
| 28 | It captures client IP, method, path, status, response size, and timing. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =over 4 | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item * logger (default: warns to STDERR) | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | A coderef that receives the formatted log line. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =item * format (default: 'combined') | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Log format: 'combined', 'common', or 'tiny'. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =back | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 47 | 4 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $self->{logger} = $config->{logger} // sub { warn @_ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$config->{"logger"} // sub {
warn @_;
}
MC/DC0%0/2
$config->{"logger"} // sub {
warn @_;
}
$config->{"logger"}
sub {
warn @_;
}
Truth table33%1/3
A // B
A= $config->{"logger"}B= sub {
warn @_;
}
| |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | $self->{format} = $config->{format} // 'combined'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"format"} // "combined"
MC/DC100%1/1
$config->{"format"} // "combined"
$config->{"format"}
Truth table100%2/2
A
A= $config->{"format"}
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 4 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 57 | 4 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 58 | # Only log HTTP requests | ||||||||||||||||||||||||||||
| 59 | 4 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 61 | 1 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | my $start_time = time(); | |||||||||||||||||||||||||||
| 65 | 3 | my $status; | |||||||||||||||||||||||||||
| 66 | 3 | my $response_size = 0; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Intercept send to capture response info | ||||||||||||||||||||||||||||
| 69 | 6 | ▶ | my $wrapped_send = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 70 | 6 | my ($event) = @_; | |||||||||||||||||||||||||||
| 71 | 6 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | 3 | $status = $event->{status}; | |||||||||||||||||||||||||||
| 73 | } elsif ($event->{type} eq 'http.response.body') { | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | $response_size += length($event->{body} // ''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"body"} // ""
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 6 | await $send->($event); | |||||||||||||||||||||||||||
| 77 | 3 | }; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Run the inner app | ||||||||||||||||||||||||||||
| 80 | eval { | ||||||||||||||||||||||||||||
| 81 | 3 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||||||||||||||||
| 82 | 3 | 1; | |||||||||||||||||||||||||||
| 83 | 3 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | my $error = $@; | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | $status //= 500; | ||||||||||||||||||||||||||
|
Condition0%0/2
$status //= 500
MC/DC0%0/1
$status //= 500
$status
Truth table0%0/2
A
A= $status
| |||||||||||||||||||||||||||||
| 86 | 0 | $self->_log_request($scope, $status, $response_size, $start_time); | |||||||||||||||||||||||||||
| 87 | 0 | die $error; | |||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Log the request | ||||||||||||||||||||||||||||
| 91 | 3 | $self->_log_request($scope, $status, $response_size, $start_time); | |||||||||||||||||||||||||||
| 92 | 4 | }; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | ▶ | sub _log_request { | ||||||||||||||||||||||||||
|
Subroutine
_log_request: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 3 | my ($self, $scope, $status, $size, $start_time) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | my $duration = time() - $start_time; | |||||||||||||||||||||||||||
| 99 | 3 | my $line = $self->_format_log($scope, $status, $size, $duration); | |||||||||||||||||||||||||||
| 100 | 3 | $self->{logger}->($line); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | ▶ | sub _format_log { | ||||||||||||||||||||||||||
|
Subroutine
_format_log: called CC=16 SCAR=28.5
| |||||||||||||||||||||||||||||
| 104 | 3 | my ($self, $scope, $status, $size, $duration) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | my $format = $self->{format}; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Extract info from scope | ||||||||||||||||||||||||||||
| 109 | 3 | ▶ | my $client_ip = exists $scope->{client} ? ($scope->{client}[0] // '-') : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$scope->{"client"}[0] // "-"
MC/DC0%0/1
$scope->{"client"}[0] // "-"
$scope->{"client"}[0]
Truth table50%1/2
A
A= $scope->{"client"}[0]
| |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | my $method = $scope->{method} // '-'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"method"} // "-"
MC/DC0%0/1
$scope->{"method"} // "-"
$scope->{"method"}
Truth table50%1/2
A
A= $scope->{"method"}
| |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | my $path = $scope->{path} // '/'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"path"} // "/"
MC/DC0%0/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table50%1/2
A
A= $scope->{"path"}
| |||||||||||||||||||||||||||||
| 112 | 3 | my $query = $scope->{query_string}; | |||||||||||||||||||||||||||
| 113 | 3 | ▶ | my $full_path = defined $query && $query ne '' ? "$path?$query" : $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $query and $query ne ""
MC/DC50%1/2
defined $query and $query ne ""
defined $query
$query ne ""
Truth table66%2/3
A and B
A= defined $queryB= $query ne ""
| |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | my $protocol = 'HTTP/' . ($scope->{http_version} // '1.1'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"http_version"} // "1.1"
MC/DC100%1/1
$scope->{"http_version"} // "1.1"
$scope->{"http_version"}
Truth table100%2/2
A
A= $scope->{"http_version"}
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Get headers | ||||||||||||||||||||||||||||
| 117 | 3 | my $referer = '-'; | |||||||||||||||||||||||||||
| 118 | 3 | my $user_agent = '-'; | |||||||||||||||||||||||||||
| 119 | 3 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||
| 120 | 2 | my $name = lc($h->[0]); | |||||||||||||||||||||||||||
| 121 | 2 | ▶ | if ($name eq 'referer') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | 1 | $referer = $h->[1]; | |||||||||||||||||||||||||||
| 123 | } elsif ($name eq 'user-agent') { | ||||||||||||||||||||||||||||
| 124 | 1 | $user_agent = $h->[1]; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Format timestamp | ||||||||||||||||||||||||||||
| 129 | 3 | my $timestamp = strftime('%d/%b/%Y:%H:%M:%S %z', localtime()); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | ▶ | $status //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$status //= 0
MC/DC0%0/1
$status //= 0
$status
Truth table50%1/2
A
A= $status
| |||||||||||||||||||||||||||||
| 132 | 3 | ▶ | $size //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$size //= 0
MC/DC0%0/1
$size //= 0
$size
Truth table50%1/2
A
A= $size
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 3 | ▶ | if ($format eq 'combined') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 135 | # Combined Log Format (Apache/nginx style) | ||||||||||||||||||||||||||||
| 136 | 1 | return sprintf( | |||||||||||||||||||||||||||
| 137 | qq{%s - - [%s] "%s %s %s" %d %d "%s" "%s" %.3fs\n}, | ||||||||||||||||||||||||||||
| 138 | $client_ip, $timestamp, $method, $full_path, $protocol, | ||||||||||||||||||||||||||||
| 139 | $status, $size, $referer, $user_agent, $duration | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | } elsif ($format eq 'common') { | ||||||||||||||||||||||||||||
| 142 | # Common Log Format | ||||||||||||||||||||||||||||
| 143 | 1 | return sprintf( | |||||||||||||||||||||||||||
| 144 | qq{%s - - [%s] "%s %s %s" %d %d\n}, | ||||||||||||||||||||||||||||
| 145 | $client_ip, $timestamp, $method, $full_path, $protocol, | ||||||||||||||||||||||||||||
| 146 | $status, $size | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||
| 149 | # Tiny format | ||||||||||||||||||||||||||||
| 150 | 1 | return sprintf( | |||||||||||||||||||||||||||
| 151 | "%s %s %d %.3fs\n", | ||||||||||||||||||||||||||||
| 152 | $method, $full_path, $status, $duration | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||