line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: file-based logging engine for Dancer2 |
2
|
|
|
|
|
|
|
$Dancer2::Logger::File::VERSION = '0.400000'; |
3
|
|
|
|
|
|
|
use Carp 'carp'; |
4
|
2
|
|
|
2
|
|
1120
|
use Moo; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
160
|
|
5
|
2
|
|
|
2
|
|
10
|
use Dancer2::Core::Types; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
28
|
|
6
|
2
|
|
|
2
|
|
808
|
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
38
|
|
7
|
|
|
|
|
|
|
with 'Dancer2::Core::Role::Logger'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use File::Spec; |
10
|
2
|
|
|
2
|
|
17383
|
use Fcntl qw(:flock SEEK_END); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
71
|
|
11
|
2
|
|
|
2
|
|
10
|
use Dancer2::FileUtils qw(open_file); |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
387
|
|
12
|
2
|
|
|
2
|
|
14
|
use IO::File; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
120
|
|
13
|
2
|
|
|
2
|
|
19
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1494
|
|
14
|
|
|
|
|
|
|
has environment => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
required => 1, |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has location => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
required => 1, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has log_dir => ( |
25
|
|
|
|
|
|
|
is => 'rw', |
26
|
|
|
|
|
|
|
isa => sub { |
27
|
|
|
|
|
|
|
my $dir = shift; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
if ( !-d $dir && !mkdir $dir ) { |
30
|
|
|
|
|
|
|
die "log directory \"$dir\" does not exist and unable to create it."; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
if ( !-w $dir ) { |
33
|
|
|
|
|
|
|
die "log directory \"$dir\" is not writable." |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
}, |
36
|
|
|
|
|
|
|
lazy => 1, |
37
|
|
|
|
|
|
|
builder => '_build_log_dir', |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has file_name => ( |
41
|
|
|
|
|
|
|
is => 'ro', |
42
|
|
|
|
|
|
|
isa => Str, |
43
|
|
|
|
|
|
|
builder => '_build_file_name', |
44
|
|
|
|
|
|
|
lazy => 1 |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has log_file => ( |
48
|
|
|
|
|
|
|
is => 'ro', |
49
|
|
|
|
|
|
|
isa => Str, |
50
|
|
|
|
|
|
|
lazy => 1, |
51
|
|
|
|
|
|
|
builder => '_build_log_file', |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has fh => ( |
55
|
|
|
|
|
|
|
is => 'ro', |
56
|
|
|
|
|
|
|
lazy => 1, |
57
|
|
|
|
|
|
|
builder => '_build_fh', |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
1
|
|
1521
|
|
62
|
|
|
|
|
|
|
my $self = shift; |
63
|
1
|
|
|
1
|
|
562
|
return File::Spec->catfile( $self->log_dir, $self->file_name ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
4
|
|
|
4
|
|
5863
|
my $self = shift; |
67
|
4
|
|
|
|
|
59
|
my $logfile = $self->log_file; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $fh; |
70
|
|
|
|
|
|
|
unless ( $fh = open_file( '>>', $logfile ) ) { |
71
|
1
|
|
|
1
|
|
9
|
carp "unable to create or append to $logfile"; |
72
|
1
|
|
|
|
|
14
|
return; |
73
|
|
|
|
|
|
|
} |
74
|
1
|
|
|
|
|
66
|
|
75
|
1
|
50
|
|
|
|
7
|
$fh->autoflush; |
76
|
0
|
|
|
|
|
0
|
|
77
|
0
|
|
|
|
|
0
|
return $fh; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
11
|
my ( $self, $level, $message ) = @_; |
81
|
|
|
|
|
|
|
my $fh = $self->fh; |
82
|
1
|
|
|
|
|
75
|
|
83
|
|
|
|
|
|
|
return unless ( ref $fh && $fh->opened ); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
flock( $fh, LOCK_EX ) |
86
|
|
|
|
|
|
|
or carp "locking logfile $self->{logfile} failed: $!"; |
87
|
|
|
|
|
|
|
seek( $fh, 0, SEEK_END ); |
88
|
|
|
|
|
|
|
$fh->print( $self->format_message( $level => $message ) ) |
89
|
|
|
|
|
|
|
or carp "writing to logfile $self->{logfile} failed"; |
90
|
|
|
|
|
|
|
flock( $fh, LOCK_UN ) |
91
|
|
|
|
|
|
|
or carp "unlocking logfile $self->{logfile} failed: $!"; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=pod |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=encoding UTF-8 |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 NAME |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Dancer2::Logger::File - file-based logging engine for Dancer2 |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 VERSION |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
version 0.400000 |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 DESCRIPTION |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This is a logging engine that allows you to save your logs to files on disk. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Logs are not automatically rotated. Use a log rotation tool like |
114
|
|
|
|
|
|
|
C<logrotate> in C<copytruncate> mode. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 METHODS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 log($level, $message) |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Writes the log message to the file. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 CONFIGURATION |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
The setting C<logger> should be set to C<File> in order to use this logging |
125
|
|
|
|
|
|
|
engine in a Dancer2 application. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
The follow attributes are supported: |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=over 4 |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item * C<log_dir> |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Directory path to hold log files. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Defaults to F<logs> in the application directory |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item * C<file_name> |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
The name of the log file. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Defaults to the environment name with a F<.log> suffix |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Here is an example configuration that use this logger and stores logs in F</var/log/myapp>: |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
logger: "File" |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
engines: |
150
|
|
|
|
|
|
|
logger: |
151
|
|
|
|
|
|
|
File: |
152
|
|
|
|
|
|
|
log_dir: "/var/log/myapp" |
153
|
|
|
|
|
|
|
file_name: "myapp.log" |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 AUTHOR |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Dancer Core Developers |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This software is copyright (c) 2022 by Alexis Sukrieh. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
164
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |