File Coverage

blib/lib/Log/Fine/Handle/File.pm
Criterion Covered Total %
statement 48 49 97.9
branch 15 26 57.6
condition 11 27 40.7
subroutine 11 11 100.0
pod 2 2 100.0
total 87 115 75.6


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Log::Fine::Handle::File - Output log messages to a file
5              
6             =head1 SYNOPSIS
7              
8             Provides logging to a file
9              
10             use Log::Fine;
11             use Log::Fine::Handle::File;
12              
13             # Get a new logger
14             my $log = Log::Fine->logger("foo");
15              
16             # Create a file handle (default values shown)
17             my $handle = Log::Fine::Handle::File
18             ->new( name => 'file0',
19             mask => LOGMASK_EMERG | LOGMASK_ALERT | LOGMASK_CRIT | LOGMASK_ERR | LOGMASK_WARNING | LOGMASK_NOTICE | LOGMASK_INFO,
20             dir => "/var/log",
21             file => "myapp.log",
22             autoflush => 0 );
23              
24             # Register the handle
25             $log->registerHandle($handle);
26              
27             # Log something
28             $log->(INFO, "Opened new log handle");
29              
30             =head1 DESCRIPTION
31              
32             Log::Fine::Handle::File provides logging to a file. Note that this
33             module will log messages to a specific file. Support for dynamic
34             time-stamps in file names (e.g., C) is provided by
35             L. Further features, such as log
36             file rotation I L can be added by sub-classing this class.
37              
38             =head2 Constructor Parameters
39              
40             The following parameters can be passed to
41             Log::Fine::Handle::File->new():
42              
43             =over
44              
45             =item * name
46              
47             [optional] Name of this object (see L). Will be autoset if
48             not specified.
49              
50             =item * mask
51              
52             Mask to set the handle to (see L)
53              
54             =item * dir
55              
56             Directory to place the log file
57              
58             =item * file
59              
60             Name of the log file. Note that if the given file is an absolute
61             path, then C will be ignored.
62              
63             =item * autoclose
64              
65             [default: 0] If set to true, will close the filehandle after every
66             invocation of L. B will I slow
67             down logging speed if multiple messages are logged at once. Consider
68             autoflush instead
69              
70             =item * autoflush
71              
72             [default: 0] If set to true, will force file flush after every write or
73             print (see L and L)
74              
75             =back
76              
77             =cut
78              
79 6     6   4699 use strict;
  6         14  
  6         335  
80 6     6   37 use warnings;
  6         13  
  6         287  
81              
82             package Log::Fine::Handle::File;
83              
84 6     6   42 use base qw( Log::Fine::Handle );
  6         11  
  6         3596  
85              
86 6     6   43 use File::Basename;
  6         20  
  6         1851  
87 6     6   3048 use File::Spec::Functions;
  6         2863  
  6         683  
88 6     6   5169 use FileHandle;
  6         92768  
  6         49  
89 6     6   4609 use Log::Fine;
  6         13  
  6         5976  
