| 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
Condition66%2/3
-d $dir or mkdir $dir
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
| |||||||||||||||||||||||||||||
| 30 | 0 | die "log directory \"$dir\" does not exist and unable to create it."; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 4 | ▶ | if ( !-w $dir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | flock( $fh, LOCK_EX ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 121 | or carp "writing to logfile $self->{logfile} failed"; | ||||||||||||||||||||||||||||
| 122 | 1 | ▶ | flock( $fh, LOCK_UN ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | or carp "unlocking logfile $self->{logfile} failed: $!"; | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||