File Coverage

blib/lib/Log/Log4perl/AutoInit.pm
Criterion Covered Total %
statement 25 32 78.1
branch 5 14 35.7
condition n/a
subroutine 8 10 80.0
pod 4 4 100.0
total 42 60 70.0


line stmt bran cond sub pod time code
1             package Log::Log4perl::AutoInit;
2              
3 1     1   13430 use 5.008;
  1         3  
4 1     1   3 use strict;
  1         1  
  1         20  
5 1     1   3 use warnings FATAL => 'all';
  1         1  
  1         37  
6 1     1   687 use Log::Log4perl;
  1         43145  
  1         4  
7              
8 1     1   39 use base qw( Exporter );
  1         2  
  1         256  
9             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             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             my $l4p_config;
57              
58             sub set_config {
59 2     2 1 1068 $l4p_config = shift;
60 2 50       8 $l4p_config = shift if $l4p_config eq __PACKAGE__;
61 2         3 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             sub set_default_category {
74 0     0 1 0 $default_category = shift;
75 0 0       0 $default_category = shift if $default_category eq __PACKAGE__;
76 0         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             sub get_logger {
88 2     2 1 7 my $category = shift;
89 2         4 _init();
90 2 50       2276 $category = $default_category unless defined $category;
91 2 50       7 $category = (caller)[0] unless defined $category;
92 2         7 return Log::Log4perl::get_logger($category);
93             }
94              
95             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             sub initialize_now {
106 0     0 1 0 my $re_init = shift;
107 0 0       0 $re_init = shift if $re_init eq __PACKAGE__;
108 0 0       0 $initialized = 0 if $re_init;
109 0         0 return _init();
110             }
111              
112             # private method for for initialization
113              
114             sub _init {
115 2 100   2   5 return if $initialized;
116 1         2 ++$initialized;
117 1         10 return Log::Log4perl->init($l4p_config);
118             }
119              
120             =head1 AUTHOR
121              
122             Binary.com, C<< >>
123              
124             =head1 BUGS
125              
126             Please report any bugs or feature requests to C, or through
127             the web interface at L. 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
147              
148             =item * AnnoCPAN: Annotated CPAN documentation
149              
150             L
151              
152             =item * CPAN Ratings
153              
154             L
155              
156             =item * Search CPAN
157              
158             L
159              
160             =back
161              
162              
163             =head1 ACKNOWLEDGEMENTS
164              
165             =cut
166              
167             1; # End of Log::Log4perl::AutoInit