File Coverage

blib/lib/Log/Fine/Levels.pm
Criterion Covered Total %
statement 44 44 100.0
branch 1 2 50.0
condition 1 2 50.0
subroutine 12 12 100.0
pod 7 7 100.0
total 65 67 97.0


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Log::Fine::Levels - Define variable logging levels
5              
6             =head1 SYNOPSIS
7              
8             Provides logging translations
9              
10             use Log::Fine::Levels;
11              
12             # Instantiate the levels object using the default translations
13             my $levels = Log::Fine::Levels->new();
14              
15             # Instantiate the levels object using customized translations
16             my $levels = Log::Fine::Levels->new("Java");
17              
18             # Supported methods
19             my @l = $levels->logLevels(); # grab list of levels
20             my @m = $levels->logMasks(); # grab list of masks
21              
22             # Translation methods
23             my $val = $levels->levelToValue("INFO");
24             my $bitmask = $levels->maskToValue("LOGMASK_INFO");
25             my $lvl = $levels->valueToLevel(3);
26              
27             =head1 DESCRIPTION
28              
29             Log::Fine::Levels is used by the L framework to translate
30             customizable log levels (such as INFO, DEBUG, WARNING, etc) to and
31             from an associated value as well as convenience methods for
32             interacting with log levels (such as grabbing a list of levels).
33              
34             In addition, the L framework supports the notion of a
35             I, which is used for customizing output. See
36             L for more details as to how masks are used.
37              
38             =head2 Customization
39              
40             Log::Fine::Levels only provides methods for interacting with log
41             levels and associated log masks. In order to define levels and masks,
42             it I be overridden. Note that, by default, the
43             L class is used to define log levels.
44              
45             =head2 Independence
46              
47             Finally, Log::Fine::Levels is written to be independant of the
48             L framework and, as such, does not inherit any methods from
49             L. This allows developers to use Log::Fine::Levels by
50             itself for defining customizable level packages for use in their own
51             programs.
52              
53             =cut
54              
55 19     19   96 use strict;
  19         22  
  19         419  
56 19     19   54 use warnings;
  19         22  
  19         456  
57              
58             package Log::Fine::Levels;
59              
60 19     19   61 use Carp;
  19         32  
  19         786  
61 19     19   62 use Log::Fine;
  19         28  
  19         759  
62              
63             our $VERSION = $Log::Fine::VERSION;
64              
65             # Constants
66             # --------------------------------------------------------------------
67              
68 19     19   63 use constant DEFAULT_LEVELMAP => "Syslog";
  19         23  
  19         7302  
