File Coverage

blib/lib/Log/Dispatch/File/Stamped.pm
Criterion Covered Total %
statement 49 49 100.0
branch 9 12 75.0
condition 3 3 100.0
subroutine 12 12 100.0
pod 1 1 100.0
total 74 77 96.1


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