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   31877  
  70         205  
  70         1996  
3             use warnings;
4 70     70   473 use strict;
  70         158  
  70         1683  
5 70     70   382  
  70         169  
  70         6848  
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   478  
  70         178  
  70         58698  
18             our $COMMENT_REGEX = qr/[#;!]/;
19              
20             ################################################
21             ################################################
22             my($self, $newtext) = @_;
23              
24 177     177 1 479 $self->text($newtext) if defined $newtext;
25              
26 177 100       1041 my $text = $self->{text};
27              
28 177         382 die "Config parser has nothing to parse" unless defined $text;
29              
30 177 50       507 my $data = {};
31             my %var_subst = ();
32 177         364  
33 177         446 while (@$text) {
34             local $_ = shift @$text;
35 177         713 s/^\s*$COMMENT_REGEX.*//;
36 1354         2500 next unless /\S/;
37 1354         6634
38 1354 100       4158 my @parts = ();
39              
40 1018         1748 while (/(.+?)\\\s*$/) {
41             my $prev = $1;
42 1018         2702 my $next = shift(@$text);
43 51         167 $next =~ s/^ +//g; #leading spaces
44 51         100 $next =~ s/^$COMMENT_REGEX.*//;
45 51         184 $_ = $prev. $next;
46 51         381 chomp;
47 51         144 }
48 51         220  
49             if(my($key, $val) = /(\S+?)\s*=\s*(.*)/) {
50              
51 1018 50       7247 my $key_org = $key;
52              
53 1018         1869 $val =~ s/\s+$//;
54              
55 1018         2235 # Everything could potentially be a variable assignment
56             $var_subst{$key} = $val;
57              
58 1018         2549 # Substitute any variables
59             $val =~ s/\$\{(.*?)\}/
60             Log::Log4perl::Config::var_subst($1, \%var_subst)/gex;
61 1018         1799  
62 14         43 $key = unlog4j($key);
63              
64 1017         2435 my $how_deep = 0;
65             my $ptr = $data;
66 1017         1649 for my $part (split /\.|::/, $key) {
67 1017         1480 push @parts, $part;
68 1017         5329 $ptr->{$part} = {} unless exists $ptr->{$part};
69 2643         4432 $ptr = $ptr->{$part};
70 2643 100       6523 ++$how_deep;
71 2643         4353 }
72 2643         4124  
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     3101 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         3 }
92 1         2 push (@{$ptr->{value}}, $val);
  1         3  
93             }else{
94 1         2 if(defined $ptr->{value}) {
  1         4  
95             if(! $Log::Log4perl::Logger::NO_STRICT) {
96 1016 100       2133 die "$key_org redefined";
97 3 100       8 }
98 2         31 }
99             $ptr->{value} = $val;
100             }
101 1014         3953 }
102             }
103             $self->{data} = $data;
104             return $data;
105 174         489 }
106 174         1178  
107             ################################################
108             ################################################
109             my($self, $path) = @_;
110              
111             $path = unlog4j($path);
112 4     4 0 25  
113             my @p = split /::/, $path;
114 4         12  
115             my $found = 0;
116 4         21 my $r = $self->{data};
117              
118 4         13 while (my $n = shift @p) {
119 4         9 if (exists $r->{$n}) {
120             $r = $r->{$n};
121 4         14 $found = 1;
122 8 100       21 } else {
123 7         14 $found = 0;
124 7         15 }
125             }
126 1         4  
127             if($found and exists $r->{value}) {
128             return $r->{value};
129             } else {
130 4 100 66     18 return undef;
131 3         16 }
132             }
133 1         5  
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