90              
91             our $VERSION = $Log::Fine::Handle::VERSION;
92              
93             =head1 METHODS
94              
95             =head2 fileHandle
96              
97             Getter for file handle. If a file handle is not defined, then one
98             will be created.
99              
100             Override this method if you wish to support
101             features such as time-stamped and/or rotating files.
102              
103             =head3 Returns
104              
105             A L object
106              
107             =cut
108              
109             sub fileHandle
110             {
111              
112 13     13 1 5171 my $self = shift;
113              
114             # Should we already have a file handle defined, return it
115 13 50 66     260 return $self->{_filehandle}
      66        
      33        
      33        
116             if ( defined $self->{_filehandle}
117             and ref $self->{_filehandle}
118             and UNIVERSAL::can($self->{_filehandle}, 'isa')
119             and $self->{_filehandle}->isa("IO::File")
120             and defined fileno($self->{_filehandle}));
121              
122             # Generate file name
123 5 50       34 my $filename =
124             ($self->{dir} =~ /\w/)
125             ? catdir($self->{dir}, $self->{file})
126             : $self->{file};
127              
128             # Otherwise create a new one
129 5         54 $self->{_filehandle} = FileHandle->new(">> " . $filename);
130              
131 5 50       2077 $self->_fatal("Unable to open log file $filename : $!\n")
132             unless defined $self->{_filehandle};
133              
134             # Set autoflush if necessary
135 5         34 $self->{_filehandle}->autoflush($self->{autoflush});
136              
137 5         277 return $self->{_filehandle};
138              
139             } # fileHandle()
140              
141             =head2 msgWrite
142              
143             See L
144              
145             =cut
146              
147             sub msgWrite
148             {
149              
150 11     11 1 25 my $self = shift;
151 11         20 my $lvl = shift;
152 11         26 my $msg = shift;
153 11         23 my $skip = shift;
154              
155             # Grab a ref to our file handle
156 11         47 my $fh = $self->fileHandle();
157              
158             # Should we have a formatter defined, then use that,
159             # otherwise, just print the raw message
160 11 50       106 $msg = $self->{formatter}->format($lvl, $msg, $skip)
161             if defined $self->{formatter};
162              
163 11 50       821 print $fh $msg or $self->_error("Cannot write to file handle : $!");
164              
165             # Should {autoclose} be set, then close the file handle. This
166             # will force the creation of a new filehandle the next time
167             # this method is called
168 11 100       53 if ($self->{autoclose}) {
169 1 50       6 $fh->close()
170             || $self->_error(
171             sprintf("Unable to close filehandle to %s : %s",
172             catdir($self->{dir}, $self->{file}), $!
173             ));
174             }
175              
176 11         76 return $self;
177              
178             } # msgWrite()
179              
180             # --------------------------------------------------------------------
181              
182             ##
183             # Initializes our object
184              
185             sub _init
186             {
187              
188 7     7   18 my $self = shift;
189              
190             # Perform any necessary upper class initializations
191 7         74 $self->SUPER::_init();
192              
193             # Default directory is the current directory unless file is an
194             # absolute path
195 7 50 33     97 if ($self->{file} =~ /^\/|^[A-Za-z]:\\/) {
    50          
196 0         0 $self->{dir} = "";
197             } elsif (not defined $self->{dir} or not -d $self->{dir}) {
198 7         25 $self->{dir} = "./";
199             }
200              
201             # Default file name is the name of the invoking program
202             # suffixed with ".log"
203 7 50       31 $self->{file} = basename($0) . ".log"
204             unless defined $self->{file};
205              
206             # autoflush is disabled by default
207 7 50       25 $self->{autoflush} = 0
208             unless defined $self->{autoflush};
209              
210             # autoclose is disabled by default
211 7 100       38 $self->{autoclose} = 0
212             unless defined $self->{autoclose};
213              
214 7         22 return $self;
215              
216             } # _init()
217              
218             ##
219             # Called when this object is destroyed
220              
221             sub DESTROY
222             {
223              
224 5     5   12 my $self = shift;
225              
226             # Close our filehandle if necessary.
227 5 50 33     468 $self->{_filehandle}->close()
      33        
      33        
      33        
228             if ( defined $self->{_filehandle}
229             and ref $self->{_filehandle}
230             and UNIVERSAL::can($self->{_filehandle}, 'isa')
231             and $self->{_filehandle}->isa("IO::File")
232             and defined fileno($self->{_filehandle}));
233              
234             } # DESTROY
235              
236             =head1 BUGS
237              
238             Please report any bugs or feature requests to
239             C, or through the web interface at
240             L.
241             I will be notified, and then you'll automatically be notified of progress on
242             your bug as I make changes.
243              
244             =head1 SUPPORT
245              
246             You can find documentation for this module with the perldoc command.
247              
248             perldoc Log::Fine
249              
250             You can also look for information at:
251              
252             =over 4
253              
254             =item * AnnoCPAN: Annotated CPAN documentation
255              
256             L
257              
258             =item * CPAN Ratings
259              
260             L
261              
262             =item * RT: CPAN's request tracker
263              
264             L
265              
266             =item * Search CPAN
267              
268             L
269              
270             =back
271              
272             =head1 AUTHOR
273              
274             Christopher M. Fuhrman, C<< >>
275              
276             =head1 SEE ALSO
277              
278             L, L, L
279              
280             =head1 COPYRIGHT & LICENSE
281              
282             Copyright (c) 2008, 2010-2011, 2013 Christopher M. Fuhrman,
283             All rights reserved.
284              
285             This program is free software licensed under the...
286              
287             The BSD License
288              
289             The full text of the license can be found in the
290             LICENSE file included with this module.
291              
292             =cut
293              
294             1; # End of Log::Fine::Handle::File