File Coverage

blib/lib/Log/Fine.pm
Criterion Covered Total %
statement 62 69 89.8
branch 16 26 61.5
condition 25 55 45.4
subroutine 18 18 100.0
pod 5 5 100.0
total 126 173 72.8


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Log::Fine - Yet another logging framework
5              
6             =head1 SYNOPSIS
7              
8             Provides fine-grained logging and tracing.
9              
10             use Log::Fine;
11             use Log::Fine::Levels::Syslog; # exports log levels
12             use Log::Fine::Levels::Syslog qw( :masks ); # exports masks and levels
13              
14             # Build a Log::Fine object
15             my $fine = Log::Fine->new();
16              
17             # Specify a custom map
18             my $fine = Log::Fine->new(levelmap => "Syslog");
19              
20             # Get the name of the log object
21             my $name = $fine->name();
22              
23             # Use logger() to get a new logger object. If "foo" is not
24             # defined then a new logger with the name "foo" will be created.
25             my $log = Log::Fine->logger("foo");
26              
27             # Get list of names of defined logger objects
28             my @loggers = $log->listLoggers();
29              
30             # Register a handle, in this case a handle that logs to console.
31             my $handle = Log::Fine::Handle::Console->new();
32             $log->registerHandle( $handle );
33              
34             # Log a message
35             $log->log(INFO, "Log object successfully initialized");
36              
37             =head1 DESCRIPTION
38              
39             Log::Fine provides a logging framework for application developers
40             who need a fine-grained logging mechanism in their program(s). By
41             itself, Log::Fine provides a mechanism to get one or more logging
42             objects (called I) from its stored namespace. Most logging
43             is then done through a logger object that is specific to the
44             application.
45              
46             For a simple functional interface to the logging sub-system, see
47             L.
48              
49             =head2 Handles
50              
51             Handlers provides a means to output log messages in one or more
52             ways. Currently, the following handles are provided:
53              
54             =over 4
55              
56             =item * L
57              
58             Provides logging to C or C
59              
60             =item * L
61              
62             Provides logging via email. Useful for delivery to one or more pager
63             addresses.
64              
65             =item * L
66              
67             Provides logging to a file
68              
69             =item * L
70              
71             Same thing with support for time-stamped files
72              
73             =item * L
74              
75             Provides logging to L
76              
77             =back
78              
79             See the relevant perldoc information for more information. Additional
80             handlers can be defined to user taste.
81              
82             =cut
83              
84 19     19   255855 use strict;
  19         27  
  19         460  
85 19     19   59 use warnings;
  19         25  
  19         786  
86              
87             package Log::Fine;
88              
89             require 5.008003;
90              
91 19     19   84 use Carp qw( cluck confess );
  19         23  
  19         940  
92 19     19   6588 use Log::Fine::Levels;
  19         133  
  19         390  
93 19     19   6330 use Log::Fine::Logger;
  19         26  
  19         452  
94 19     19   7873 use POSIX qw( strftime );
  19         88744  
  19         81  
