File Coverage

blib/lib/Dancer/Logger/File.pm
Criterion Covered Total %
statement 52 61 85.2
branch 16 28 57.1
condition 11 24 45.8
subroutine 11 11 100.0
pod 2 2 100.0
total 92 126 73.0


line stmt bran cond sub pod time code
1             package Dancer::Logger::File;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: file-based logging engine for Dancer
4             $Dancer::Logger::File::VERSION = '1.3520';
5 12     12   74610 use strict;
  12         48  
  12         396  
6 12     12   90 use warnings;
  12         35  
  12         339  
7 12     12   76 use Carp;
  12         40  
  12         824  
8 12     12   77 use base 'Dancer::Logger::Abstract';
  12         37  
  12         5014  
9              
10 12     12   105 use Dancer::Config 'setting';
  12         30  
  12         686  
11 12     12   102 use Dancer::FileUtils qw(open_file);
  12         28  
  12         590  
12 12     12   84 use IO::File;
  12         40  
  12         2016  
13 12     12   108 use Fcntl qw(:flock SEEK_END);
  12         41  
  12         9089  
14              
15             sub logdir {
16 19 100   19 1 86 if ( my $altpath = setting('log_path') ) {
17 3         13 return $altpath;
18             }
19              
20 16         49 my $logroot = setting('appdir');
21              
22 16 50 66     366 if ( $logroot and ! -d $logroot and ! mkdir $logroot ) {
      33        
23 0         0 carp "app directory '$logroot' doesn't exist, am unable to create it";
24 0         0 return;
25             }
26              
27 16 100       131 my $expected_path = $logroot ?
28             Dancer::FileUtils::path($logroot, 'logs') :
29             Dancer::FileUtils::path('logs');
30              
31 16 50 66     514 return $expected_path if -d $expected_path && -x _ && -w _;
      66        
32              
33 9 50 33     237 unless (-w $logroot and -x _) {
34 0         0 my $perm = (stat $logroot)[2] & 07777;
35 0         0 chmod($perm | 0700, $logroot);
36 0 0 0     0 unless (-w $logroot and -x _) {
37 0         0 carp "app directory '$logroot' isn't writable/executable and can't chmod it";
38 0         0 return;
39             }
40             }
41 9         58 return $expected_path;
42             }
43              
44             sub init {
45 18     18 1 40 my $self = shift;
46 18         138 $self->SUPER::init(@_);
47              
48 18         49 my $logdir = logdir();
49 18 50       78 return unless ($logdir);
50              
51 18   66     73 my $logfile = setting('log_file') || setting('environment').".log";
52              
53 18 100       783 mkdir($logdir) unless(-d $logdir);
54 18         230 $logfile = File::Spec->catfile($logdir, $logfile);
55              
56 18         96 my $fh;
57 18 50       119 unless($fh = open_file('>>', $logfile)) {
58 0         0 carp "unable to create or append to $logfile";
59 0         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         38 eval { $fh->autoflush };
  18         139  
66              
67 18         1060 $self->{logfile} = $logfile;
68 18         63 $self->{fh} = $fh;
69             }
70              
71             sub _log {
72 20     20   944 my ($self, $level, $message) = @_;
73 20         39 my $fh = $self->{fh};
74              
75 20 50 33     120 return unless(ref $fh && $fh->opened);
76              
77 20 50       377 flock($fh, LOCK_EX)
78             or carp "locking logfile $self->{logfile} failed";
79 20 50       209 seek($fh, 0, SEEK_END)
80             or carp "seeking to logfile $self->{logfile} end failed";
81 20 50       100 $fh->print($self->format_message($level => $message))
82             or carp "writing to logfile $self->{logfile} failed";
83 20 50       1323 flock($fh, LOCK_UN)
84             or carp "unlocking logfile $self->{logfile} failed";
85              
86             }
87              
88             1;
89              
90             __END__