69              
70             # --------------------------------------------------------------------
71              
72             =head1 METHODS
73              
74             The following methods are provided:
75              
76             =head2 new
77              
78             Creates a new Log::Fine::Levels object
79              
80             =head3 Parameters
81              
82             =over
83              
84             =item * levelmap
85              
86             The name of the level map to use (e.g., C, C, etc)
87              
88             =back
89              
90             =head3 Returns
91              
92             an L object
93              
94             =cut
95              
96             sub new
97             {
98              
99 16     16 1 28 my $class = shift;
100 16   50     105 my $lvlmap = shift || DEFAULT_LEVELMAP;
101              
102             # Construct the string containing name of sub-class
103 16         48 my $levelClass = join("::", $class, $lvlmap);
104              
105             # Validate levelclass and return if successful
106 16         1015 eval "require $levelClass";
107              
108 16 50       89 confess "Error : Level Class $levelClass does not exist : $@"
109             if $@;
110              
111 16         100 return $levelClass->new();
112              
113             } # new()
114              
115             =head2 bitmaskAll
116              
117             Getter for a bitmask representing B possible values
118              
119             =head2 Returns
120              
121             Bitmask representing all possible mask values
122              
123             =cut
124              
125             sub bitmaskAll
126             {
127              
128 26     26 1 37 my $self = shift;
129 26         28 my $mask = 0; # bitmask
130              
131             # bitor all the mask values together
132 26         27 $mask |= $self->MASK_MAP->{$_} foreach (keys %{ $self->MASK_MAP });
  26         280  
133              
134 26         89 return $mask;
135              
136             } # bitmaskAll()
137              
138             =head2 levelToValue
139              
140             Level name to numeric value
141              
142             =head3 Parameters
143              
144             =over
145              
146             =item * level name
147              
148             The name of the level
149              
150             =back
151              
152             =head3 Returns
153              
154             The numeric value representing the given level name. Undef if name is
155             not defined
156              
157             =cut
158              
159 15     15 1 8986 sub levelToValue { return $_[0]->LVLTOVAL_MAP->{ $_[1] }; }
160              
161             =head2 logLevels
162              
163             Getter for all log levels
164              
165             =head3 Returns
166              
167             An array representing all level names, sorted by ascending numeric
168             value
169              
170             =cut
171              
172             sub logLevels
173             {
174              
175 2     2 1 4814 my $self = shift;
176 2         5 my @lvls;
177              
178             # Construct array sorted by level value (ascending) and return
179 2         3 push @lvls, $self->VALTOLVL_MAP->{$_} foreach (sort { $a <=> $b } (keys %{ $self->VALTOLVL_MAP }));
  29         45  
  2         18  
180              
181 2         10 return @lvls;
182              
183             } # logLevels()
184              
185             =head2 logMasks
186              
187             Getter for all log masks
188              
189             =head3 Returns
190              
191             An array representing all mask names, sorted by ascending numeric
192             value
193              
194             =cut
195              
196             sub logMasks
197             {
198              
199 4     4 1 12 my $self = shift;
200 4         8 my $vtom = {};
201 4         3 my @masks;
202              
203             # Build hash of mask values to mask names
204 4         7 $vtom->{ $self->MASK_MAP->{$_} } = $_ foreach (keys %{ $self->MASK_MAP });
  4         58  
205              
206             # Construct array sorted by mask value (ascending) and return
207 4         6 push @masks, $vtom->{$_} foreach (sort { $a <=> $b } (keys %{$vtom}));
  61         64  
  4         14  
208              
209 4         20 return @masks;
210              
211             } # logMasks()
212              
213             =head2 maskToValue
214              
215             Mask name to numeric value
216              
217             =head3 Parameters
218              
219             =over
220              
221             =item * mask name
222              
223             The name of the mask
224              
225             =back
226              
227             =head3 Returns
228              
229             The numeric value representing the given mask name. Undef if name is
230             not defined
231              
232             =cut
233              
234 46     46 1 186 sub maskToValue { return $_[0]->MASK_MAP->{ $_[1] }; }
235              
236             =head2 valueToLevel
237              
238             Level value to level name
239              
240             =head3 Parameters
241              
242             =over
243              
244             =item * numeric value
245              
246             The numeric value representing a level
247              
248             =back
249              
250             =head3 Returns
251              
252             The level name associated with the given numeric value. Undef if the
253             value is not defined
254              
255             =cut
256              
257 43     43 1 401 sub valueToLevel { return $_[0]->VALTOLVL_MAP->{ $_[1] }; }
258              
259             =head1 BUGS
260              
261             Please report any bugs or feature requests to
262             C, or through the web interface at
263             L.
264             I will be notified, and then you'll automatically be notified of progress on
265             your bug as I make changes.
266              
267             =head1 SUPPORT
268              
269             You can find documentation for this module with the perldoc command.
270              
271             perldoc Log::Fine::Levels
272              
273             You can also look for information at:
274              
275             =over 4
276              
277             =item * AnnoCPAN: Annotated CPAN documentation
278              
279             L
280              
281             =item * CPAN Ratings
282              
283             L
284              
285             =item * RT: CPAN's request tracker
286              
287             L
288              
289             =item * Search CPAN
290              
291             L
292              
293             =back
294              
295             =head1 AUTHOR
296              
297             Christopher M. Fuhrman, C<< >>
298              
299             =head1 SEE ALSO
300              
301             L, L, L, L
302              
303             =head1 COPYRIGHT & LICENSE
304              
305             Copyright (c) 2009, 2010, 2013 Christopher M. Fuhrman,
306             All rights reserved.
307              
308             This program is free software licensed under the...
309              
310             The BSD License
311              
312             The full text of the license can be found in the
313             LICENSE file included with this module.
314              
315             =cut
316              
317             1; # End of Log::Fine::Levels