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.3514_04'; # TRIAL |
5
|
|
|
|
|
|
|
$Dancer::Logger::File::VERSION = '1.351404'; |
6
|
12
|
|
|
12
|
|
61929
|
use strict; |
|
12
|
|
|
|
|
42
|
|
|
12
|
|
|
|
|
316
|
|
7
|
12
|
|
|
12
|
|
55
|
use warnings; |
|
12
|
|
|
|
|
26
|
|
|
12
|
|
|
|
|
258
|
|
8
|
12
|
|
|
12
|
|
50
|
use Carp; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
638
|
|
9
|
12
|
|
|
12
|
|
71
|
use base 'Dancer::Logger::Abstract'; |
|
12
|
|
|
|
|
19
|
|
|
12
|
|
|
|
|
4095
|
|
10
|
|
|
|
|
|
|
|
11
|
12
|
|
|
12
|
|
71
|
use Dancer::Config 'setting'; |
|
12
|
|
|
|
|
31
|
|
|
12
|
|
|
|
|
465
|
|
12
|
12
|
|
|
12
|
|
73
|
use Dancer::FileUtils qw(open_file); |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
452
|
|
13
|
12
|
|
|
12
|
|
56
|
use IO::File; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
2785
|
|
14
|
12
|
|
|
12
|
|
107
|
use Fcntl qw(:flock SEEK_END); |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
6846
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub logdir { |
17
|
19
|
100
|
|
19
|
1
|
62
|
if ( my $altpath = setting('log_path') ) { |
18
|
3
|
|
|
|
|
9
|
return $altpath; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
16
|
|
|
|
|
39
|
my $logroot = setting('appdir'); |
22
|
|
|
|
|
|
|
|
23
|
16
|
50
|
66
|
|
|
313
|
if ( $logroot and ! -d $logroot and ! mkdir $logroot ) { |
|
|
|
33
|
|
|
|
|
24
|
0
|
|
|
|
|
0
|
carp "app directory '$logroot' doesn't exist, am unable to create it"; |
25
|
0
|
|
|
|
|
0
|
return; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
16
|
100
|
|
|
|
91
|
my $expected_path = $logroot ? |
29
|
|
|
|
|
|
|
Dancer::FileUtils::path($logroot, 'logs') : |
30
|
|
|
|
|
|
|
Dancer::FileUtils::path('logs'); |
31
|
|
|
|
|
|
|
|
32
|
16
|
50
|
66
|
|
|
385
|
return $expected_path if -d $expected_path && -x _ && -w _; |
|
|
|
66
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
9
|
50
|
33
|
|
|
161
|
unless (-w $logroot and -x _) { |
35
|
0
|
|
|
|
|
0
|
my $perm = (stat $logroot)[2] & 07777; |
36
|
0
|
|
|
|
|
0
|
chmod($perm | 0700, $logroot); |
37
|
0
|
0
|
0
|
|
|
0
|
unless (-w $logroot and -x _) { |
38
|
0
|
|
|
|
|
0
|
carp "app directory '$logroot' isn't writable/executable and can't chmod it"; |
39
|
0
|
|
|
|
|
0
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
9
|
|
|
|
|
32
|
return $expected_path; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub init { |
46
|
18
|
|
|
18
|
1
|
35
|
my $self = shift; |
47
|
18
|
|
|
|
|
98
|
$self->SUPER::init(@_); |
48
|
|
|
|
|
|
|
|
49
|
18
|
|
|
|
|
40
|
my $logdir = logdir(); |
50
|
18
|
50
|
|
|
|
49
|
return unless ($logdir); |
51
|
|
|
|
|
|
|
|
52
|
18
|
|
66
|
|
|
74
|
my $logfile = setting('log_file') || setting('environment').".log"; |
53
|
|
|
|
|
|
|
|
54
|
18
|
100
|
|
|
|
612
|
mkdir($logdir) unless(-d $logdir); |
55
|
18
|
|
|
|
|
188
|
$logfile = File::Spec->catfile($logdir, $logfile); |
56
|
|
|
|
|
|
|
|
57
|
18
|
|
|
|
|
46
|
my $fh; |
58
|
18
|
50
|
|
|
|
67
|
unless($fh = open_file('>>', $logfile)) { |
59
|
0
|
|
|
|
|
0
|
carp "unable to create or append to $logfile"; |
60
|
0
|
|
|
|
|
0
|
return; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# looks like older perls don't auto-convert to IO::File |
64
|
|
|
|
|
|
|
# and can't autoflush |
65
|
|
|
|
|
|
|
# see https://github.com/PerlDancer/Dancer/issues/954 |
66
|
18
|
|
|
|
|
33
|
eval { $fh->autoflush }; |
|
18
|
|
|
|
|
108
|
|
67
|
|
|
|
|
|
|
|
68
|
18
|
|
|
|
|
837
|
$self->{logfile} = $logfile; |
69
|
18
|
|
|
|
|
54
|
$self->{fh} = $fh; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _log { |
73
|
20
|
|
|
20
|
|
632
|
my ($self, $level, $message) = @_; |
74
|
20
|
|
|
|
|
42
|
my $fh = $self->{fh}; |
75
|
|
|
|
|
|
|
|
76
|
20
|
50
|
33
|
|
|
105
|
return unless(ref $fh && $fh->opened); |
77
|
|
|
|
|
|
|
|
78
|
20
|
50
|
|
|
|
305
|
flock($fh, LOCK_EX) |
79
|
|
|
|
|
|
|
or carp "locking logfile $self->{logfile} failed"; |
80
|
20
|
50
|
|
|
|
174
|
seek($fh, 0, SEEK_END) |
81
|
|
|
|
|
|
|
or carp "seeking to logfile $self->{logfile} end failed"; |
82
|
20
|
50
|
|
|
|
82
|
$fh->print($self->format_message($level => $message)) |
83
|
|
|
|
|
|
|
or carp "writing to logfile $self->{logfile} failed"; |
84
|
20
|
50
|
|
|
|
1101
|
flock($fh, LOCK_UN) |
85
|
|
|
|
|
|
|
or carp "unlocking logfile $self->{logfile} failed"; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
__END__ |