95              
96             our $VERSION = '0.65';
97              
98             =head2 Formatters
99              
100             A formatter specifies how Log::Fine displays messages. When a message
101             is logged, it gets passed through a formatter object, which adds any
102             additional information such as a time-stamp or caller information.
103              
104             By default, log messages are formatted as follows using the
105             L formatter object.
106              
107             [
108              
109             For more information on the customization of log messages, see
110             L.
111              
112             =head1 INSTALLATION
113              
114             To install Log::Fine:
115              
116             perl Makefile.PL
117             make
118             make test
119             make install
120              
121             =cut
122              
123             # Private Methods
124             # --------------------------------------------------------------------
125              
126             {
127              
128             # Private global variables
129             my $levelmap;
130             my $loggers = {};
131             my $objcount = 0;
132              
133             # Getter/setter for levelMap. Note that levelMap can only be
134             # set _once_. Once levelmap is set, any other value passed,
135             # whether a valid object or not, will be ignored!
136             sub _levelMap
137             {
138              
139 3381     3381   2657 my $map = shift;
140              
141 3381 100 66     12698 if ( defined $map
    50 66        
      33        
      33        
      33        
142             and ref $map
143             and UNIVERSAL::can($map, 'isa')
144             and $map->isa("Log::Fine::Levels")
145             and not $levelmap) {
146 16         32 $levelmap = $map;
147             } elsif (defined $map and not $levelmap) {
148 0   0     0 _fatal(sprintf("Invalid Value: \"%s\"", $map || "{undef}"));
149             }
150              
151 3381         11960 return $levelmap;
152              
153             } # _levelMap()
154              
155 43     43   243 sub _logger { return $loggers }
156 827     827   1910 sub _objectCount { return $objcount }
157 834     834   870 sub _incrObjectCount { return ++$objcount }
158              
159             }
160              
161             # --------------------------------------------------------------------
162              
163             =head1 METHODS
164              
165             The Log::Fine module, by itself, provides getters & setter methods for
166             loggers and level classes. After a logger is created, further actions
167             are done through the logger object. The following two constructors
168             are defined:
169              
170             =head2 new
171              
172             Creates a new Log::Fine object.
173              
174             =head3 Parameters
175              
176             A hash with the following keys
177              
178             =over
179              
180             =item * levelmap
181              
182             [default: Syslog] Name of level map to use. See L
183             for further details
184              
185             =item * no_croak
186              
187             [optional] If set to true, then do not L when
188             L<_error> is called.
189              
190             =item * err_callback
191              
192             [optional] If defined to a valid CODE ref, then this subroutine will
193             be called instead of L<_fatal> when L<_error> is called.
194              
195             =back
196              
197             =head3 Returns
198              
199             The newly blessed object
200              
201             =cut
202              
203             sub new
204             {
205              
206 845     845 1 806468 my $class = shift;
207 845         1646 my %h = @_;
208              
209             # Bless the hash into a class
210 845         1238 my $self = bless \%h, $class;
211              
212             # Perform any necessary initializations
213 845         2064 $self->_init();
214              
215 840         1679 return $self;
216              
217             } # new()
218              
219             =head2 listLoggers
220              
221             Provides list of currently defined loggers
222              
223             =head3 Parameters
224              
225             None
226              
227             =head3 Returns
228              
229             Array containing list of currently defined loggers
230              
231             =cut
232              
233 8     8 1 8 sub listLoggers { return keys %{ _logger() } }
  8         17  
234              
235             =head2 levelMap
236              
237             Getter for the global level map.
238              
239             =head3 Returns
240              
241             A L subclass
242              
243             =cut
244              
245 81     81 1 3549 sub levelMap { return _levelMap() }
246              
247             =head2 logger
248              
249             Getter/Constructor for a logger object.
250              
251             =head3 Parameters
252              
253             =over
254              
255             =item * logger name
256              
257             The name of the logger object. If the specified logger object does
258             not exist, then a new one will be created.
259              
260             =back
261              
262             =head3 Returns
263              
264             an L object
265              
266             =cut
267              
268             sub logger
269             {
270              
271 11     11 1 85450 my $self = shift;
272 11         18 my $name = shift; # name of logger
273              
274             # Validate name
275 11 50 33     106 $self->_fatal("First parameter must be a valid name!")
276             unless (defined $name and $name =~ /\w/);
277              
278             # Should the requested logger be found, then return it,
279             # otherwise store and return a newly created logger object
280             # with the given name
281             _logger()->{$name} = Log::Fine::Logger->new(name => $name)
282             unless ( defined _logger()->{$name}
283             and ref _logger()->{$name}
284             and UNIVERSAL::can(_logger()->{$name}, 'isa')
285 11 50 66     64 and _logger()->{$name}->isa('Log::Fine::Logger'));
      66        
      33        
286              
287 11         27 return _logger()->{$name};
288              
289             } # logger()
290              
291             =head2 name
292              
293             Getter for name of object
294              
295             =head3 Parameters
296              
297             None
298              
299             =head3 Returns
300              
301             String containing name of object, otherwise undef
302              
303             =cut
304              
305 44   50 44 1 66154 sub name { return $_[0]->{name} || undef }
306              
307             # --------------------------------------------------------------------
308              
309             =head2 _error
310              
311             Private internal method that is called when an error condition is
312             encountered. Will call L<_fatal> unless C<{no_croak}> is defined.
313              
314             This method can be overridden per taste.
315              
316             =head3 Parameters
317              
318             =over
319              
320             =item message
321              
322             Message passed to L.
323              
324             =back
325              
326             =cut
327              
328             sub _error
329             {
330 2     2   11 my $self;
331             my $msg;
332              
333             # How were we called?
334 2 50       7 if (scalar @_ > 1) {
335 2         3 $self = shift;
336 2         5 $msg = shift;
337             } else {
338 0         0 $msg = shift;
339             }
340              
341 2 50 33     35 if ( defined $self
      33        
      33        
342             and ref $self
343             and UNIVERSAL::can($self, 'isa')
344             and $self->isa("Log::Fine")) {
345              
346 2 50 33     12 if (defined $self->{err_callback}
    0          
347             and ref $self->{err_callback} eq "CODE") {
348 2         3 &{ $self->{err_callback} }($msg);
  2         7  
349             } elsif ($self->{no_croak}) {
350 0         0 $self->{_err_msg} = $msg;
351 0         0 cluck $msg;
352             } else {
353 0         0 $self->_fatal($msg);
354             }
355              
356             } else {
357 0         0 _fatal($msg);
358             }
359              
360             }
361              
362             =head2 _fatal
363              
364             Private internal method that is called when a fatal (non-recoverable)
365             condition is encountered. Calls L with given error
366             message.
367              
368             While this method can be overridden, this is generally not advised.
369              
370             =head3 Parameters
371              
372             =over
373              
374             =item message
375              
376             Message passed to L.
377              
378             =back
379              
380             =cut
381              
382             sub _fatal
383             {
384              
385 10     10   15 my $self;
386             my $msg;
387              
388             # How were we called?
389 10 50       32 if (scalar @_ > 1) {
390 10         16 $self = shift;
391 10         15 $msg = shift;
392             } else {
393 0         0 $msg = shift;
394             }
395              
396 10         1405 confess $msg;
397              
398             } # _fatal()
399              
400             ##
401             # Initializes our object
402              
403             sub _init
404             {
405              
406 834     834   673 my $self = shift;
407              
408 834         978 _incrObjectCount();
409              
410             # We set the objects name unless it is already set for us
411 834 100 66     2175 unless (defined $self->{name} and $self->{name} =~ /\w/) {
412              
413             # grab the class name
414 827         1373 $self->{name} = ref $self;
415 827         4441 $self->{name} =~ /\:(\w+)$/;
416 827         1826 $self->{name} = lc($+) . _objectCount();
417              
418             }
419              
420             # Validate {err_callback}
421 834 100       1727 if (defined $self->{err_callback}) {
422             $self->_fatal("{err_callback} must be a valid code ref")
423 2 100       17 unless ref $self->{err_callback} eq "CODE";
424             }
425              
426             # Set our levels if we need to
427 833 50 66     1029 _levelMap(Log::Fine::Levels->new($self->{levelmap}))
      66        
      33        
428             unless ( defined _levelMap()
429             and ref _levelMap()
430             and UNIVERSAL::can(_levelMap(), 'isa')
431             and _levelMap()->isa("Log::Fine::Levels"));
432              
433 833         1701 return $self;
434              
435             } # _init()
436              
437             # is "Python" a dirty word in perl POD documentation? Oh well.
438              
439             =head1 ACKNOWLEDGMENTS
440              
441             I'd like the thank the following people for either inspiration or past
442             work on logging: Josh Glover for his work as well as teaching me all I
443             know about object-oriented programming in perl. Dan Boger for taking
444             the time and patience to review this code and offer his own
445             suggestions. Additional thanks to Tom Maher and Chris Josephs for
446             encouragement.
447              
448             =head2 Related Modules/Frameworks
449              
450             The following logging frameworks provided inspiration for parts of Log::Fine.
451              
452             =over 4
453              
454             =item
455              
456             Dave Rolsky's L module
457              
458             =item
459              
460             Sun Microsystem's C framework
461              
462             =item
463              
464             The Python logging package
465              
466             =back
467              
468             =head1 BUGS
469              
470             Please report any bugs or feature requests to
471             C, or through the web interface at
472             L.
473             I will be notified, and then you'll automatically be notified of progress on
474             your bug as I make changes.
475              
476             =head1 SUPPORT
477              
478             You can find documentation for this module with the perldoc command.
479              
480             perldoc Log::Fine
481              
482             You can also look for information at:
483              
484             =over 4
485              
486             =item * AnnoCPAN: Annotated CPAN documentation
487              
488             L
489              
490             =item * CPAN Ratings
491              
492             L
493              
494             =item * RT: CPAN's request tracker
495              
496             L
497              
498             =item * Search CPAN
499              
500             L
501              
502             =back
503              
504             =head1 CONTRIBUTING
505              
506             Want to contribute? The source code repository for Log::Fine is now
507             available at L. To clone your
508             own copy:
509              
510             $ git clone git://github.com/cfuhrman/log-fine.git
511              
512             Signed patches generated by L(1) may be submitted
513             L.
514              
515             =head1 AUTHOR
516              
517             Christopher M. Fuhrman, C<< >>
518              
519             =head1 SEE ALSO
520              
521             L, L, L, L,
522             L, L, L
523              
524             =head1 COPYRIGHT & LICENSE
525              
526             Copyright (c) 2008-2011, 2013 Christopher M. Fuhrman,
527             All rights reserved.
528              
529             This program is free software licensed under the...
530              
531             The BSD License
532              
533             The full text of the license can be found in the
534             LICENSE file included with this module.
535              
536             =cut
537              
538             1; # End of Log::Fine