blib/lib/Log/Log4perl/Lazy.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Log::Log4perl::Lazy;
6 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use Params::Lazy;
Subroutine BEGIN: called CC=1 SCAR=2.6
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 my ($self_pkg, @levels) = @_;
18 my $caller_pkg = caller;
19  
20 if (@levels == 0) {
21 @levels = @available_levels;
22 } else {
23 for my $level (@levels) {
24 unless ($is_available_level{$level}) {
25 croak qq("$level" is not exported by the $self_pkg module);
26 }
27 }
28 }
29  
30 my $logger = Log::Log4perl->get_logger($caller_pkg);
31 my @defined_levels;
32  
33 for my $level (@levels) {
34 my $method = lc($level);
35 my $is_method = "is_$method";
36  
37 *{$caller_pkg.'::'.$level} = sub {
38 if ($logger->$is_method) {
39 local $Log::Log4perl::caller_depth =
40 $Log::Log4perl::caller_depth + 1;
41 $logger->$method(force($_[0]));
42 }
43 };
44  
45 push @defined_levels, $level;
46 }
47  
48 Params::Lazy->import(map {
49 $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<TRACE>, C<DEBUG>, C<INFO>, C<WARN>, C<ERROR>, and C<FATAL>
73  
74 =head1 DESCRIPTION
75  
76 This module is an extension of L<Log::Log4perl>.
77  
78 You can simply write something like this:
79  
80 DEBUG 'obj = '.slow_func($obj);
81  
82 where the argument of the C<DEBUG> 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<slow_func> 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<slow_func>
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<Log::Log4perl> 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<< <mahiro at cpan.org> >>
119  
120 =head1 BUGS
121  
122 Please report any bugs or feature requests to C<bug-log-log4perl-lazy at rt.cpan.org>, or through
123 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-Log4perl-Lazy>. 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<https://github.com/mahiro/perl-Log-Log4perl-Lazy>
139  
140 =item * RT: CPAN's request tracker (report bugs here, alternatively)
141  
142 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Log4perl-Lazy>
143  
144 =item * AnnoCPAN: Annotated CPAN documentation
145  
146 L<http://annocpan.org/dist/Log-Log4perl-Lazy>
147  
148 =item * CPAN Ratings
149  
150 L<http://cpanratings.perl.org/d/Log-Log4perl-Lazy>
151  
152 =item * Search CPAN
153  
154 L<http://search.cpan.org/dist/Log-Log4perl-Lazy/>
155  
156 =back
157  
158 =head1 ACKNOWLEDGEMENTS
159  
160 L<Log::Log4perl>, L<Params::Lazy>
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<http://www.perlfoundation.org/artistic_license_2_0>
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
↑ Summary