blib/lib/Log/Log4perl/ConfigByInifile.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 94.5% 69 / 73 CC 8 SCAR 14.2
CC
8
Cov
93%
new
8.1
BEGIN
1.0
BEGIN
1.0
CRAP 4.1
↑ Summary
1 package Log::Log4perl::ConfigByInifile;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # $Revision: 10 $
10 # $Date: 2007-03-16 18:39:40 +0100 (Fri, 16 Mar 2007) $
11 # $Id: ConfigByInifile.pm 10 2007-03-16 17:39:40Z horshack $
12 # $HeadURL: http://svn.rosi13.de/svn/Log-Log4perl-ConfigByInifile/trunk/lib/Log/Log4perl/ConfigByInifile.pm $
13 # $Author: horshack $
14  
15 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 our $VERSION = '0.04';
18  
19 # our @EXPORT = qw();
20 2 our @EXPORT_OK = qw(ConfigByInifile);
21  
22 # our %EXPORT_TAGS = ();
23 }
24  
25 =head1 NAME
26  
27 Log::Log4perl::ConfigByInifile - Get Log::Log4perl config from an ini-File
28  
29 =head1 VERSION
30  
31 0.03 - more Perl Best Practices with http://perlcritic.com/perl/critic.pl
32  
33 =head1 SYNOPSIS
34  
35 use Log::Log4perl::ConfigByInifile;
36 Log::Log4perl::ConfigByInifile->new(
37 { ini_fn => 'acme.ini', }
38 );
39 my $logger = Log::Log4perl->get_logger('main');
40 $logger->debug('Starting...');
41  
42  
43 =head1 DESCRIPTION
44  
45 Initialize Log::Log4perl with an ini-File. You must supply a
46 section for Log4perl like this:
47  
48 [log4perl]
49 log4perl.category = INFO, Logfile, Screen
50  
51 log4perl.appender.Logfile = Log::Log4perl::Appender::File
52 log4perl.appender.Logfile.filename = your_logfile.log
53 log4perl.appender.Logfile.mode = write
54  
55 log4perl.appender.Logfile.layout = Log::Log4perl::Layout::SimpleLayout
56 log4perl.appender.Screen = Log::Log4perl::Appender::Screen
57 log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
58  
59 [myfiles]
60 ...
61  
62 =head1 SUBROUTINES/METHODS
63  
64  
65 =head2 new
66  
67 This is the only method this module has. Calling it
68 initializes Log::Log4perl with the section [log4perl]
69 in your inifile.
70  
71 Usage:
72  
73 Log::Log4perl::ConfigByInifile->new(
74 { ini_fn => 'acme.ini', }
75 );
76 my $logger = Log::Log4perl->get_logger('main');
77 $logger->debug('Starting...');
78  
79 or
80  
81 my $ini_obj = Config::IniFiles->new(
82 -file => 'acme.ini');
83  
84 Log::Log4perl::ConfigByInifile->new(
85 { ini_obj => $ini_obj, }
86 );
87 my $logger = Log::Log4perl->get_logger('main');
88 $logger->debug('Starting...');
89  
90 Returns: Nothing. This routine only initializes Log::Log4perl.
91  
92 Argument: Either ini_file or ini_obj.
93  
94 Throws: Dies in all kinds of errors with a good message (inifile
95 does not exist, not even single argument given etc.)
96  
97 =cut
98  
99 6 ▶sub new {
Subroutine new: called CC=8 SCAR=20.9
Pod documented
100 6 my $class = shift;
101 6 my %defaults = ( section => 'log4perl', );
102 6 my $ini_section = $defaults{section};
103  
104 6 my $args_href = shift;
105 3 %{$args_href} = validate_with(
106 params => $args_href,
107 spec => {
108 ini_file => {
109 type => SCALAR,
110 optional => 1,
111 4 ▶ callbacks => { 'file_must_exist' => sub { -f $_[0] }, },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 },
113 ini_obj => {
114 type => OBJECT,
115 optional => 1,
116 callbacks => {
117 'must_be_config_inifiles' =>
118 2 ▶ sub { ref $_[0] eq 'Config::IniFiles' },
Subroutine __ANON__: called CC=1 SCAR=0.0
119 },
120 },
121 }
122 6 );
123  
124 # either ini_file _or_ ini_obj
125 3 my $anz_file_obj = 0;
126 3 ▶ $anz_file_obj += exists $args_href->{ini_file} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args_href->{'ini_file'} ? : 3 0 3
127 3 ▶ $anz_file_obj += exists $args_href->{ini_obj} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args_href->{'ini_obj'} ? : 1 2 3
128 3 ▶ if ( $anz_file_obj != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anz_file_obj != 1) 1 2 3
129 1 confess 'Submit either ini_file or ini_obj to new';
130 }
131  
132 2 my $ini_obj;
133  
134 2 ▶ if ( $args_href->{ini_obj} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_href->{'ini_obj'}) { } 0 2 2
135 0 $ini_obj = $args_href->{ini_obj};
136 }
137 else {
138 2 $ini_obj = Config::IniFiles->new( -file => $args_href->{ini_file} );
139 }
140  
141 # At this point there is an ini-Object which points
142 # to our ini-file
143  
144 2 ▶ if ( !$ini_obj->SectionExists($ini_section) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ini_obj->SectionExists($ini_section)) 1 1 2
145 1 confess "There must be a section '$ini_section' in the inifile";
146 }
147  
148 1 my $log_conf;
149 1 for my $var ( $ini_obj->Parameters($ini_section) ) {
150 6 ▶ next if !defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 0 6 6
151 6 $log_conf .= sprintf "%s=%s\n", $var,
152 $ini_obj->val( $ini_section, $var );
153 }
154 1 Log::Log4perl::init( \$log_conf );
155  
156 # return something (PBP)
157 1 return 1;
158 }
159  
160 =head1 DEPENDENCIES
161  
162 Depends on these Perl modules:
163  
164 Log::Log4perl;
165 Params::Validate;
166 Config::IniFiles;
167  
168 =head1 AUTHOR
169  
170 Richard Lippmann
171 CPAN ID: HORSHACK
172 horshack@lisa.franken.de
173 http://lena.franken.de
174  
175 =head1 LICENSE AND COPYRIGHT
176  
177 This program is free software; you can redistribute
178 it and/or modify it under the same terms as Perl itself.
179  
180 The full text of the license can be found in the
181 LICENSE file included with this module.
182  
183  
184 =head1 SEE ALSO
185  
186 perl(1).
187  
188 =cut
189  
190 2 1;
↑ Summary