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   23166  
  70         153  
  70         1521  
3             use warnings;
4 70     70   358 use strict;
  70         126  
  70         1314  
5 70     70   290  
  70         120  
  70         5159  
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   374  
  70         146  
  70         44183  
18             our $COMMENT_REGEX = qr/[#;!]/;
19              
20             ################################################
21             ################################################
22             my($self, $newtext) = @_;
23              
24 177     177 1 372 $self->text($newtext) if defined $newtext;
25              
26 177 100       806 my $text = $self->{text};
27              
28 177         307 die "Config parser has nothing to parse" unless defined $text;
29              
30 177 50       433 my $data = {};
31             my %var_subst = ();
32 177         295  
33 177         351 while (@$text) {
34             local $_ = shift @$text;
35 177         501 s/^\s*$COMMENT_REGEX.*//;
36 1354         1984 next unless /\S/;
37 1354         5207
38 1354 100       3269 my @parts = ();
39              
40 1018         1316 while (/(.+?)\\\s*$/) {
41             my $prev = $1;
42 1018         2140 my $next = shift(@$text);
43 51         114 $next =~ s/^ +//g; #leading spaces
44 51         71 $next =~ s/^$COMMENT_REGEX.*//;
45 51         129 $_ = $prev. $next;
46 51         289 chomp;
47 51         116 }
48 51         153  
49             if(my($key, $val) = /(\S+?)\s*=\s*(.*)/) {
50              
51 1018 50       5633 my $key_org = $key;
52              
53 1018         1451 $val =~ s/\s+$//;
54              
55 1018         1722 # Everything could potentially be a variable assignment
56             $var_subst{$key} = $val;
57              
58 1018         1884 # Substitute any variables
59             $val =~ s/\$\{(.*?)\}/
60             Log::Log4perl::Config::var_subst($1, \%var_subst)/gex;
61 1018         1436  
62 14         27 $key = unlog4j($key);
63              
64 1017         1906 my $how_deep = 0;
65             my $ptr = $data;
66 1017         1341 for my $part (split /\.|::/, $key) {
67 1017         1149 push @parts, $part;
68 1017         3941 $ptr->{$part} = {} unless exists $ptr->{$part};
69 2643         3512 $ptr = $ptr->{$part};
70 2643 100       5002 ++$how_deep;
71 2643         3286 }
72 2643         3254  
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     2431 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         2 push (@{$ptr->{value}}, $temp);
91 1         1 }
92 1         2 push (@{$ptr->{value}}, $val);
  1         2  
93             }else{
94 1         2 if(defined $ptr->{value}) {
  1         3  
95             if(! $Log::Log4perl::Logger::NO_STRICT) {
96 1016 100       1753 die "$key_org redefined";
97 3 100       9 }
98 2         25 }
99             $ptr->{value} = $val;
100             }
101 1014         3041 }
102             }
103             $self->{data} = $data;
104             return $data;
105 174         325 }
106 174         607  
107             ################################################
108             ################################################
109             my($self, $path) = @_;
110              
111             $path = unlog4j($path);
112 4     4 0 14  
113             my @p = split /::/, $path;
114 4         10  
115             my $found = 0;
116 4         12 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       16 } else {
123 7         7 $found = 0;
124 7         14 }
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         16 }
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