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   1129 use strict;
  2         2  
  2         44  
40 2     2   7 use warnings;
  2         2  
  2         55  
41              
42             package Log::Fine::Handle::File::Timestamp;
43              
44 2     2   7 use base qw( Log::Fine::Handle::File );
  2         1  
  2         406  
45              
46             #use Data::Dumper;
47 2     2   9 use File::Spec::Functions;
  2         1  
  2         117  
48 2     2   7 use FileHandle;
  2         2  
  2         5  
49 2     2   454 use POSIX qw( strftime );
  2         2  
  2         9  
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 4 my $self = shift;
65 3         210 my $filename = strftime($self->{file}, localtime(time));
66              
67 3 100 66     43 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             $self->_fileHandle($filename, 1)
75 1 50       8 if ($self->{_filename} ne $filename);
76              
77             } else {
78 2         4 $self->_fileHandle($filename);
79             }
80              
81 3         6 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   4 my $self = shift;
100 3         6 my $filename = shift;
101 3   100     9 my $doclose = shift || undef;
102              
103             # Validate filename
104 3 50 33     20 $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       7 if ($doclose) {
109             $self->{_filehandle}->close()
110 1 50       19 || $self->_error(sprintf("Unable to close file handle to %s : %s", $filename, $!));
111             }
112              
113 3   33     84 $self->{_filehandle} = FileHandle->new(">> " . $filename)
114             || $self->error("Unable to open log file $filename : $!\n");
115              
116             # Set autoflush if necessary
117 3         370 $self->{_filehandle}->autoflush($self->{autoflush});
118              
119             # Update cached file name
120 3         103 $self->{_filename} = $filename;
121              
122 3         7 return $self->{_filehandle};
123              
124             } # _fileHandle()
125              
126             =head1 BUGS
127              
128             Please report any bugs or feature requests to
129             C, or through the
130             web interface at
131             L. I will be
132             notified, and then you'll automatically be notified of progress on
133             your bug as I make changes.
134              
135             =head1 SUPPORT
136              
137             You can find documentation for this module with the perldoc command.
138              
139             perldoc Log::Fine
140              
141             You can also look for information at:
142              
143             =over 4
144              
145             =item * AnnoCPAN: Annotated CPAN documentation
146              
147             L
148              
149             =item * CPAN Ratings
150              
151             L
152              
153             =item * RT: CPAN's request tracker
154              
155             L
156              
157             =item * Search CPAN
158              
159             L
160              
161             =back
162              
163             =head1 AUTHOR
164              
165             Christopher M. Fuhrman, C<< >>
166              
167             =head1 SEE ALSO
168              
169             L, L, L
170              
171             =head1 COPYRIGHT & LICENSE
172              
173             Copyright (c) 2008, 2010-2011, 2013 Christopher M. Fuhrman,
174             All rights reserved.
175              
176             This program is free software licensed under the...
177              
178             The BSD License
179              
180             The full text of the license can be found in the
181             LICENSE file included with this module.
182              
183             =cut
184              
185             1; # End of Log::Fine::Handle::File::Timestamp