| 1 | package PAGI::Middleware::AccessLog; | ||
| 2 | $PAGI::Middleware::AccessLog::VERSION = '0.002001'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent 'PAGI::Middleware'; | ||
| 6 | use Future::AsyncAwait; | ||
| 7 | use Time::HiRes qw(time); | ||
| 8 | use POSIX qw(strftime); | ||
| 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 | sub _init { | ||
| 47 | my ($self, $config) = @_; | ||
| 48 | |||
| 49 | $self->{logger} = $config->{logger} // sub { warn @_ }; | ||
| 50 | $self->{format} = $config->{format} // 'combined'; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub wrap { | ||
| 54 | my ($self, $app) = @_; | ||
| 55 | |||
| 56 | return async sub { | ||
| 57 | my ($scope, $receive, $send) = @_; | ||
| 58 | # Only log HTTP requests | ||
| 59 | if ($scope->{type} ne 'http') { | ||
| 60 | await $app->($scope, $receive, $send); | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $start_time = time(); | ||
| 65 | my $status; | ||
| 66 | my $response_size = 0; | ||
| 67 | |||
| 68 | # Intercept send to capture response info | ||
| 69 | my $wrapped_send = async sub { | ||
| 70 | my ($event) = @_; | ||
| 71 | if ($event->{type} eq 'http.response.start') { | ||
| 72 | $status = $event->{status}; | ||
| 73 | } elsif ($event->{type} eq 'http.response.body') { | ||
| 74 | $response_size += length($event->{body} // ''); | ||
| 75 | } | ||
| 76 | await $send->($event); | ||
| 77 | }; | ||
| 78 | |||
| 79 | # Run the inner app | ||
| 80 | eval { | ||
| 81 | await $app->($scope, $receive, $wrapped_send); | ||
| 82 | 1; | ||
| 83 | } or do { | ||
| 84 | my $error = $@; | ||
| 85 | $status //= 500; | ||
| 86 | $self->_log_request($scope, $status, $response_size, $start_time); | ||
| 87 | die $error; | ||
| 88 | }; | ||
| 89 | |||
| 90 | # Log the request | ||
| 91 | $self->_log_request($scope, $status, $response_size, $start_time); | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _log_request { | ||
| 96 | my ($self, $scope, $status, $size, $start_time) = @_; | ||
| 97 | |||
| 98 | my $duration = time() - $start_time; | ||
| 99 | my $line = $self->_format_log($scope, $status, $size, $duration); | ||
| 100 | $self->{logger}->($line); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _format_log { | ||
| 104 | my ($self, $scope, $status, $size, $duration) = @_; | ||
| 105 | |||
| 106 | my $format = $self->{format}; | ||
| 107 | |||
| 108 | # Extract info from scope | ||
| 109 | my $client_ip = exists $scope->{client} ? ($scope->{client}[0] // '-') : '-'; | ||
| 110 | my $method = $scope->{method} // '-'; | ||
| 111 | my $path = $scope->{path} // '/'; | ||
| 112 | my $query = $scope->{query_string}; | ||
| 113 | my $full_path = defined $query && $query ne '' ? "$path?$query" : $path; | ||
| 114 | my $protocol = 'HTTP/' . ($scope->{http_version} // '1.1'); | ||
| 115 | |||
| 116 | # Get headers | ||
| 117 | my $referer = '-'; | ||
| 118 | my $user_agent = '-'; | ||
| 119 | for my $h (@{$scope->{headers} // []}) { | ||
| 120 | my $name = lc($h->[0]); | ||
| 121 | if ($name eq 'referer') { | ||
| 122 | $referer = $h->[1]; | ||
| 123 | } elsif ($name eq 'user-agent') { | ||
| 124 | $user_agent = $h->[1]; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | # Format timestamp | ||
| 129 | my $timestamp = strftime('%d/%b/%Y:%H:%M:%S %z', localtime()); | ||
| 130 | |||
| 131 | $status //= 0; | ||
| 132 | $size //= 0; | ||
| 133 | |||
| 134 | if ($format eq 'combined') { | ||
| 135 | # Combined Log Format (Apache/nginx style) | ||
| 136 | 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 | 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 | return sprintf( | ||
| 151 | "%s %s %d %.3fs\n", | ||
| 152 | $method, $full_path, $status, $duration | ||
| 153 | ); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ |