File Coverage

blib/lib/Log/Log4perl/Config/BaseConfigurator.pm
Criterion Covered Total %
statement 42 45 93.3
branch 20 24 83.3
condition 9 9 100.0
subroutine 8 9 88.8
pod 4 6 66.6
total 83 93 89.2


line stmt bran cond sub pod time code
1              
2             use warnings;
3 70     70   381 use strict;
  70         115  
  70         1783  
4 70     70   294 use constant _INTERNAL_DEBUG => 0;
  70         121  
  70         1173  
5 70     70   265  
  70         115  
  70         44320  
6             *eval_if_perl = \&Log::Log4perl::Config::eval_if_perl;
7             *compile_if_perl = \&Log::Log4perl::Config::compile_if_perl;
8             *leaf_path_to_hash = \&Log::Log4perl::Config::leaf_path_to_hash;
9              
10             ################################################
11             ################################################
12             my($class, %options) = @_;
13              
14 364     364 1 869 my $self = {
15             utf8 => 0,
16 364         945 %options,
17             };
18              
19             bless $self, $class;
20              
21 364         668 $self->file($self->{file}) if exists $self->{file};
22             $self->text($self->{text}) if exists $self->{text};
23 364 50       1044  
24 364 50       1154 return $self;
25             }
26 364         862  
27             ################################################
28             ################################################
29             my($self, $text) = @_;
30              
31             # $text is an array of scalars (lines)
32 197     197 1 413 if(defined $text) {
33             if(ref $text eq "ARRAY") {
34             $self->{text} = $text;
35 197 100       446 } else {
36 177 50       475 $self->{text} = [split "\n", $text];
37 177         367 }
38             }
39 0         0  
40             return $self->{text};
41             }
42              
43 197         435 ################################################
44             ################################################
45             my($self, $filename) = @_;
46              
47             open my $fh, "$filename" or die "Cannot open $filename ($!)";
48              
49 22     22 1 54 if( $self->{ utf8 } ) {
50             binmode $fh, ":utf8";
51 22 100       1043 }
52              
53 20 100       124 $self->file_h_read( $fh );
54 1         8 close $fh;
55             }
56              
57 20         77 ################################################
58 20         321 ################################################
59             my($self, $fh) = @_;
60              
61             # Dennis Gregorovic <dgregor@redhat.com> added this
62             # to protect apps which are tinkering with $/ globally.
63             local $/ = "\n";
64 21     21 0 75  
65             $self->{text} = [<$fh>];
66             }
67              
68 21         109 ################################################
69             ################################################
70 21         4338 die __PACKAGE__ . "::parse() is a virtual method. " .
71             "It must be implemented " .
72             "in a derived class (currently: ", ref(shift), ")";
73             }
74              
75             ################################################
76 0     0 1 0 ################################################
77             my($self, $data, $leaf_paths) = @_;
78            
79             # [
80             # 'category',
81             # 'value',
82             # 'WARN, Logfile'
83             # ],
84 172     172 0 405 # [
85             # 'appender',
86             # 'Logfile',
87             # 'value',
88             # 'Log::Log4perl::Appender::File'
89             # ],
90             # [
91             # 'appender',
92             # 'Logfile',
93             # 'filename',
94             # 'value',
95             # 'test.log'
96             # ],
97             # [
98             # 'appender',
99             # 'Logfile',
100             # 'layout',
101             # 'value',
102             # 'Log::Log4perl::Layout::PatternLayout'
103             # ],
104             # [
105             # 'appender',
106             # 'Logfile',
107             # 'layout',
108             # 'ConversionPattern',
109             # 'value',
110             # '%d %F{1} %L> %m %n'
111             # ]
112              
113             for my $path ( @{ Log::Log4perl::Config::leaf_paths( $data )} ) {
114              
115             print "path=@$path\n" if _INTERNAL_DEBUG;
116              
117             if(0) {
118             } elsif(
119             $path->[0] eq "appender" and
120 172         289 $path->[2] eq "trigger"
  172         403  
121             ) {
122 999         1154 my $ref = leaf_path_to_hash( $path, $data );
123             my $code = compile_if_perl( $$ref );
124 999 100 100     6097  
    100 100        
    100 100        
    100          
125 0 100       0 if(_INTERNAL_DEBUG) {
126             if($code) {
127             print "Code compiled: $$ref\n";
128             } else {
129 3         6 print "Not compiled: $$ref\n";
130 3         8 }
131             }
132 3         5  
133             $$ref = $code if defined $code;
134             } elsif (
135             $path->[0] eq "filter"
136             ) {
137             # do nothing
138             } elsif (
139             $path->[0] eq "appender" and
140 3 50       10 $path->[2] eq "warp_message"
141             ) {
142             # do nothing
143             } elsif (
144             $path->[0] eq "appender" and
145             $path->[3] eq "cspec" or
146             $path->[1] eq "cspec"
147             ) {
148             # could be either
149             # appender appndr layout cspec
150             # or
151             # PatternLayout cspec U value ...
152             #
153             # do nothing
154             } else {
155             my $ref = leaf_path_to_hash( $path, $data );
156              
157             if(_INTERNAL_DEBUG) {
158             print "Calling eval_if_perl on $$ref\n";
159             }
160              
161             $$ref = eval_if_perl( $$ref );
162 922         1751 }
163             }
164 922         1138  
165             return $data;
166             }
167              
168 922         1549 1;
169              
170              
171             =encoding utf8
172 168         916  
173             =head1 NAME
174              
175             Log::Log4perl::Config::BaseConfigurator - Configurator Base Class
176              
177             =head1 SYNOPSIS
178              
179             This is a virtual base class, all configurators should be derived from it.
180              
181             =head1 DESCRIPTION
182              
183             =head2 METHODS
184              
185             =over 4
186              
187             =item C<< new >>
188              
189             Constructor, typically called like
190              
191             my $config_parser = SomeConfigParser->new(
192             file => $file,
193             );
194              
195             my $data = $config_parser->parse();
196              
197             Instead of C<file>, the derived class C<SomeConfigParser> may define any
198             type of configuration input medium (e.g. C<url =E<gt> 'http://foobar'>).
199             It just has to make sure its C<parse()> method will later pull the input
200             data from the medium specified.
201              
202             The base class accepts a filename or a reference to an array
203             of text lines:
204              
205             =over 4
206              
207             =item C<< file >>
208              
209             Specifies a file which the C<parse()> method later parses.
210              
211             =item C<< text >>
212              
213             Specifies a reference to an array of scalars, representing configuration
214             records (typically lines of a file). Also accepts a simple scalar, which it
215             splits at its newlines and transforms it into an array:
216              
217             my $config_parser = MyYAMLParser->new(
218             text => ['foo: bar',
219             'baz: bam',
220             ],
221             );
222              
223             my $data = $config_parser->parse();
224              
225             =back
226              
227             If either C<file> or C<text> parameters have been specified in the
228             constructor call, a later call to the configurator's C<text()> method
229             will return a reference to an array of configuration text lines.
230             This will typically be used by the C<parse()> method to process the
231             input.
232              
233             =item C<< parse >>
234              
235             Virtual method, needs to be defined by the derived class.
236              
237             =back
238              
239             =head2 Parser requirements
240              
241             =over 4
242              
243             =item *
244              
245             If the parser provides variable substitution functionality, it has
246             to implement it.
247              
248             =item *
249              
250             The parser's C<parse()> method returns a reference to a hash of hashes (HoH).
251             The top-most hash contains the
252             top-level keywords (C<category>, C<appender>) as keys, associated
253             with values which are references to more deeply nested hashes.
254              
255             =item *
256              
257             The C<log4perl.> prefix (e.g. as used in the PropertyConfigurator class)
258             is stripped, it's not part in the HoH structure.
259              
260             =item *
261              
262             Each Log4perl config value is indicated by the C<value> key, as in
263              
264             $data->{category}->{Bar}->{Twix}->{value} = "WARN, Logfile"
265              
266             =back
267              
268             =head2 EXAMPLES
269              
270             The following Log::Log4perl configuration:
271              
272             log4perl.category.Bar.Twix = WARN, Screen
273             log4perl.appender.Screen = Log::Log4perl::Appender::File
274             log4perl.appender.Screen.filename = test.log
275             log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
276              
277             needs to be transformed by the parser's C<parse()> method
278             into this data structure:
279              
280             { appender => {
281             Screen => {
282             layout => {
283             value => "Log::Log4perl::Layout::SimpleLayout" },
284             value => "Log::Log4perl::Appender::Screen",
285             },
286             },
287             category => {
288             Bar => {
289             Twix => {
290             value => "WARN, Screen" }
291             } }
292             }
293              
294             For a full-fledged example, check out the sample YAML parser implementation
295             in C<eg/yamlparser>. It uses a simple YAML syntax to specify the Log4perl
296             configuration to illustrate the concept.
297              
298             =head1 SEE ALSO
299              
300             Log::Log4perl::Config::PropertyConfigurator
301              
302             Log::Log4perl::Config::DOMConfigurator
303              
304             Log::Log4perl::Config::LDAPConfigurator (tbd!)
305              
306             =head1 LICENSE
307              
308             Copyright 2002-2013 by Mike Schilli E<lt>m@perlmeister.comE<gt>
309             and Kevin Goess E<lt>cpan@goess.orgE<gt>.
310              
311             This library is free software; you can redistribute it and/or modify
312             it under the same terms as Perl itself.
313              
314             =head1 AUTHOR
315              
316             Please contribute patches to the project on Github:
317              
318             http://github.com/mschilli/log4perl
319              
320             Send bug reports or requests for enhancements to the authors via our
321              
322             MAILING LIST (questions, bug reports, suggestions/patches):
323             log4perl-devel@lists.sourceforge.net
324              
325             Authors (please contact them via the list above, not directly):
326             Mike Schilli <m@perlmeister.com>,
327             Kevin Goess <cpan@goess.org>
328              
329             Contributors (in alphabetical order):
330             Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton
331             Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony
332             Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy
333             Grundman, Paul Harrington, Alexander Hartmaier David Hull,
334             Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter,
335             Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope,
336             Lars Thegler, David Viner, Mac Yang.
337