blib/lib/Log/Log4perl/AutoInit.pm

filter: Statementstmt 81.0% 30 / 37 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod 100% 4 / 4 totaltotal 72.3% 47 / 65 CC 8 SCAR 14.2
CC
8
Cov
69%
initialize_now
12.0
set_default_category
6.0
get_logger
3.1
CRAP 4.1
↑ Summary
1 package Log::Log4perl::AutoInit;
2  
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our @EXPORT_OK = qw( init_log4perl get_logger );
10  
11 =head1 NAME
12  
13 Log::Log4perl::AutoInit - Log4Perl with autoinitialization.
14  
15 =cut
16  
17 2 our $VERSION = '1.0.2';
18  
19 =head1 SYNOPSIS
20  
21 use Log::Log4perl::AutoInit qw(get_logger);
22 Log::Log4perl::AutoInit->set_config('path/to/l4p.conf');
23  
24 get_logger->warning('l4p initialized and warning logged');
25  
26 =head1 DESCRIPTION
27  
28 This module provides a simple wrapper around Log::Log4perl for cases where
29 initialization may need to be delayed until a statup process is complete, but
30 where configuration may need to be registered before that point. In essence
31 it provides a way to delay logger initialization until the logger is actually
32 needed or used.
33  
34 A key use for this is for daemons where configuration may be set on loading a
35 Perl module, but where file handles may be subsequently closed. This module
36 allows you to delay initialization until you are actually logging, with an
37 ability to initialize the logger at a specific point and reinitialize if
38 necessary.
39  
40 =head1 EXPORT
41  
42 =head2 get_logger
43  
44  
45 =head1 SUBROUTINES/METHODS
46  
47 =head2 set_config
48  
49 This API sets the configuration for subsequent logger initialization. This
50 only caches the config and does no initialization itself. This is safe to call
51 at any point in the program but if logging is already being done, you must
52 reinitialize (see initialize_now() below).
53  
54 =cut
55  
56 2 my $l4p_config;
57  
58 2 ▶sub set_config {
Subroutine set_config: called CC=2 SCAR=7.1
Pod documented
59 2 $l4p_config = shift;
60 2 ▶ $l4p_config = shift if $l4p_config eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $l4p_config eq __PACKAGE__ 0 2 2
61 2 return;
62 }
63  
64 =head2 set_default_category
65  
66 Sets the default category for all future loggers. If not found the callers'
67 module name is used. To unset pass in undef.
68  
69 =cut
70  
71 my $default_category;
72  
73 0 ▶sub set_default_category {
Subroutine set_default_category: not called CC=2 SCAR=17.9
Pod documented
74 0 $default_category = shift;
75 0 ▶ $default_category = shift if $default_category eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $default_category eq __PACKAGE__ 0 0 0
76 0 return;
77 }
78  
79 =head2 get_logger
80  
81 Initializes, if necessary, and returns a logger with the identical syntax to
82 Log4perl::get_logger(). If you close the file handles out from under the
83 logger, you must reinitialize immediately after. See initialize_now() below.
84  
85 =cut
86  
87 2 ▶sub get_logger {
Subroutine get_logger: called CC=3 SCAR=11.3
Pod documented
88 2 my $category = shift;
89 2 _init();
90 2 ▶ $category = $default_category unless defined $category;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $category 2 0 2
91 2 ▶ $category = (caller)[0] unless defined $category;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $category 2 0 2
92 2 return Log::Log4perl::get_logger($category);
93 }
94  
95 2 my $initialized = 0; # move to state when we can drop 5.8 support
96  
97 =head2 initialize_now(bool $reinitialize);
98  
99 This initializes Log4perl. If $reinitialize is set, it allows Log4perl to be
100 explicitly reinitialized. This can be used to force a reinitialization, for
101 example after file handles have been closed or after a configuration change.
102  
103 =cut
104  
105 0 ▶sub initialize_now {
Subroutine initialize_now: not called CC=3 SCAR=24.8
Pod documented
106 0 my $re_init = shift;
107 0 ▶ $re_init = shift if $re_init eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $re_init eq __PACKAGE__ 0 0 0
108 0 ▶ $initialized = 0 if $re_init;
Branch: 1 branch
BranchTrueFalseTotal
if $re_init 0 0 0
109 0 return _init();
110 }
111  
112 # private method for for initialization
113  
114 2 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
115 2 ▶ return if $initialized;
Branch: 1 branch
BranchTrueFalseTotal
if $initialized 1 1 2
116 1 ++$initialized;
117 1 return Log::Log4perl->init($l4p_config);
118 }
119  
120 =head1 AUTHOR
121  
122 Binary.com, C<< <perl@binary.com> >>
123  
124 =head1 BUGS
125  
126 Please report any bugs or feature requests to C<bug-log-log4perl-autoinit at rt.cpan.org>, or through
127 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-Log4perl-AutoInit>. I will be notified, and then you'll
128 automatically be notified of progress on your bug as I make changes.
129  
130  
131  
132  
133 =head1 SUPPORT
134  
135 You can find documentation for this module with the perldoc command.
136  
137 perldoc Log::Log4perl::AutoInit
138  
139  
140 You can also look for information at:
141  
142 =over 4
143  
144 =item * RT: CPAN's request tracker (report bugs here)
145  
146 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Log4perl-AutoInit>
147  
148 =item * AnnoCPAN: Annotated CPAN documentation
149  
150 L<http://annocpan.org/dist/Log-Log4perl-AutoInit>
151  
152 =item * CPAN Ratings
153  
154 L<http://cpanratings.perl.org/d/Log-Log4perl-AutoInit>
155  
156 =item * Search CPAN
157  
158 L<http://search.cpan.org/dist/Log-Log4perl-AutoInit/>
159  
160 =back
161  
162  
163 =head1 ACKNOWLEDGEMENTS
164  
165 =cut
166  
167 2 1; # End of Log::Log4perl::AutoInit
↑ Summary