lib/Log4Perl/ImportHandle.pm

filter: Statementstmt 96.3% 26 / 27 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 94.7% 36 / 38 CC 5 SCAR 11.7
CC
5
Cov
94%
import
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.2
↑ Summary
1 package Log4Perl::ImportHandle; # Imports a Log4Perl handle with category
2 2 $Log4Perl::ImportHandle::VERSION = '0.033';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 # This class imports an easy to use Log4Perl handle to the current class. Instead of
9 # the not recommended way to use direct functions like DEBUG(), here you can define
10 # a category.
11 #
12 # SYNOPSIS
13 # ========
14 #
15 # # Imports function LOG() to access category 'area1'
16 # use Log4Perl::ImportHandle LOG => 'area1'
17 #
18 # # Imports also function LOG2() to access category 'area2'
19 # Log4Perl::ImportHandle LOG => 'area1',LOG2 => 'area2
20 #
21 # # Imports function logger() to access category '' (default)
22 # use Log4Perl::ImportHandle logger
23 #
24 # # Imports function LOG() to access category '' (default)
25 # # LOG is the default function name.
26 # use Log4Perl::ImportHandle
27 #
28 # LOG->debug('hello');
29 #
30 # WHY
31 # ===
32 # Please have a look at the normal way to handle with Log4Perl:
33 #
34 # my $log = Log::Log4perl->get_logger('area1');
35 # $log->debug('hello');
36 #
37 # For classes with some methods, that is not very nice to read and maintain. You will
38 # also need that lines in every method again.
39 #
40 # With Log4Perl::ImportHandle it gets a bit smoother:
41 #
42 # use Log4Perl::ImportHandle
43 #
44 # LOG->debug('hello');
45 #
46 # Once you called it in the header via 'use', you can easily access it with the default function 'LOG'
47 # or any other you defined.
48 #
49 # HOWTO
50 # =====
51 # As the SYNOPSIS examples above, just 'use' the class and add maybe some parameters.
52 #
53 # What is called a handler here, is just a function, that returns the same as get_logger() from Log4Perl.
54 #
55 # no parameters - Sets the default handle 'LOG'.
56 #
57 # one parameter - Sets the handle (function) name.
58 #
59 # two parameters or pairs - first is the handlename and the second the category name. You can define many pairs.
60 #
61 #
62 # LICENSE
63 # =======
64 # You can redistribute it and/or modify it under the conditions of LGPL.
65 #
66 # AUTHOR
67 # ======
68 # Andreas Hernitscheck ahernit(AT)cpan.org
69  
70  
71  
72  
73  
74  
75  
76  
77 # Don't use that method! It is not a method but used by perl when
78 # this class is included to export a function in current namespace.
79 4 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
80 4 my $pkg = shift;
81  
82 4 my @param = @_;
83  
84 4 my %pair;
85  
86
87 4 ▶ if (scalar(@param) == 0){ ## no parameters
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @param == 0) { } 2 2 4
elsif (scalar @param == 1) { } 1 1 2
elsif (scalar @param == 3) { } 0 1 1
88  
89 2 $pair{'LOG'} = '';
90  
91 }elsif (scalar(@param) == 1){ ## one parameter
92  
93 1 $pair{ $param[0] } = '';
94  
95 }elsif(scalar(@param) == 3){
96 0 croak "You can not call the importer with an unpair amount of parameters beyond 1";
97 }else{ ## any parameter
98 1 %pair = @param;
99 }
100  
101  
102 4 my $caller = caller;
103  
104 4 require Exporter::AutoClean;
105  
106 4 foreach my $exportfunc (keys %pair){
107  
108 5 my $cat = $pair{$exportfunc};
109  
110 # must be done before to keep reference to log4perl for desctruction
111 5 my $logger = Log::Log4perl->get_logger( $cat );
112  
113 my %exports = (
114 4 ▶ "$exportfunc" => sub { return $logger }, # closure
Subroutine __ANON__: called CC=1 SCAR=0.0
115 5 );
116  
117 # installs the function $exportfunc in the calling class
118 5 Exporter::AutoClean->export( $caller, %exports );
119  
120 }
121  
122  
123  
124 }
125  
126  
127  
128 2 1;
129  
130 #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
131  
132 =head1 NAME
133  
134 Log4Perl::ImportHandle - Imports a Log4Perl handle with category
135  
136  
137 =head1 SYNOPSIS
138  
139  
140 # Imports function LOG() to access category 'area1'
141 use Log4Perl::ImportHandle LOG => 'area1'
142  
143 # Imports also function LOG2() to access category 'area2'
144 Log4Perl::ImportHandle LOG => 'area1',LOG2 => 'area2
145  
146 # Imports function logger() to access category '' (default)
147 use Log4Perl::ImportHandle logger
148  
149 # Imports function LOG() to access category '' (default)
150 # LOG is the default function name.
151 use Log4Perl::ImportHandle
152  
153 LOG->debug('hello');
154  
155  
156  
157 =head1 DESCRIPTION
158  
159 This class imports an easy to use Log4Perl handle to the current class. Instead of
160 the not recommended way to use direct functions like DEBUG(), here you can define
161 a category.
162  
163  
164  
165 =head1 REQUIRES
166  
167 L<Log::Log4perl>
168  
169  
170 =head1 HOWTO
171  
172 As the SYNOPSIS examples above, just 'use' the class and add maybe some parameters.
173  
174 What is called a handler here, is just a function, that returns the same as get_logger() from Log4Perl.
175  
176 no parameters - Sets the default handle 'LOG'.
177  
178 one parameter - Sets the handle (function) name.
179  
180 two parameters or pairs - first is the handlename and the second the category name. You can define many pairs.
181  
182  
183  
184 =head1 WHY
185  
186 Please have a look at the normal way to handle with Log4Perl:
187  
188 my $log = Log::Log4perl->get_logger('area1');
189 $log->debug('hello');
190  
191 For classes with some methods, that is not very nice to read and maintain. You will
192 also need that lines in every method again.
193  
194 With Log4Perl::ImportHandle it gets a bit smoother:
195  
196 use Log4Perl::ImportHandle
197  
198 LOG->debug('hello');
199  
200 Once you called it in the header via 'use', you can easily access it with the default function 'LOG'
201 or any other you defined.
202  
203  
204  
205 =head1 AUTHOR
206  
207 Andreas Hernitscheck ahernit(AT)cpan.org
208  
209  
210 =head1 LICENSE
211  
212 You can redistribute it and/or modify it under the conditions of LGPL.
213  
214  
215  
216 =cut
217  
↑ Summary