File Coverage

lib/Log4Perl/ImportHandle.pm
Criterion Covered Total %
statement 20 20 100.0
branch 4 4 100.0
condition n/a
subroutine 4 4 100.0
pod n/a
total 28 28 100.0


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