| 1 | package Plack::Middleware::SimpleLogger; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use parent qw(Plack::Middleware); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Config (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Plack::Util::Accessor qw(level); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use POSIX (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Scalar::Util (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | # Should this be in Plack::Util? | ||||||||||||
| 10 | 1 | my $i = 0; | |||||||||||
| 11 | 1 | my %level_numbers = map { $_ => $i++ } qw(debug info warn error fatal); | |||||||||||
| 12 | |||||||||||||
| 13 | 1 | ▶ | sub call { | ||||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 14 | 1 | my($self, $env) = @_; | |||||||||||
| 15 | |||||||||||||
| 16 | 1 | ▶ | my $min = $level_numbers{ $self->level || "debug" }; | ||||||||||
|
Condition50%1/2
$self->level || 'debug'
MC/DC0%0/1
$self->level || 'debug'
$self->level
Truth table50%1/2
A
A= $self->level
| |||||||||||||
| 17 | |||||||||||||
| 18 | 1 | my $env_ref = $env; | |||||||||||
| 19 | 1 | Scalar::Util::weaken($env_ref); | |||||||||||
| 20 | |||||||||||||
| 21 | 2 | ▶ | $env->{'psgix.logger'} = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 22 | 2 | my $args = shift; | |||||||||||
| 23 | |||||||||||||
| 24 | 2 | ▶ | if ($level_numbers{$args->{level}} >= $min) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 1 | $env_ref->{'psgi.errors'}->print($self->format_message($args->{level}, $args->{message})); | |||||||||||
| 26 | } | ||||||||||||
| 27 | 1 | }; | |||||||||||
| 28 | |||||||||||||
| 29 | 1 | $self->app->($env); | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | 1 | ▶ | sub format_time { | ||||||||||
|
Subroutine
format_time: called CC=3 SCAR=13.0
Pod
undocumented
| |||||||||||||
| 33 | 1 | my $old_locale; | |||||||||||
| 34 | 1 | ▶ | if ( $Config::config{d_setlocale} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 0 | $old_locale = POSIX::setlocale(&POSIX::LC_ALL); | |||||||||||
| 36 | 0 | POSIX::setlocale(&POSIX::LC_ALL, 'C'); | |||||||||||
| 37 | } | ||||||||||||
| 38 | 1 | my $out = POSIX::strftime(@_); | |||||||||||
| 39 | 1 | ▶ | if ( $Config::config{d_setlocale} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 0 | POSIX::setlocale(&POSIX::LC_ALL, $old_locale); | |||||||||||
| 41 | }; | ||||||||||||
| 42 | 1 | return $out; | |||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 1 | ▶ | sub format_message { | ||||||||||
|
Subroutine
format_message: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 46 | 1 | my($self, $level, $message) = @_; | |||||||||||
| 47 | |||||||||||||
| 48 | 1 | my $time = format_time("%Y-%m-%dT%H:%M:%S", localtime); | |||||||||||
| 49 | 1 | sprintf "%s [%s #%d] %s: %s\n", uc substr($level, 0, 1), $time, $$, uc $level, $message; | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 1 | 1; | |||||||||||
| 53 | |||||||||||||
| 54 | __END__ | ||||||||||||