File Coverage

blib/lib/Log/Log4perl/Lazy.pm
Criterion Covered Total %
statement 41 41 100.0
branch 5 6 83.3
condition n/a
subroutine 9 9 100.0
pod n/a
total 55 56 98.2


line stmt bran cond sub pod time code
1 2     2   54365 use 5.008;
  2         9  
  2         86  
2 2     2   13 use strict;
  2         5  
  2         98  
3 2     2   13 use warnings FATAL => 'all';
  2         15  
  2         137  
4              
5             package Log::Log4perl::Lazy;
6 2     2   11 no strict 'refs';
  2         4  
  2         99  
7 2     2   10 no warnings 'redefine';
  2         5  
  2         74  
8              
9 2     2   10 use Carp qw(croak);
  2         3  
  2         155  
10 2     2   1883 use Params::Lazy;
  2         5193  
  2         14  
11             require Log::Log4perl;
12              
13             my @available_levels = qw(TRACE DEBUG INFO WARN ERROR FATAL);
14             my %is_available_level = map {$_ => 1} @available_levels;
15              
16             sub import {
17 4     4   3231 my ($self_pkg, @levels) = @_;
18 4         16 my $caller_pkg = caller;
19              
20 4 100       19 if (@levels == 0) {
21 3         11 @levels = @available_levels;
22             } else {
23 1         4 for my $level (@levels) {
24 1 50       6 unless ($is_available_level{$level}) {
25 1         235 croak qq("$level" is not exported by the $self_pkg module);
26             }
27             }
28             }
29              
30 3         26 my $logger = Log::Log4perl->get_logger($caller_pkg);
31 3         675 my @defined_levels;
32              
33 3         9 for my $level (@levels) {
34 18         37 my $method = lc($level);
35 18         25 my $is_method = "is_$method";
36              
37 18         83 *{$caller_pkg.'::'.$level} = sub {
38 12 100   12   10867 if ($logger->$is_method) {
39 6         54 local $Log::Log4perl::caller_depth =
40             $Log::Log4perl::caller_depth + 1;
41 6         49 $logger->$method(force($_[0]));
42             }
43 18         73 };
44              
45 18         44 push @defined_levels, $level;
46             }
47              
48 18         68 Params::Lazy->import(map {
49 3         19 $caller_pkg.'::'.$_ => '^'
50             } @defined_levels);
51             }
52              
53             =head1 NAME
54              
55             Log::Log4perl::Lazy - Lazily evaluate logging arguments
56              
57             =head1 VERSION
58              
59             Version 0.02
60              
61             =cut
62              
63             our $VERSION = '0.02';
64              
65             =head1 SYNOPSIS
66              
67             use Log::Log4perl::Lazy;
68             DEBUG 'debug message';
69              
70             =head1 EXPORT
71              
72             C, C, C, C, C, and C
73              
74             =head1 DESCRIPTION
75              
76             This module is an extension of L.
77              
78             You can simply write something like this:
79              
80             DEBUG 'obj = '.slow_func($obj);
81              
82             where the argument of the C subroutine will be *lazily* evaluated.
83              
84             This means, if the DEBUG level is not enabled for the current package,
85             the argument is not evaluated at all. As a result, C is not
86             called when it is unnecessary.
87              
88             It allows you to avoid writing the if-statements everywhere like this:
89              
90             use Log::Log4perl qw(get_logger);
91             my $logger = get_logger;
92             $logger->debug('obj1 = '.slow_func($obj1)) if $logger->is_debug;
93             $logger->debug('obj2 = '.slow_func($obj2)) if $logger->is_debug;
94             $logger->debug('obj3 = '.slow_func($obj3)) if $logger->is_debug;
95              
96             while you don't need to worry about the performance overhead of C
97             in the production code.
98              
99             =head1 LIMITATIONS
100              
101             The current version does not support the object-oriented interface.
102              
103             With the non-OO subroutines, although C allows you to pass
104             as many arguments as you want, this module only allows you to pass one
105             argument.
106              
107             # This does not work with Log::Log4perl::Lazy.
108             DEBUG 'a', 'b', 'c';
109              
110             # You should concatenate the argument into one.
111             DEBUG 'a'.'b'.'c';
112              
113             In the above, if the debug level is not enabled, the concatenation will
114             not take place, so no worries about overhead!
115              
116             =head1 AUTHOR
117              
118             Mahiro Ando, C<< >>
119              
120             =head1 BUGS
121              
122             Please report any bugs or feature requests to C, or through
123             the web interface at L. I will be notified, and then you'll
124             automatically be notified of progress on your bug as I make changes.
125              
126             =head1 SUPPORT
127              
128             You can find documentation for this module with the perldoc command.
129              
130             perldoc Log::Log4perl::Lazy
131              
132             You can also look for information at:
133              
134             =over 4
135              
136             =item * GitHub repository (report bugs here)
137              
138             L
139              
140             =item * RT: CPAN's request tracker (report bugs here, alternatively)
141              
142             L
143              
144             =item * AnnoCPAN: Annotated CPAN documentation
145              
146             L
147              
148             =item * CPAN Ratings
149              
150             L
151              
152             =item * Search CPAN
153              
154             L
155              
156             =back
157              
158             =head1 ACKNOWLEDGEMENTS
159              
160             L, L
161              
162             =head1 LICENSE AND COPYRIGHT
163              
164             Copyright 2013 Mahiro Ando.
165              
166             This program is free software; you can redistribute it and/or modify it
167             under the terms of the the Artistic License (2.0). You may obtain a
168             copy of the full license at:
169              
170             L
171              
172             Any use, modification, and distribution of the Standard or Modified
173             Versions is governed by this Artistic License. By using, modifying or
174             distributing the Package, you accept this license. Do not use, modify,
175             or distribute the Package, if you do not accept this license.
176              
177             If your Modified Version has been derived from a Modified Version made
178             by someone other than you, you are nevertheless required to ensure that
179             your Modified Version complies with the requirements of this license.
180              
181             This license does not grant you the right to use any trademark, service
182             mark, tradename, or logo of the Copyright Holder.
183              
184             This license includes the non-exclusive, worldwide, free-of-charge
185             patent license to make, have made, use, offer to sell, sell, import and
186             otherwise transfer the Package with respect to any patent claims
187             licensable by the Copyright Holder that are necessarily infringed by the
188             Package. If you institute patent litigation (including a cross-claim or
189             counterclaim) against any party alleging that the Package constitutes
190             direct or contributory patent infringement, then this Artistic License
191             to you shall terminate on the date that such litigation is filed.
192              
193             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
194             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
195             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
196             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
197             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
198             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
199             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
200             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
201              
202             =cut
203              
204             1; # End of Log::Log4perl::Lazy