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.3521'; |
5
|
12
|
|
|
12
|
|
70620
|
use strict; |
|
12
|
|
|
|
|
42
|
|
|
12
|
|
|
|
|
365
|
|
6
|
12
|
|
|
12
|
|
64
|
use warnings; |
|
12
|
|
|
|
|
25
|
|
|
12
|
|
|
|
|
306
|
|
7
|
12
|
|
|
12
|
|
60
|
use Carp; |
|
12
|
|
|
|
|
25
|
|
|
12
|
|
|
|
|
747
|
|
8
|
12
|
|
|
12
|
|
87
|
use base 'Dancer::Logger::Abstract'; |
|
12
|
|
|
|
|
36
|
|
|
12
|
|
|
|
|
5010
|
|
9
|
|
|
|
|
|
|
|
10
|
12
|
|
|
12
|
|
88
|
use Dancer::Config 'setting'; |
|
12
|
|
|
|
|
38
|
|
|
12
|
|
|
|
|
592
|
|
11
|
12
|
|
|
12
|
|
85
|
use Dancer::FileUtils qw(open_file); |
|
12
|
|
|
|
|
27
|
|
|
12
|
|
|
|
|
525
|
|
12
|
12
|
|
|
12
|
|
69
|
use IO::File; |
|
12
|
|
|
|
|
35
|
|
|
12
|
|
|
|
|
1936
|
|
13
|
12
|
|
|
12
|
|
88
|
use Fcntl qw(:flock SEEK_END); |
|
12
|
|
|
|
|
52
|
|
|
12
|
|
|
|
|
8438
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub logdir { |
16
|
19
|
100
|
|
19
|
1
|
80
|
if ( my $altpath = setting('log_path') ) { |
17
|
3
|
|
|
|
|
20
|
return $altpath; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
16
|
|
|
|
|
46
|
my $logroot = setting('appdir'); |
21
|
|
|
|
|
|
|
|
22
|
16
|
50
|
66
|
|
|
339
|
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
|
|
|
|
121
|
my $expected_path = $logroot ? |
28
|
|
|
|
|
|
|
Dancer::FileUtils::path($logroot, 'logs') : |
29
|
|
|
|
|
|
|
Dancer::FileUtils::path('logs'); |
30
|
|
|
|
|
|
|
|
31
|
16
|
50
|
66
|
|
|
472
|
return $expected_path if -d $expected_path && -x _ && -w _; |
|
|
|
66
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
9
|
50
|
33
|
|
|
203
|
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
|
|
|
|
|
41
|
return $expected_path; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub init { |
45
|
18
|
|
|
18
|
1
|
51
|
my $self = shift; |
46
|
18
|
|
|
|
|
110
|
$self->SUPER::init(@_); |
47
|
|
|
|
|
|
|
|
48
|
18
|
|
|
|
|
41
|
my $logdir = logdir(); |
49
|
18
|
50
|
|
|
|
63
|
return unless ($logdir); |
50
|
|
|
|
|
|
|
|
51
|
18
|
|
66
|
|
|
68
|
my $logfile = setting('log_file') || setting('environment').".log"; |
52
|
|
|
|
|
|
|
|
53
|
18
|
100
|
|
|
|
722
|
mkdir($logdir) unless(-d $logdir); |
54
|
18
|
|
|
|
|
253
|
$logfile = File::Spec->catfile($logdir, $logfile); |
55
|
|
|
|
|
|
|
|
56
|
18
|
|
|
|
|
56
|
my $fh; |
57
|
18
|
50
|
|
|
|
95
|
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
|
|
|
|
|
154
|
|
66
|
|
|
|
|
|
|
|
67
|
18
|
|
|
|
|
1065
|
$self->{logfile} = $logfile; |
68
|
18
|
|
|
|
|
66
|
$self->{fh} = $fh; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _log { |
72
|
20
|
|
|
20
|
|
767
|
my ($self, $level, $message) = @_; |
73
|
20
|
|
|
|
|
43
|
my $fh = $self->{fh}; |
74
|
|
|
|
|
|
|
|
75
|
20
|
50
|
33
|
|
|
109
|
return unless(ref $fh && $fh->opened); |
76
|
|
|
|
|
|
|
|
77
|
20
|
50
|
|
|
|
380
|
flock($fh, LOCK_EX) |
78
|
|
|
|
|
|
|
or carp "locking logfile $self->{logfile} failed"; |
79
|
20
|
50
|
|
|
|
245
|
seek($fh, 0, SEEK_END) |
80
|
|
|
|
|
|
|
or carp "seeking to logfile $self->{logfile} end failed"; |
81
|
20
|
50
|
|
|
|
116
|
$fh->print($self->format_message($level => $message)) |
82
|
|
|
|
|
|
|
or carp "writing to logfile $self->{logfile} failed"; |
83
|
20
|
50
|
|
|
|
1425
|
flock($fh, LOCK_UN) |
84
|
|
|
|
|
|
|
or carp "unlocking logfile $self->{logfile} failed"; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |