| 1 | package Dancer::Logger::File; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 13 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: file-based logging engine for Dancer | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | $Dancer::Logger::File::VERSION = '1.3522'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use base 'Dancer::Logger::Abstract'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use Dancer::FileUtils qw(open_file); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use Fcntl qw(:flock SEEK_END); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 19 | ▶ | sub logdir { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logdir: called CC=13 SCAR=34.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 19 | ▶ | if ( my $altpath = setting('log_path') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | return $altpath; | |||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 16 | my $logroot = setting('appdir'); | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 16 | ▶ | if ( $logroot and ! -d $logroot and ! mkdir $logroot ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$logroot and not -d $logroot and not mkdir($logroot)
$logroot and not -d $logroot
MC/DC0%0/3
$logroot and not -d $logroot and not mkdir($logroot)
$logroot
not -d $logroot
not mkdir($logroot)
Truth table50%2/4
A and not(B) and not(C)
A= $logrootB= not -d $logrootC= not mkdir($logroot)
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | carp "app directory '$logroot' doesn't exist, am unable to create it"; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 16 | ▶ | my $expected_path = $logroot ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | Dancer::FileUtils::path($logroot, 'logs') : | ||||||||||||||||||||||||||||||||||||||||||
| 29 | Dancer::FileUtils::path('logs'); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 16 | ▶ | return $expected_path if -d $expected_path && -x _ && -w _; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
-d $expected_path and -x _ and -w _
-d $expected_path and -x _
MC/DC33%1/3
-d $expected_path and -x _ and -w _
-d $expected_path
-x _
-w _
Truth table50%2/4
A and B and C
A= -d $expected_pathB= -x _C= -w _
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | ▶ | unless (-w $logroot and -x _) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-w $logroot and -x _
MC/DC0%0/2
-w $logroot and -x _
-w $logroot
-x _
Truth table33%1/3
A and B
A= -w $logrootB= -x _
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $perm = (stat $logroot)[2] & 07777; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | chmod($perm | 0700, $logroot); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | unless (-w $logroot and -x _) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-w $logroot and -x _
MC/DC0%0/2
-w $logroot and -x _
-w $logroot
-x _
Truth table0%0/3
A and B
A= -w $logrootB= -x _
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | carp "app directory '$logroot' isn't writable/executable and can't chmod it"; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 9 | return $expected_path; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 18 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 18 | $self->SUPER::init(@_); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 18 | my $logdir = logdir(); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 18 | ▶ | return unless ($logdir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 18 | ▶ | my $logfile = setting('log_file') || setting('environment').".log"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
setting('log_file') || setting('environment') . '.log'
MC/DC100%1/1
setting('log_file') || setting('environment') . '.log'
setting('log_file')
Truth table100%2/2
A
A= setting('log_file')
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 18 | ▶ | mkdir($logdir) unless(-d $logdir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 18 | $logfile = File::Spec->catfile($logdir, $logfile); | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 18 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 18 | ▶ | unless($fh = open_file('>>', $logfile)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | carp "unable to create or append to $logfile"; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # looks like older perls don't auto-convert to IO::File | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # and can't autoflush | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # see https://github.com/PerlDancer/Dancer/issues/954 | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 18 | eval { $fh->autoflush }; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 18 | $self->{logfile} = $logfile; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 18 | $self->{fh} = $fh; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 20 | ▶ | sub _log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=7 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 20 | my ($self, $level, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 20 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 20 | ▶ | return unless(ref $fh && $fh->opened); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $fh and $fh->opened
MC/DC0%0/2
ref $fh and $fh->opened
ref $fh
$fh->opened
Truth table33%1/3
A and B
A= ref $fhB= $fh->opened
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 20 | ▶ | flock($fh, LOCK_EX) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | or carp "locking logfile $self->{logfile} failed"; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 20 | ▶ | seek($fh, 0, SEEK_END) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | or carp "seeking to logfile $self->{logfile} end failed"; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 20 | ▶ | $fh->print($self->format_message($level => $message)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | or carp "writing to logfile $self->{logfile} failed"; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 20 | ▶ | flock($fh, LOCK_UN) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | or carp "unlocking logfile $self->{logfile} failed"; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||||||||||||||||