File Coverage

blib/lib/Log/Dispatch/File/Stamped.pm
Criterion Covered Total %
statement 60 60 100.0
branch 9 12 75.0
condition 3 3 100.0
subroutine 15 15 100.0
pod 1 1 100.0
total 88 91 96.7


line stmt bran cond sub pod time code
1 7     7   1621390 use strict;
  7         47  
  7         210  
2 7     7   36 use warnings;
  7         13  
  7         391  
3             # vim: set ts=8 sts=4 sw=4 tw=115 et :
4             # ABSTRACT: Logging to date/time stamped files
5             # KEYWORDS: log logging output file timestamp date rolling rotate
6              
7             our $VERSION = '0.20';
8              
9             use File::Basename qw(fileparse);
10 7     7   81 use File::Spec::Functions qw(catfile);
  7         25  
  7         577  
11 7     7   2849 use POSIX qw(strftime);
  7         5148  
  7         430  
12 7     7   52  
  7         24  
  7         67  
13             use Log::Dispatch::File 2.38;
14 7     7   13482 use parent 'Log::Dispatch::File';
  7         763024  
  7         294  
15 7     7   69  
  7         17  
  7         67  
16             use Params::ValidationCompiler qw(validation_for);
17 7     7   569  
  7         19  
  7         326  
18             use Specio::Library::Builtins;
19 7     7   62 use Specio::Library::String;
  7         15  
  7         75  
20 7     7   65514 use Specio::Declare;
  7         18  
  7         59  
21 7     7   15097  
  7         25  
  7         67  
22             use namespace::clean 0.19;
23 7     7   1530  
  7         183  
  7         128  
