File Coverage

blib/lib/Log/Log4perl/AutoInit.pm
Criterion Covered Total %
statement 25 31 80.6
branch 5 14 35.7
condition n/a
subroutine 8 10 80.0
pod 4 4 100.0
total 42 59 71.1


line stmt bran cond sub pod time code
1             package Log::Log4perl::AutoInit;
2              
3 2     2   54106 use 5.008;
  2         8  
  2         99  
4 2     2   12 use strict;
  2         3  
  2         86  
5 2     2   11 use warnings FATAL => 'all';
  2         9  
  2         95  
6 2     2   2828 use Log::Log4perl;
  2         164976  
  2         17  
7              
8 2     2   113 use base qw( Exporter );
  2         5  
  2         767  
9             our @EXPORT_OK = qw( init_log4perl get_logger );
10              
11              
12             =head1 NAME
13              
14             Log::Log4perl::AutoInit - Log4Perl with autoinitialization.
15              
16             =head1 VERSION
17              
18             Version 1.0.0
19              
20             =cut
21              
22             our $VERSION = '1.0.0';
23              
24              
25             =head1 SYNOPSIS
26              
27             use Log::Log4perl::AutoInit qw(get_logger);
28             Log::Log4perl::AutoInit->set_config('path/to/l4p.conf');
29              
30             get_logger->warning('l4p initialized and warning logged');
31              
32             =head1 DESCRIPTION
33              
34             This module provides a simple wrapper around Log::Log4perl for cases where
35             initialization may need to be delayed until a statup process is complete, but
36             where configuration may need to be registered before that point. In essence
37             it provides a way to delay logger initialization until the logger is actually
38             needed or used.
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.
51              
52             =cut
53              
54             my $l4p_config;
55              
56             sub set_config {
57 2     2 1 1214 $l4p_config = shift;
58 2 50       11 $l4p_config = shift if $l4p_config eq __PACKAGE__;
59             }
60              
61             =head2 set_default_category
62              
63             Sets the default category for all future loggers. If not found the callers'
64             module name is used. To unset pass in undef.
65              
66             =cut
67              
68             my $default_category;
69              
70             sub set_default_category {
71 0     0 1 0 $default_category = shift;
72 0 0       0 $default_category = shift if $default_category eq __PACKAGE__;
73             }
74              
75             =head2 get_logger
76              
77             Initializes, if necessary, and returns a logger with the identical syntax to
78             Log4perl::get_logger()
79              
80             =cut
81              
82             sub get_logger {
83 2     2 1 12 _init();
84 2         28977 my $category = $_[0];
85 2 50       13 $category = $default_category unless defined $category;
86 2 50       10 $category = (caller)[0] unless defined $category;
87 2         10 return Log::Log4perl::get_logger($category);
88             }
89              
90             my $initialized = 0; # move to state when we can drop 5.8 support
91              
92             =head2 initialize_now(bool $reinitialize);
93              
94             This initializes Log4perl. If $reinitialize is set, it allows Log4perl to be
95             explicitly reinitialized.
96              
97             =cut
98              
99             sub initialize_now {
100 0     0 1 0 my $re_init = shift;
101 0 0       0 $re_init = shift if $re_init eq __PACKAGE__;
102 0 0       0 $initialized = 0 if $re_init;
103 0         0 _init();
104             }
105              
106             # private method for for initialization
107              
108             sub _init {
109 2 100   2   8 return if $initialized;
110 1         2 ++$initialized;
111 1         13 Log::Log4perl->init($l4p_config);
112             }
113              
114              
115             =head1 AUTHOR
116              
117             Binary.com, C<< >>
118              
119             =head1 BUGS
120              
121             Please report any bugs or feature requests to C, or through
122             the web interface at L. I will be notified, and then you'll
123             automatically be notified of progress on your bug as I make changes.
124              
125              
126              
127              
128             =head1 SUPPORT
129              
130             You can find documentation for this module with the perldoc command.
131              
132             perldoc Log::Log4perl::AutoInit
133              
134              
135             You can also look for information at:
136              
137             =over 4
138              
139             =item * RT: CPAN's request tracker (report bugs here)
140              
141             L
142              
143             =item * AnnoCPAN: Annotated CPAN documentation
144              
145             L
146              
147             =item * CPAN Ratings
148              
149             L
150              
151             =item * Search CPAN
152              
153             L
154              
155             =back
156              
157              
158             =head1 ACKNOWLEDGEMENTS
159              
160              
161             =head1 LICENSE
162              
163             This program is free software; you can redistribute it and/or modify it
164             under the terms of the the Artistic License (2.0). You may obtain a
165             copy of the full license at:
166              
167             L
168              
169             Any use, modification, and distribution of the Standard or Modified
170             Versions is governed by this Artistic License. By using, modifying or
171             distributing the Package, you accept this license. Do not use, modify,
172             or distribute the Package, if you do not accept this license.
173              
174             If your Modified Version has been derived from a Modified Version made
175             by someone other than you, you are nevertheless required to ensure that
176             your Modified Version complies with the requirements of this license.
177              
178             This license does not grant you the right to use any trademark, service
179             mark, tradename, or logo of the Copyright Holder.
180              
181             This license includes the non-exclusive, worldwide, free-of-charge
182             patent license to make, have made, use, offer to sell, sell, import and
183             otherwise transfer the Package with respect to any patent claims
184             licensable by the Copyright Holder that are necessarily infringed by the
185             Package. If you institute patent litigation (including a cross-claim or
186             counterclaim) against any party alleging that the Package constitutes
187             direct or contributory patent infringement, then this Artistic License
188             to you shall terminate on the date that such litigation is filed.
189              
190             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
191             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
192             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
193             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
194             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
195             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
196             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
197             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
198              
199              
200             =cut
201              
202             1; # End of Log::Log4perl::AutoInit