File Coverage

blib/lib/Log/Fine/Logger.pm
Criterion Covered Total %
statement 49 49 100.0
branch 16 18 88.8
condition 26 42 61.9
subroutine 11 11 100.0
pod 5 5 100.0
total 107 125 85.6


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Log::Fine::Logger - Main logging object
5              
6             =head1 SYNOPSIS
7              
8             Provides an object through which to log.
9              
10             use Log::Fine;
11             use Log::Fine::Logger;
12              
13             # Get a new logging object
14             my $log = Log::Fine->logger("mylogger");
15              
16             # Alternatively, specify a custom map
17             my $log = Log::Fine->logger("mylogger", "Syslog");
18              
19             # Register a handle
20             $log->registerHandle( Log::Fine::Handle::Console->new() );
21              
22             # Log a message
23             $log->log(DEBG, "This is a really cool module!");
24              
25             # Illustrate use of the log skip API
26             package Some::Package::That::Overrides::Log::Fine::Logger;
27              
28             use base qw( Log::Fine::Logger );
29              
30             sub log
31             {
32             my $self = shift;
33             my $lvl = shift;
34             my $msg = shift;
35              
36             # Do some custom stuff to message
37              
38             # Make sure the formatter logs the correct calling method.
39             $self->incrSkip();
40             $self->SUPER::log($lvl, $msg);
41             $self->decrSkip();
42              
43             } # log()
44              
45             =head1 DESCRIPTION
46              
47             The Logger class is the main workhorse of the Log::Fine framework,
48             providing the main L method from which to log. In addition,
49             the Logger class provides means by which the developer can control the
50             parameter passed to any caller() call so information regarding the
51             correct stack frame is displayed.
52              
53             =cut
54              
55 19     19   112 use strict;
  19         38  
  19         684  
56 19     19   104 use warnings;
  19         35  
  19         766  
57              
58             package Log::Fine::Logger;
59              
60 19     19   107 use base qw( Log::Fine );
  19         45  
  19         2396  
61              
62 19     19   161 use Log::Fine;
  19         40  
  19         880  
63              
64             our $VERSION = $Log::Fine::VERSION;
65              
66             # Constant: LOG_SKIP_DEFAULT
67             #
68             # By default, calls to caller() will be given a stack frame of 2.
69              
70 19     19   178 use constant LOG_SKIP_DEFAULT => 2;
  19         111  
  19         16694  
