File Coverage

blib/lib/Log/Log4perl/Config/PropertyConfigurator.pm
Criterion Covered Total %
statement 71 71 100.0
branch 19 22 86.3
condition 14 18 77.7
subroutine 6 6 100.0
pod 1 2 50.0
total 111 119 93.2


line stmt bran cond sub pod time code
1             use Log::Log4perl::Config::BaseConfigurator;
2 70     70   26074  
  70         163  
  70         1631  
3             use warnings;
4 70     70   382 use strict;
  70         125  
  70         1443  
5 70     70   298  
  70         127  
  70         5471  
6             our @ISA = qw(Log::Log4perl::Config::BaseConfigurator);
7              
8             our %NOT_A_MULT_VALUE = map { $_ => 1 }
9             qw(conversionpattern);
10              
11             #poor man's export
12             *eval_if_perl = \&Log::Log4perl::Config::eval_if_perl;
13             *compile_if_perl = \&Log::Log4perl::Config::compile_if_perl;
14             *unlog4j = \&Log::Log4perl::Config::unlog4j;
15              
16             use constant _INTERNAL_DEBUG => 0;
17 70     70   398  
  70         155  
  70         48417  
18             our $COMMENT_REGEX = qr/[#;!]/;
19              
20             ################################################
21             ################################################
22             my($self, $newtext) = @_;
23              
24 177     177 1 428 $self->text($newtext) if defined $newtext;
25              
26 177 100       920 my $text = $self->{text};
27              
28 177         355 die "Config parser has nothing to parse" unless defined $text;
29              
30 177 50       450 my $data = {};
31             my %var_subst = ();
32 177         315  
33 177         390 while (@$text) {
34             local $_ = shift @$text;
35 177         540 s/^\s*$COMMENT_REGEX.*//;
36 1354         2139 next unless /\S/;
37 1354         5534
38 1354 100       3470 my @parts = ();
39              
40 1018         1442 while (/(.+?)\\\s*$/) {
41             my $prev = $1;
42 1018         2243 my $next = shift(@$text);
43 51         124 $next =~ s/^ +//g; #leading spaces
44 51         82 $next =~ s/^$COMMENT_REGEX.*//;
45 51         142 $_ = $prev. $next;
46 51         333 chomp;
47 51         123 }
48 51         173  
49             if(my($key, $val) = /(\S+?)\s*=\s*(.*)/) {
50              
51 1018 50       6006 my $key_org = $key;
52              
53 1018         1599 $val =~ s/\s+$//;
54              
55 1018         2536 # Everything could potentially be a variable assignment
56             $var_subst{$key} = $val;
57              
58 1018         2179 # Substitute any variables
59             $val =~ s/\$\{(.*?)\}/
60             Log::Log4perl::Config::var_subst($1, \%var_subst)/gex;
61 1018         1503  
62 14         45 $key = unlog4j($key);
63              
64 1017         2088 my $how_deep = 0;
65             my $ptr = $data;
66 1017         1346 for my $part (split /\.|::/, $key) {
67 1017         1269 push @parts, $part;
68 1017         4700 $ptr->{$part} = {} unless exists $ptr->{$part};
69 2643         3642 $ptr = $ptr->{$part};
70 2643 100       5379 ++$how_deep;
71 2643         3560 }
72 2643         3379  
73             #here's where we deal with turning multiple values like this:
74             # log4j.appender.jabbender.to = him@a.jabber.server
75             # log4j.appender.jabbender.to = her@a.jabber.server
76             #into an arrayref like this:
77             #to => { value =>
78             # ["him\@a.jabber.server", "her\@a.jabber.server"] },
79             #
80             # This only is allowed for properties of appenders
81             # not listed in %NOT_A_MULT_VALUE (see top of file).
82             if (exists $ptr->{value} &&
83             $how_deep > 2 &&
84 1017 100 100     2555 defined $parts[0] && lc($parts[0]) eq "appender" &&
      66        
      100        
      66        
      66        
85             defined $parts[2] && ! exists $NOT_A_MULT_VALUE{lc($parts[2])}
86             ) {
87             if (ref ($ptr->{value}) ne 'ARRAY') {
88             my $temp = $ptr->{value};
89 1 50       4 $ptr->{value} = [];
90 1         3 push (@{$ptr->{value}}, $temp);
91 1         2 }
92 1         2 push (@{$ptr->{value}}, $val);
  1         3  
93             }else{
94 1         2 if(defined $ptr->{value}) {
  1         3  
95             if(! $Log::Log4perl::Logger::NO_STRICT) {
96 1016 100       1783 die "$key_org redefined";
97 3 100       7 }
98 2         23 }
99             $ptr->{value} = $val;
100             }
101 1014         3349 }
102             }
103             $self->{data} = $data;
104             return $data;
105 174         387 }
106 174         726  
107             ################################################
108             ################################################
109             my($self, $path) = @_;
110              
111             $path = unlog4j($path);
112 4     4 0 14  
113             my @p = split /::/, $path;
114 4         9  
115             my $found = 0;
116 4         14 my $r = $self->{data};
117              
118 4         5 while (my $n = shift @p) {
119 4         8 if (exists $r->{$n}) {
120             $r = $r->{$n};
121 4         10 $found = 1;
122 8 100       15 } else {
123 7         9 $found = 0;
124 7         15 }
125             }
126 1         3  
127             if($found and exists $r->{value}) {
128             return $r->{value};
129             } else {
130 4 100 66     15 return undef;
131 3         17 }
132             }
133 1         4  
134             1;
135              
136              
137             =encoding utf8
138              
139             =head1 NAME
140              
141             Log::Log4perl::Config::PropertyConfigurator - reads properties file
142              
143             =head1 SYNOPSIS
144              
145             # This class is used internally by Log::Log4perl
146              
147             use Log::Log4perl::Config::PropertyConfigurator;
148              
149             my $conf = Log::Log4perl::Config::PropertyConfigurator->new();
150             $conf->file("l4p.conf");
151             $conf->parse(); # will die() on error
152              
153             my $value = $conf->value("log4perl.appender.LOGFILE.filename");
154            
155             if(defined $value) {
156             printf("The appender's file name is $value\n");
157             } else {
158             printf("The appender's file name is not defined.\n");
159             }
160              
161             =head1 DESCRIPTION
162              
163             Initializes log4perl from a properties file, stuff like
164              
165             log4j.category.a.b.c.d = WARN, A1
166             log4j.category.a.b = INFO, A1
167              
168             It also understands variable substitution, the following
169             configuration is equivalent to the previous one:
170              
171             settings = WARN, A1
172             log4j.category.a.b.c.d = ${settings}
173             log4j.category.a.b = INFO, A1
174              
175             =head1 SEE ALSO
176              
177             Log::Log4perl::Config
178              
179             Log::Log4perl::Config::BaseConfigurator
180              
181             Log::Log4perl::Config::DOMConfigurator
182              
183             Log::Log4perl::Config::LDAPConfigurator (tbd!)
184              
185             =head1 LICENSE
186              
187             Copyright 2002-2013 by Mike Schilli E<lt>m@perlmeister.comE<gt>
188             and Kevin Goess E<lt>cpan@goess.orgE<gt>.
189              
190             This library is free software; you can redistribute it and/or modify
191             it under the same terms as Perl itself.
192              
193             =head1 AUTHOR
194              
195             Please contribute patches to the project on Github:
196              
197             http://github.com/mschilli/log4perl
198              
199             Send bug reports or requests for enhancements to the authors via our
200              
201             MAILING LIST (questions, bug reports, suggestions/patches):
202             log4perl-devel@lists.sourceforge.net
203              
204             Authors (please contact them via the list above, not directly):
205             Mike Schilli <m@perlmeister.com>,
206             Kevin Goess <cpan@goess.org>
207              
208             Contributors (in alphabetical order):
209             Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton
210             Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony
211             Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy
212             Grundman, Paul Harrington, Alexander Hartmaier David Hull,
213             Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter,
214             Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope,
215             Lars Thegler, David Viner, Mac Yang.
216