| 1 | package Toadfarm::Plugin::AccessLog; | ||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | sub register { | ||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 6 | 1 | my ($self, $app, $config) = @_; | |||||||||||||||||||
| 7 | 1 | my $log = $app->log; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | $app->hook( | ||||||||||||||||||||
| 10 | 5 | ▶ | before_dispatch => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 11 | 5 | my $tx = $_[0]->tx; | |||||||||||||||||||
| 12 | 5 | my ($req, $timeout, $url); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 5 | $tx->req->env->{t0} = [gettimeofday]; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 5 | ▶ | if (my $stream = Mojo::IOLoop->stream($tx->connection)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | 5 | ▶ | $stream->on(timeout => sub { $timeout = 1 }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | $tx->on( | ||||||||||||||||||||
| 21 | 5 | ▶ | finish => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||
| 22 | 5 | my $tx = shift; | |||||||||||||||||||
| 23 | 5 | my $code = $tx->res->code; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 5 | ▶ | $code ||= 504 if $timeout; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$code ||= 504
MC/DC0%0/1
$code ||= 504
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||
| 26 | 5 | ▶ | $code or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 4 | $req = $tx->req; | |||||||||||||||||||
| 28 | 4 | $url = $req->url->clone->to_abs->userinfo(undef); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 4 | unshift @{$url->path->parts}, @{$url->base->path->parts}; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | $log->info( | ||||||||||||||||||||
| 33 | sprintf '[%s] %s %s %s %s %.4fs', | ||||||||||||||||||||
| 34 | $req->request_id, $req->env->{identity} || $tx->remote_address, | ||||||||||||||||||||
| 35 | 4 | ▶ | $req->method, $url, $code, tv_interval($req->env->{t0}), | ||||||||||||||||||
|
Condition66%2/3
$req->env->{'identity'} || $tx->remote_address
MC/DC0%0/2
$req->env->{'identity'} || $tx->remote_address
$req->env->{'identity'}
$tx->remote_address
Truth table66%2/3
A || B
A= $req->env->{'identity'}B= $tx->remote_address
| |||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | 5 | ); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | 1 | ); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | 1; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =encoding utf8 | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =head1 NAME | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | Toadfarm::Plugin::AccessLog - Log requests | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | #!/usr/bin/env perl | ||||||||||||||||||||
| 54 | use Toadfarm -init; | ||||||||||||||||||||
| 55 | # mount applications, set up logging, ... | ||||||||||||||||||||
| 56 | plugin "Toadfarm::Plugin::AccessLog"; | ||||||||||||||||||||
| 57 | start; | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head1 DESCRIPTION | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | This module will log the request with "info" log level. The log format | ||||||||||||||||||||
| 62 | is subject for change. For now it is: | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | $remote_address $http_method $url $status_code | ||||||||||||||||||||
| 65 | 1.2.3.4 GET http://localhost/favicon.ico 200 | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | See also L<Mojolicious::Plugin::AccessLog> if you think this plugin is too | ||||||||||||||||||||
| 68 | limiting. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =head1 METHODS | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head2 register | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | Register an "around_dispatch" hook which will log the request. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =head1 AUTHOR | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||