24             enum(
25             'TimeFunctions',
26             values => [qw(localtime gmtime)],
27             );
28              
29             {
30             my $self = shift;
31              
32 13     13   59413 $self->SUPER::_basic_init(@_);
33              
34 13         89 my $validator = validation_for(
35             params => {
36 13         1338 stamp_fmt => {
37             type => t('SimpleStr'),
38             default => '%Y%m%d',
39             },
40             stamp_sep => {
41             type => t('SimpleStr'),
42             default => '-',
43             },
44             time_function => {
45             type => t('TimeFunctions'),
46             default => 'localtime',
47             },
48             },
49             slurpy => 1,
50             );
51             my %p = $validator->(@_);
52              
53 13         27700 $self->{stamp_fmt} = $p{stamp_fmt};
54             $self->{stamp_sep} = $p{stamp_sep};
55 12         418 $self->{time_function} = $p{time_function};
56 12         38  
57 12         31 # cache of last timestamp used
58             $self->{_stamp} = '';
59             $self->{_time} = 0;
60 12         46  
61 12         29 # split pathname into basename, path, extension
62             @$self{qw(_name _path _ext)} = fileparse($self->{filename}, '\.[^.]+');
63              
64 12         722 # stored in $self->{filename} (overwrites original); used by _open_file()
65             $self->_make_filename;
66             }
67 12         64  
68             {
69             my $self = shift;
70              
71             # re-use last filename if the time has not changed
72 24     24   49 my $time = time();
73             return $self->{filename} if $time eq $self->{_time};
74              
75 24         71 $self->{_time} = $time;
76 24 100       115 my $stamp = strftime($self->{stamp_fmt}, $self->{time_function} eq 'localtime' ? localtime : gmtime);
77              
78 18         41 # re-use last filename if the stamp has not changed
79 18 100       646 return $self->{filename} if $stamp eq $self->{_stamp};
80              
81             $self->{filename} = catfile(
82 18 50       428 $self->{_path},
83             join($self->{stamp_sep}, $self->{_name}, $stamp) . ($self->{_ext} ? $self->{_ext} : '')
84             );
85             }
86 18 50       876  
87             {
88             my $self = shift;
89              
90             # check if the filename is the same as last time...
91             my $old_filename = $self->{filename};
92 12     12 1 25959 $self->_make_filename;
93              
94             # don't re-open if we use close-after-write - the superclass will do it
95 12         29 if (not $self->{ Log::Dispatch->VERSION >= '2.59' ? 'close_after_write' : 'close' }
96 12         40 and $old_filename ne $self->{filename})
97             {
98             $self->_open_file;
99 12 50 100     281 }
    100          
100              
101             $self->SUPER::log_message(@_);
102 1         5 }
103              
104             1;
105 12         204  
106              
107             =pod
108              
109             =encoding UTF-8
110              
111             =head1 NAME
112              
113             Log::Dispatch::File::Stamped - Logging to date/time stamped files
114              
115             =head1 VERSION
116              
117             version 0.20
118              
119             =head1 SYNOPSIS
120              
121             use Log::Dispatch::File::Stamped;
122              
123             my $file = Log::Dispatch::File::Stamped->new(
124             name => 'file1',
125             min_level => 'info',
126             filename => 'Somefile.log',
127             stamp_fmt => '%Y%m%d',
128             stamp_sep => ':',
129             time_function => 'localtime',
130             mode => 'append',
131             );
132              
133             $file->log( level => 'emerg', message => "I've fallen and I can't get up\n" );
134              
135             =head1 DESCRIPTION
136              
137             This module subclasses L<Log::Dispatch::File> for logging to date/time-stamped
138             files, respecting all its configuration options. As with other L<Log::Dispatch>
139             handlers, the destination file is kept open for as long as the filename remains
140             constant (unless C<close_after_write> is set).
141              
142             =head1 METHODS
143              
144             =head2 new(%p)
145              
146             This method takes the same set of parameters as L<Log::Dispatch::File::new()|Log::Dispatch::File/new>,
147             with the following differences:
148              
149             =over 4
150              
151             =item * filename ($)
152              
153             The filename template. The actual timestamp will be appended to this filename
154             when creating the actual logfile. If the filename has an extension, the
155             timestamp is inserted before the extension. See examples below.
156              
157             =item * stamp_fmt ($)
158              
159             The format of the timestamp string. This module uses L<POSIX::strftime|POSIX/strftime> to
160             create the timestamp string from the current local date and time.
161             Refer to your platform's C<strftime> documentation for the list of allowed
162             tokens.
163              
164             Defaults to C<%Y%m%d>.
165              
166             =item * stamp_sep ($)
167              
168             The separator character(s) used between components in the log filename.
169              
170             Defaults to C<->.
171              
172             =item * time_function ($)
173              
174             The function used to determine the current time; present choices are L<perlfunc/localtime> or
175             L<perlfunc/gmtime>. Defaults to C<localtime>.
176              
177             =back
178              
179             =head2 log_message( message => $ )
180              
181             Sends a message to the appropriate output. Generally this
182             shouldn't be called directly but should be called through the
183             C<log()> method (in L<Log::Dispatch::Output>).
184              
185             =head1 EXAMPLES
186              
187             =for stopwords txt
188              
189             Assuming the current date and time is:
190              
191             % perl -e 'print scalar localtime'
192             Sat Feb 8 13:56:13 2003
193              
194             Log::Dispatch::File::Stamped->new(
195             name => 'file',
196             min_level => 'debug',
197             filename => 'logfile.txt',
198             );
199              
200             This will log to file F<logfile-20030208.txt>.
201              
202             Log::Dispatch::File::Stamped->new(
203             name => 'file',
204             min_level => 'debug',
205             filename => 'logfile.txt',
206             stamp_fmt => '%d%H',
207             );
208              
209             This will log to file F<logfile-0813.txt>.
210              
211             =head1 SEE ALSO
212              
213             =over 4
214              
215             =item *
216              
217             L<Log::Dispatch::File>
218              
219             =item *
220              
221             L<POSIX>
222              
223             =item *
224              
225             L<Log::Dispatch::File::Rolling>
226              
227             =item *
228              
229             L<Log::Dispatch::FileRotate>
230              
231             =item *
232              
233             L<Log::Dispatch::FileWriteRotate>
234              
235             =back
236              
237             =head1 ACKNOWLEDGEMENTS
238              
239             =for stopwords Rolsky
240              
241             Dave Rolsky, author of the L<Log::Dispatch> suite and many other
242             fine modules on CPAN.
243              
244             This module was rewritten to respect all present (and future) options to
245             L<Log::Dispatch::File> by Karen Etheridge, <ether@cpan.org>.
246              
247             =head1 SUPPORT
248              
249             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Log-Dispatch-File-Stamped>
250             (or L<bug-Log-Dispatch-File-Stamped@rt.cpan.org|mailto:bug-Log-Dispatch-File-Stamped@rt.cpan.org>).
251              
252             =head1 AUTHORS
253              
254             =over 4
255              
256             =item *
257              
258             Eric Cholet <cholet@logilune.com>
259              
260             =item *
261              
262             Karen Etheridge <ether@cpan.org>
263              
264             =back
265              
266             =head1 CONTRIBUTOR
267              
268             =for stopwords Sven Willenbuecher
269              
270             Sven Willenbuecher <sven.willenbuecher@kuehne-nagel.com>
271              
272             =head1 COPYRIGHT AND LICENCE
273              
274             This software is copyright (c) 2003 by Eric Cholet and Karen Etheridge.
275              
276             This is free software; you can redistribute it and/or modify it under
277             the same terms as the Perl 5 programming language system itself.
278              
279             =cut