File Coverage

blib/lib/Log/Fine/Handle/File/Timestamp.pm
Criterion Covered Total %
statement 34 34 100.0
branch 7 10 70.0
condition 10 20 50.0
subroutine 8 8 100.0
pod 1 1 100.0
total 60 73 82.1


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Log::Fine::Handle::File::Timestamp - Output log messages to time-stamped files
5              
6             =head1 SYNOPSIS
7              
8             Provides logging to a time-stamped file
9              
10             use Log::Fine;
11             use Log::Fine::Handle::File::Timestamp;
12              
13             # Get a new logger
14             my $log = Log::Fine->getLogger("foo");
15              
16             # register a file handle (default values shown)
17             my $handle = Log::Fine::Handle::File::Timestamp
18             ->new( name => 'file1',
19             mask => LOGMASK_EMERG | LOGMASK_ALERT | LOGMASK_CRIT | LOGMASK_ERR | LOGMASK_WARNING | LOGMASK_NOTICE | LOGMASK_INFO,
20             dir => "/var/log",
21             file => "myapp.%y%m%d.log" );
22              
23             # Register the handle
24             $log->registerHandle($handle);
25              
26             # Log something
27             $log->(INFO, "Opened new log handle");
28              
29              
30             =head1 DESCRIPTION
31              
32             Log::Fine::Handle::File::Timestamp, aside from having a ridiculously
33             long name, provides logging to a time-stamped file. Usage is similar
34             to L with the exception that the file name
35             can take an L string.
36              
37             =cut
38              
39 2     2   3657 use strict;
  2         6  
  2         88  
40 2     2   12 use warnings;
  2         5  
  2         102  
41              
42             package Log::Fine::Handle::File::Timestamp;
43              
44 2     2   12 use base qw( Log::Fine::Handle::File );
  2         4  
  2         2090  
45              
46             #use Data::Dumper;
47 2     2   15 use File::Spec::Functions;
  2         4  
  2         233  
48 2     2   12 use FileHandle;
  2         5  
  2         15  
49 2     2   2405 use POSIX qw( strftime );
  2         5  
  2         58  
50              
51             our $VERSION = $Log::Fine::Handle::File::VERSION;
52              
53             =head1 OVERRIDDEN METHODS
54              
55             =head2 fileHandle
56              
57             See L
58              
59             =cut
60              
61             sub fileHandle
62             {
63              
64 3     3 1 6 my $self = shift;
65 3         745 my $filename = strftime($self->{file}, localtime(time));
66              
67 3 100 66     68 if ( defined $self->{_filehandle}
      66        
      33        
      33        
68             and ref $self->{_filehandle}
69             and UNIVERSAL::can($self->{_filehandle}, 'isa')
70             and $self->{_filehandle}->isa("IO::File")
71             and defined fileno($self->{_filehandle})) {
72              
73             # We rotate the file if the expanded name has changed
74 1 50       10 $self->_fileHandle($filename, 1)
75             if ($self->{_filename} ne $filename);
76              
77             } else {
78 2         15 $self->_fileHandle($filename);
79             }
80              
81 3         12 return $self->{_filehandle};
82              
83             } # fileHandle();
84              
85             # --------------------------------------------------------------------
86              
87             ##
88             # Sets new file handle
89             #
90             # @param filename - filename to use
91             # @param doclose - [default:undef] specifies whether we close
92             # the existing filehandle or not
93             #
94             # @returns a valid fileHandle object
95              
96             sub _fileHandle
97             {
98              
99 3     3   6 my $self = shift;
100 3         9 my $filename = shift;
101 3   100     19 my $doclose = shift || undef;
102              
103             # Validate filename
104 3 50 33     32 $self->_error("First parameter must be a valid string")
105             unless (defined $filename and $filename =~ /\w/);
106              
107             # Close existing file handle
108 3 100       9 if ($doclose) {
109 1 50       12 $self->{_filehandle}->close()
110             || $self->_error(
111             sprintf("Unable to close file handle to %s : %s",
112             $filename, $!
113             ));
114             }
115              
116 3   33     93 $self->{_filehandle} = FileHandle->new(">> " . $filename)
117             || $self->error("Unable to open log file $filename : $!\n");
118              
119             # Set autoflush if necessary
120 3         649 $self->{_filehandle}->autoflush($self->{autoflush});
121              
122             # Update cached file name
123 3         177 $self->{_filename} = $filename;
124              
125 3         12 return $self->{_filehandle};
126              
127             } # _fileHandle()
128              
129             =head1 BUGS
130              
131             Please report any bugs or feature requests to
132             C, or through the
133             web interface at
134             L. I will be
135             notified, and then you'll automatically be notified of progress on
136             your bug as I make changes.
137              
138             =head1 SUPPORT
139              
140             You can find documentation for this module with the perldoc command.
141              
142             perldoc Log::Fine
143              
144             You can also look for information at:
145              
146             =over 4
147              
148             =item * AnnoCPAN: Annotated CPAN documentation
149              
150             L
151              
152             =item * CPAN Ratings
153              
154             L
155              
156             =item * RT: CPAN's request tracker
157              
158             L
159              
160             =item * Search CPAN
161              
162             L
163              
164             =back
165              
166             =head1 AUTHOR
167              
168             Christopher M. Fuhrman, C<< >>
169              
170             =head1 SEE ALSO
171              
172             L, L, L
173              
174             =head1 COPYRIGHT & LICENSE
175              
176             Copyright (c) 2008, 2010-2011, 2013 Christopher M. Fuhrman,
177             All rights reserved.
178              
179             This program is free software licensed under the...
180              
181             The BSD License
182              
183             The full text of the license can be found in the
184             LICENSE file included with this module.
185              
186             =cut
187              
188             1; # End of Log::Fine::Handle::File::Timestamp