Debian Codegraph @

lib/Dancer2/Logger/File.pm

raw
1 package Dancer2::Logger::File;
2 # ABSTRACT: file-based logging engine for Dancer2
3 $Dancer2::Logger::File::VERSION = '2.2.2';
4 use Carp 'carp';
5 use Moo;
6 use Dancer2::Core::Types;
7
8 with 'Dancer2::Core::Role::Logger';
9
10 use Fcntl qw(:flock SEEK_END);
11 use Path::Tiny ();
12 use IO::File;
13
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 _log_dir_path => (
41 is => 'ro',
42 lazy => 1,
43 builder => '_build_log_dir_path',
44 init_arg => undef,
45 );
46
47 has file_name => (
48 is => 'ro',
49 isa => Str,
50 builder => '_build_file_name',
51 lazy => 1
52 );
53
54 has log_file => (
55 is => 'ro',
56 isa => Str,
57 lazy => 1,
58 builder => '_build_log_file',
59 );
60
61 has _log_file_path => (
62 is => 'ro',
63 lazy => 1,
64 builder => '_build_log_file_path',
65 init_arg => undef,
66 );
67
68 has fh => (
69 is => 'ro',
70 lazy => 1,
71 builder => '_build_fh',
72 );
73
74 sub _build_log_dir { Path::Tiny::path( $_[0]->location, 'logs' )->stringify }
75
76 sub _build_log_dir_path {
77 my $self = shift;
78 return Path::Tiny::path( $self->log_dir );
79 }
80
81 sub _build_file_name {$_[0]->environment . ".log"}
82
83 sub _build_log_file {
84 my $self = shift;
85 return $self->_log_dir_path->child( $self->file_name )->stringify;
86 }
87
88 sub _build_log_file_path {
89 my $self = shift;
90 return Path::Tiny::path( $self->log_file );
91 }
92
93 sub _build_fh {
94 my $self = shift;
95 my $logfile = $self->_log_file_path->stringify;
96
97 my $fh = eval {
98 $self->_log_file_path->filehandle(
99 '>>', ':encoding(UTF-8)',
100 );
101 } or do {
102 Carp::carp("unable to create or append to $logfile");
103 return;
104 };
105
106 $fh->autoflush;
107
108 return $fh;
109 }
110
111 sub log {
112 my ( $self, $level, $message ) = @_;
113 my $fh = $self->fh;
114
115 return unless ( ref $fh && $fh->opened );
116
117 flock( $fh, LOCK_EX )
118 or carp "locking logfile $self->{logfile} failed: $!";
119 seek( $fh, 0, SEEK_END );
120 $fh->print( $self->format_message( $level => $message ) )
121 or carp "writing to logfile $self->{logfile} failed";
122 flock( $fh, LOCK_UN )
123 or carp "unlocking logfile $self->{logfile} failed: $!";
124 }
125
126 1;
127
128 __END__
129
130 =pod
131
132 =encoding UTF-8
133
134 =head1 NAME
135
136 Dancer2::Logger::File - file-based logging engine for Dancer2
137
138 =head1 VERSION
139
140 version 2.2.2
141
142 =head1 DESCRIPTION
143
144 This is a logging engine that allows you to save your logs to files on disk.
145
146 Logs are not automatically rotated. Use a log rotation tool like
147 C<logrotate> in C<copytruncate> mode.
148
149 =head1 METHODS
150
151 =head2 log($level, $message)
152
153 Writes the log message to the file.
154
155 =head1 CONFIGURATION
156
157 The setting C<logger> should be set to C<File> in order to use this logging
158 engine in a Dancer2 application.
159
160 The follow attributes are supported:
161
162 =over 4
163
164 =item * C<log_dir>
165
166 Directory path to hold log files.
167
168 Defaults to F<logs> in the application directory
169
170 =item * C<file_name>
171
172 The name of the log file.
173
174 Defaults to the environment name with a F<.log> suffix
175
176 =back
177
178 Here is an example configuration that use this logger and stores logs in F</var/log/myapp>:
179
180 logger: "File"
181
182 engines:
183 logger:
184 File:
185 log_dir: "/var/log/myapp"
186 file_name: "myapp.log"
187
188 =head1 AUTHOR
189
190 Dancer Core Developers
191
192 =head1 COPYRIGHT AND LICENSE
193
194 This software is copyright (c) 2026 by Alexis Sukrieh.
195
196 This is free software; you can redistribute it and/or modify it under
197 the same terms as the Perl 5 programming language system itself.
198
199 =cut