File Coverage

blib/lib/Business/Shipping/Logging.pm
Criterion Covered Total %
statement 30 53 56.6
branch 2 8 25.0
condition n/a
subroutine 9 24 37.5
pod 16 16 100.0
total 57 101 56.4


line stmt bran cond sub pod time code
1             package Business::Shipping::Logging;
2              
3             =head1 NAME
4              
5             Business::Shipping::Logging - Log4perl wrapper for easy, non-OO usage.
6              
7             =head1 NOTES
8              
9             The Log4perl category is Package::subroutine::line. This gives a lot of
10             information for debugging. (Technically, category is whatever the fourth
11             return value of caller(1) is.)
12              
13             =head1 METHODS
14              
15             =cut
16              
17 13     13   137 use strict;
  13         28  
  13         440  
18 13     13   70 use warnings;
  13         24  
  13         406  
19 13     13   72 use base qw(Exporter);
  13         23  
  13         1742  
20 13     13   72 use vars qw(@EXPORT $Current_Level);
  13         27  
  13         794  
21 13     13   64 use Carp;
  13         22  
  13         789  
22 13     13   65365 use Log::Log4perl;
  13         1167262  
  13         129  
23 13     13   10959 use Business::Shipping::Config;
  13         66  
  13         2069  
24 13     13   213 use version; our $VERSION = qv('400');
  13         39  
  13         132  
25              
26             Log::Log4perl->wrapper_register(__PACKAGE__);
27             $Current_Level = 'WARN';
28             @EXPORT = qw(
29             fatal is_fatal logdie
30             error is_error
31             warn is_warn logwarn
32             info is_info
33             debug is_debug
34             trace is_trace
35             );
36              
37             init();
38              
39             1;
40              
41             =head2 init
42              
43             Build wrapper on top of Log4perl, increasing caller_depth to one:
44              
45             Business::Shipping::UPS_Offline::RateRequest::debug()
46             |
47             |
48             Business::Shipping::Logging::debug()
49             |
50             |
51             Log::Log4perl->logger->DEBUG()
52              
53             =cut
54              
55             # TODO: Should assume some basic configuration when the file isn't available.
56              
57             sub init {
58 13     13 1 73 my $config_dir = Business::Shipping::Config::config_dir();
59 13 50       77 return carp "Could not find config directory." unless defined $config_dir;
60              
61 13         38 my $file = "$config_dir/log4perl.conf";
62 13 50       701 return croak "Could not get log4perl config file: $file" unless -f $file;
63              
64 13         66 Log::Log4perl::init($file);
65              
66 13         126503 return;
67             }
68              
69             =head1 Exported functions
70              
71             Please see Log4perl for more about these wrapped functions.
72              
73             =head2 logdie
74              
75             =head2 logwarn
76              
77             =head2 fatal
78              
79             =head2 error
80              
81             =head2 warn
82              
83             =head2 info
84              
85             =head2 debug
86              
87             =head2 trace
88              
89             =head2 is_fatal
90              
91             =head2 is_error
92              
93             =head2 is_warn
94              
95             =head2 is_info
96              
97             =head2 is_debug
98              
99             =head2 is_trace
100              
101             =cut
102              
103             # (caller(1))[3] is shorthand for my (undef, undef, undef, $sub) = caller(1);
104             # Using call frame depth of 1
105              
106 0     0 1   sub logdie { Log::Log4perl->get_logger((caller(1))[3])->logdie(@_); }
107 0     0 1   sub logwarn { Log::Log4perl->get_logger((caller(1))[3])->logwarn(@_); }
108 0     0 1   sub fatal { Log::Log4perl->get_logger((caller(1))[3])->fatal(@_); }
109 0     0 1   sub error { Log::Log4perl->get_logger((caller(1))[3])->error(@_); }
110 0     0 1   sub warn { Log::Log4perl->get_logger((caller(1))[3])->warn(@_); }
111 0     0 1   sub info { Log::Log4perl->get_logger((caller(1))[3])->info(@_); }
112 0     0 1   sub debug { Log::Log4perl->get_logger((caller(1))[3])->debug(@_); }
113 0     0 1   sub trace { Log::Log4perl->get_logger((caller(1))[3])->trace(@_); }
114 0     0 1   sub is_fatal { Log::Log4perl->get_logger((caller(1))[3])->is_fatal(); }
115 0     0 1   sub is_error { Log::Log4perl->get_logger((caller(1))[3])->is_error(); }
116 0     0 1   sub is_warn { Log::Log4perl->get_logger((caller(1))[3])->is_warn(); }
117 0     0 1   sub is_info { Log::Log4perl->get_logger((caller(1))[3])->is_info(); }
118 0     0 1   sub is_debug { Log::Log4perl->get_logger((caller(1))[3])->is_debug(); }
119 0     0 1   sub is_trace { Log::Log4perl->get_logger((caller(1))[3])->is_trace(); }
120              
121             =head2 log_level()
122              
123             Does the heavy lifting for Business::Shipping->log_level().
124              
125             =cut
126              
127             sub log_level {
128 0     0 1   my ($class, $log_level) = @_;
129 0 0         return unless $log_level;
130              
131 0           $log_level = lc $log_level;
132 0           my @levels = qw(fatal error warn info debug trace);
133 0 0         if (grep { $_ eq $log_level } @levels) {
  0            
134 0           $Current_Level = uc $log_level;
135             }
136 0           Business::Shipping::Logging::init();
137              
138 0           return $log_level;
139             }
140              
141             __END__