blib/lib/Dancer2/Logger/File.pm

filter: Statementstmt 92.9% 53 / 57 Branchbran 50.0% 7 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 81.2% 78 / 96 CC 10 SCAR 12.4
CC
10
Cov
82%
log
7.3
__ANON__
5.2
_build_fh
2.1
CRAP 3.5
« lib/Dancer2/Template/Tiny.pm ↑ Summary lib/Dancer2/Core/Time.pm »
1 package Dancer2::Logger::File;
2 # ABSTRACT: file-based logging engine for Dancer2
3 2 $Dancer2::Logger::File::VERSION = '2.3.0';
4 3 ▶use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 with 'Dancer2::Core::Role::Logger';
9  
10 3 ▶use Fcntl qw(:flock SEEK_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 has environment => (
15 is => 'ro',
16 required => 1,
17 );
18  
19 2 has location => (
20 is => 'ro',
21 required => 1,
22 );
23  
24 has log_dir => (
25 is => 'rw',
26 4 ▶ isa => sub {
Subroutine __ANON__: called CC=4 SCAR=16.4
27 4 my $dir = shift;
28  
29 4 ▶ if ( !-d $dir && !mkdir $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir or mkdir $dir) 0 4 4
Condition66%2/3
-d $dir or mkdir $dir
l!l&&r!l&&!r
220
MC/DC0%0/2
-d $dir or mkdir $dir
-d $dir mkdir $dir
Truth table66%2/3
A or B
A= -d $dirB= mkdir $dir
ABresult
1X1
011
000
30 0 die "log directory \"$dir\" does not exist and unable to create it.";
31 }
32 4 ▶ if ( !-w $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -w $dir) 0 4 4
33 0 die "log directory \"$dir\" is not writable."
34 }
35 },
36 2 lazy => 1,
37 builder => '_build_log_dir',
38 );
39  
40 2 has _log_dir_path => (
41 is => 'ro',
42 lazy => 1,
43 builder => '_build_log_dir_path',
44 init_arg => undef,
45 );
46  
47 2 has file_name => (
48 is => 'ro',
49 isa => Str,
50 builder => '_build_file_name',
51 lazy => 1
52 );
53  
54 2 has log_file => (
55 is => 'ro',
56 isa => Str,
57 lazy => 1,
58 builder => '_build_log_file',
59 );
60  
61 2 has _log_file_path => (
62 is => 'ro',
63 lazy => 1,
64 builder => '_build_log_file_path',
65 init_arg => undef,
66 );
67  
68 2 has fh => (
69 is => 'ro',
70 lazy => 1,
71 builder => '_build_fh',
72 );
73  
74 1 ▶sub _build_log_dir { Path::Tiny::path( $_[0]->location, 'logs' )->stringify }
Subroutine _build_log_dir: called CC=1 SCAR=0.0
75  
76 4 ▶sub _build_log_dir_path {
Subroutine _build_log_dir_path: called CC=1 SCAR=0.0
77 4 my $self = shift;
78 4 return Path::Tiny::path( $self->log_dir );
79 }
80  
81 1 ▶sub _build_file_name {$_[0]->environment . ".log"}
Subroutine _build_file_name: called CC=1 SCAR=0.0
82  
83 4 ▶sub _build_log_file {
Subroutine _build_log_file: called CC=1 SCAR=0.0
84 4 my $self = shift;
85 4 return $self->_log_dir_path->child( $self->file_name )->stringify;
86 }
87  
88 1 ▶sub _build_log_file_path {
Subroutine _build_log_file_path: called CC=1 SCAR=0.0
89 1 my $self = shift;
90 1 return Path::Tiny::path( $self->log_file );
91 }
92  
93 1 ▶sub _build_fh {
Subroutine _build_fh: called CC=2 SCAR=7.5
94 1 my $self = shift;
95 1 my $logfile = $self->_log_file_path->stringify;
96  
97 my $fh = eval {
98 1 $self->_log_file_path->filehandle(
99 '>>', ':encoding(UTF-8)',
100 );
101 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $fh = eval { do { $self->_log_file_path->filehandle(">>", ":encoding(UTF-8)") } } or do { Carp::carp("unable to create or append to $logfile"); return } 1 0 1
102 0 Carp::carp("unable to create or append to $logfile");
103 0 return;
104 };
105  
106 1 $fh->autoflush;
107  
108 1 return $fh;
109 }
110  
111 1 ▶sub log {
Subroutine log: called CC=6 SCAR=19.9
Pod documented
112 1 my ( $self, $level, $message ) = @_;
113 1 my $fh = $self->fh;
114  
115 1 ▶ return unless ( ref $fh && $fh->opened );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fh and $fh->opened 0 1 1
Condition33%1/3
ref $fh and $fh->opened
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
116  
117 1 ▶ flock( $fh, LOCK_EX )
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or carp(qq[locking logfile $self->{"logfile"} failed: $!]) 1 0 1
118 or carp "locking logfile $self->{logfile} failed: $!";
119 1 seek( $fh, 0, SEEK_END );
120 1 ▶ $fh->print( $self->format_message( $level => $message ) )
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($self->format_message($level, $message)) or carp(qq[writing to logfile $self->{"logfile"} failed]) 1 0 1
121 or carp "writing to logfile $self->{logfile} failed";
122 1 ▶ flock( $fh, LOCK_UN )
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 8 or carp(qq[unlocking logfile $self->{"logfile"} failed: $!]) 1 0 1
123 or carp "unlocking logfile $self->{logfile} failed: $!";
124 }
125  
126 2 1;
127  
128 __END__
« lib/Dancer2/Template/Tiny.pm ↑ Summary lib/Dancer2/Core/Time.pm »