71              
72             # --------------------------------------------------------------------
73              
74             =head2 decrSkip
75              
76             Decrements the value of the skip attribute by one
77              
78             =head3 Returns
79              
80             The newly decremented value
81              
82             =cut
83              
84 4     4 1 22 sub decrSkip { return --$_[0]->{_skip}; } # decrSkip()
85              
86             =head2 incrSkip
87              
88             Increments the value of the skip attribute by one
89              
90             =head3 Returns
91              
92             The newly incremented value
93              
94             =cut
95              
96 4     4 1 346 sub incrSkip { return ++$_[0]->{_skip}; } # incrSkip()
97              
98             =head2 log
99              
100             Logs the message at the given log level
101              
102             =head3 Parameters
103              
104             =over
105              
106             =item * level
107              
108             Level at which to log
109              
110             =item * message
111              
112             Message to log
113              
114             =back
115              
116             =head3 Returns
117              
118             The object
119              
120             =cut
121              
122             sub log
123             {
124              
125 10     10 1 24 my $self = shift;
126 10         22 my $lvl = shift;
127 10         25 my $msg = shift;
128              
129             # See if we have any handles defined
130 9         64 $self->_fatal("No handles defined!")
131             unless ( defined $self->{_handles}
132             and ref $self->{_handles} eq "ARRAY"
133 10 50 66     128 and scalar @{ $self->{_handles} } > 0);
      66        
134              
135             # Iterate through each handle, logging as appropriate
136 9         24 foreach my $handle (@{ $self->{_handles} }) {
  9         31  
137 9 100       90 $handle->msgWrite($lvl, $msg, $self->{_skip})
138             if $handle->isLoggable($lvl);
139             }
140              
141 9         2580 return $self;
142              
143             } # log()
144              
145             =head2 registerHandle
146              
147             Register one or more L objects with the logging
148             facility.
149              
150             =head3 Parameters
151              
152             =over
153              
154             =item * handle
155              
156             Can either be a valid Log::Fine::Handle object or an array ref
157             containing one or more Log::Fine::Handle objects
158              
159             =back
160              
161             =head3 Returns
162              
163             The object
164              
165             =cut
166              
167             sub registerHandle
168             {
169              
170 7     7 1 5144 my $self = shift;
171 7         23 my $obj = shift;
172              
173             # Initialize handles if we haven't already
174 7 100 66     58 $self->{_handles} = []
175             unless (defined $self->{_handles}
176             and ref $self->{_handles} eq "ARRAY");
177              
178 7 100 66     165 if ( defined $obj
    100 100        
      66        
      66        
      66        
179             and ref $obj
180             and UNIVERSAL::can($obj, 'isa')
181 3         14 and $obj->isa('Log::Fine::Handle')) {
182 3         11 push @{ $self->{_handles} }, $obj;
  3         13  
183             } elsif (defined $obj and ref $obj eq 'ARRAY' and scalar @{$obj} > 0) {
184              
185 3         8 foreach my $handle (@{$obj}) {
  3         10  
186 4 100 33     115 $self->_fatal( "Array ref must contain valid "
      33        
      66        
187             . "Log::Fine::Handle objects")
188             unless ( defined $handle
189             and ref $handle
190             and UNIVERSAL::can($handle, 'isa')
191             and $handle->isa('Log::Fine::Handle'));
192             }
193              
194 2         12 push @{ $self->{_handles} }, @{$obj};
  2         6  
  2         4  
195              
196             } else {
197 1         6 $self->_fatal( "first argument must either be a "
198             . "valid Log::Fine::Handle object\n"
199             . "or an array ref containing one or more "
200             . "valid Log::Fine::Handle objects\n");
201             }
202              
203 5         20 return $self;
204              
205             } # registerHandle()
206              
207             =head2 skip
208              
209             Getter/Setter for the objects skip attribute
210              
211             See L for details
212              
213             =head3 Returns
214              
215             The objects skip attribute
216              
217             =cut
218              
219             sub skip
220             {
221              
222 3     3 1 799 my $self = shift;
223 3         5 my $val = shift;
224              
225             # Should we be given a value, then set skip
226 3 100 66     22 $self->{_skip} = $val
227             if (defined $val and $val =~ /^\d+$/);
228              
229 3         18 return $self->{_skip};
230              
231             } # skip()
232              
233             # --------------------------------------------------------------------
234              
235             ##
236             # Initializes our object
237              
238             sub _init
239             {
240              
241 11     11   42 my $self = shift;
242              
243             # Validate name
244 11 100 66     256 $self->_fatal("Loggers need names!")
245             unless (defined $self->{name} and $self->{name} =~ /^\w+$/);
246              
247             # Set logskip if necessary
248 10 50 33     158 $self->{_skip} = LOG_SKIP_DEFAULT
249             unless ($self->{_skip} and $self->{_skip} =~ /\d+/);
250              
251 10         38 return $self;
252              
253             } # _init()
254              
255             =head1 BUGS
256              
257             Please report any bugs or feature requests to
258             C, or through the web interface at
259             L.
260             I will be notified, and then you'll automatically be notified of progress on
261             your bug as I make changes.
262              
263             =head1 SUPPORT
264              
265             You can find documentation for this module with the perldoc command.
266              
267             perldoc Log::Fine
268              
269             You can also look for information at:
270              
271             =over 4
272              
273             =item * AnnoCPAN: Annotated CPAN documentation
274              
275             L
276              
277             =item * CPAN Ratings
278              
279             L
280              
281             =item * RT: CPAN's request tracker
282              
283             L
284              
285             =item * Search CPAN
286              
287             L
288              
289             =back
290              
291             =head1 AUTHOR
292              
293             Christopher M. Fuhrman, C<< >>
294              
295             =head1 SEE ALSO
296              
297             L, L, L
298              
299             =head1 COPYRIGHT & LICENSE
300              
301             Copyright (c) 2008, 2010, 2013 Christopher M. Fuhrman,
302             All rights reserved
303              
304             This program is free software licensed under the...
305              
306             The BSD License
307              
308             The full text of the license can be found in the
309             LICENSE file included with this module.
310              
311             =cut
312              
313             1; # End of Log::Fine::Logger