File Coverage

blib/lib/Plack/Middleware/AxsLog.pm
Criterion Covered Total %
statement 80 81 98.7
branch 32 34 94.1
condition 15 18 83.3
subroutine 15 15 100.0
pod 2 3 66.6
total 144 151 95.3


line stmt bran cond sub pod time code
1             package Plack::Middleware::AxsLog;
2              
3 6     6   30259 use strict;
  6         14  
  6         601  
4 6     6   38 use warnings;
  6         9  
  6         1010  
5 6     6   97 use 5.8.5;
  6         25  
  6         324  
6 6     6   870 use parent qw/Plack::Middleware/;
  6         297  
  6         74  
7 6     6   43448 use Plack::Util;
  6         15  
  6         166  
8 6     6   13637 use Time::HiRes qw/gettimeofday/;
  6         16554  
  6         36  
9 6     6   1861 use Plack::Util::Accessor qw/response_time combined ltsv format format_options compiled_format error_only long_response_time logger/;
  6         14  
  6         73  
10 6     6   10136 use POSIX qw//;
  6         68185  
  6         184  
11 6     6   6097 use Time::Local qw//;
  6         11649  
  6         160  
12 6     6   1002 use HTTP::Status qw//;
  6         4703  
  6         136  
13 6     6   5896 use Apache::LogFormat::Compiler;
  6         53234  
  6         4384  
14              
15             our $VERSION = '0.21';
16              
17             sub prepare_app {
18 17     17 1 19553 my $self = shift;
19 17 100       87 $self->combined(1) if ! defined $self->combined;
20 17 100       757 $self->response_time(0) if ! defined $self->response_time;
21 17 100       184 $self->error_only(0) if ! defined $self->error_only;
22 17 100       254 $self->long_response_time(0) if ! defined $self->long_response_time;
23              
24 17         229 my ($format, %format_options);
25 17 100       65 if ( $self->format ) {
    100          
    100          
26 2         19 $format = $self->format;
27 2 100       15 %format_options = %{ $self->format_options } if $self->format_options;
  1         11  
28             }
29             elsif ( $self->ltsv ) {
30 2         31 $format = join "\t",
31             qw!host:%h user:%u time:%t req:%r status:%>s size:%b referer:%{Referer}i ua:%{User-agent}i!;
32 2 100       6 $format .= "\t" . 'taken:%D' if $self->response_time;
33             }
34             elsif ( $self->combined ) {
35 9         138 $format = q!%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"!;
36 9 100       24 $format .= ' %D' if $self->response_time;
37             }
38             else {
39 4         56 $format = q!%h %l %u %t "%r" %>s %b!;
40 4 100       12 $format .= ' %D' if $self->response_time;
41             }
42              
43 17         217 $self->compiled_format(Apache::LogFormat::Compiler->new($format, %format_options)->code_ref);
44             }
45              
46             sub call {
47 17     17 1 167824 my $self = shift;
48 17         42 my($env) = @_;
49              
50 17         127 my $t0 = [gettimeofday];
51              
52 17         129 my $res = $self->app->($env);
53 17 100 66     5005468 if ( ref($res) && ref($res) eq 'ARRAY' ) {
54 15         126 my $length = Plack::Util::content_length($res->[2]);
55 15 50       338 if ( defined $length ) {
56 15         82 $self->log_line($t0, $env,$res,$length);
57 15         246 return $res;
58             }
59             }
60             Plack::Util::response_cb($res, sub {
61 2     2   86 my $res = shift;
62 2         8 my $length = Plack::Util::content_length($res->[2]);
63 2 100       19 if ( defined $length ) {
64 1         4 $self->log_line($t0, $env,$res,$length);
65 1         6 return;
66             }
67             return sub {
68 2         169 my $chunk = shift;
69 2 100       8 if ( ! defined $chunk ) {
70 1         35 $self->log_line($t0, $env,$res,$length);
71 1         7 return;
72             }
73 1         3 $length += length($chunk);
74 1         9 return $chunk;
75 1         7 };
76 2         16 });
77             }
78              
79             sub log_line {
80 17     17 0 29 my $self = shift;
81 17         45 my ($t0, $env, $res, $length) = @_;
82              
83 17         92 my $elapsed = int(Time::HiRes::tv_interval($t0) * 1_000_000);
84              
85 17 100 100     533 unless (
      100        
      66        
      100        
      66        
86             ( $self->{long_response_time} == 0 && !$self->{error_only} )
87             || ( $self->{long_response_time} != 0 && $elapsed >= $self->{long_response_time} )
88             || ( $self->{error_only} && HTTP::Status::is_error($res->[0]) )
89             ) {
90 3         23 return;
91             }
92 14         659 my $log_line = $self->{compiled_format}->(
93             $env,
94             $res,
95             $length,
96             $elapsed,
97             $t0->[0],
98             );
99              
100 14 50       4943 if ( ! $self->{logger} ) {
101 0         0 $env->{'psgi.errors'}->print($log_line."\n");
102             }
103             else {
104 14         79 $self->{logger}->($log_line."\n");
105             }
106             }
107              
108              
109             1;
110             __END__