File Coverage

blib/lib/Log/Log4perl/Shortcuts.pm
Criterion Covered Total %
statement 103 119 86.5
branch 21 40 52.5
condition 2 4 50.0
subroutine 21 21 100.0
pod 9 10 90.0
total 156 194 80.4


line stmt bran cond sub pod time code
1             package Log::Log4perl::Shortcuts ;
2             $Log::Log4perl::Shortcuts::VERSION = '0.024';
3 6     6   873958 use 5.10.0;
  6         47  
4 6     6   33 use Carp;
  6         13  
  6         353  
5 6     6   4989 use Log::Log4perl;
  6         282561  
  6         40  
6 6     6   445 use Log::Log4perl::Level;
  6         13  
  6         30  
7 6     6   2712 use Path::Tiny;
  6         25695  
  6         363  
8 6     6   3215 use Module::Data;
  6         244137  
  6         221  
9 6     6   3292 use File::UserConfig;
  6         102520  
  6         234  
10 6     6   2926 use Data::Dumper qw(Dumper);
  6         28003  
  6         8451  
11              
12             require Exporter;
13             @ISA = Exporter;
14             @EXPORT_OK = qw(logc logt logd logi logw loge logf set_log_config set_log_level get_log_config);
15             %EXPORT_TAGS = ( all => [qw(logc logt logd logi logw loge logf set_log_config set_log_level get_log_config)] );
16             Exporter::export_ok_tags('all');
17              
18             my $package = __PACKAGE__;
19             $package =~ s/::/-/g;
20             my $config_file;
21             my $config_dir = path(File::UserConfig->new(dist => $package)->sharedir, 'log_config');
22              
23             my $default_config_file = path($config_dir, 'default.cfg');
24              
25             if (!$default_config_file->exists) {
26             carp ("Unable to load default Log::Log4perl::Shortcuts configuration file. Aborting");
27             } else {
28             Log::Log4perl->init_once($default_config_file->canonpath);
29             $config_file = $default_config_file->canonpath;
30             }
31              
32             my $log_level = $TRACE;
33              
34             ### Public methods ###
35              
36             sub get_log_config {
37 1     1 0 1353 return $config_file;
38             }
39              
40             sub set_log_config {
41 2     2 1 3359 my $new_config = shift;
42 2   50     12 my $module = shift || '';
43              
44             # must pass in name of a file
45 2 50       7 if (!$new_config) {
46 0         0 logw('No log config file passed. Configuration file unchanged');
47 0         0 return;
48             }
49              
50             # try to get config file from path passed directly in
51 2         7 my $cf_path = path($new_config);
52 2 100       95 if ($cf_path->exists) {
53 1         26 return _init_config($cf_path);
54             }
55              
56             # try to get the config from the module argument or pkg of caller
57 1 50       64 if (!$module) {
58 1         5 ($module) = caller;
59             }
60 1         53 $module =~ s/::/-/g;
61 1         2 my $temp_config_dir;
62 1         2 eval {
63 1         9 my $share_dir = File::UserConfig->new(dist => $module)->sharedir;
64 0 0       0 if ($share_dir) {
65 0         0 $temp_config_dir = path(File::UserConfig->new(dist => $module)->sharedir, 'log_config');
66             }
67             };
68 1 50       1533 if ($temp_config_dir) {
69 0         0 $cf_path = path($temp_config_dir, $new_config);
70 0 0       0 if ($cf_path->exists) {
71 0         0 return _init_config($cf_path);
72             }
73             }
74              
75             # Lastly, check the Log::Log4perl::Shortcuts module for config file
76 1         3 $temp_config_dir = $config_dir;
77 1         5 $cf_path = path($temp_config_dir, $new_config);
78              
79 1 50       47 if (! $cf_path->exists) {
80 1         49 carp ("Configuration file $new_config does not exist. Configuration file unchanged.");
81             } else {
82 0         0 return _init_config($cf_path);
83             }
84             }
85              
86             sub _init_config {
87 1     1   13 my $config = shift;
88 1         8 Log::Log4perl->init($config->canonpath);
89 1         14420 $config_file = $config->canonpath;
90 1         12 return 'success';
91             }
92              
93             sub set_log_level {
94 1     1 1 1584 my $level = ${uc(shift)};
  1         7  
95 1         2 $log_level = $level;
96             }
97              
98             sub logc {
99 1     1 1 2895 my $log = _get_logger(shift);
100 1 50       8 return unless $log->is_trace;
101              
102 1         18 my $msg = sprintf(' ' x 81 . "%s\n", [caller(0)]->[0] . ": " . [caller(0)]->[2]);
103 1         55 $msg .= ' ' . _get_callers() . "\n ";
104              
105 1         5 $log->trace($msg);
106             }
107              
108             sub logt {
109 3     3 1 5968 my $msg = shift;
110              
111 3         11 my $log = _get_logger(shift);;
112 3 100       13 return unless $log->is_trace;
113              
114 2         24 $log->trace($msg);
115             }
116              
117             sub logd {
118 1     1 1 3054 my $msg = shift;
119              
120 1         3 my $log = _get_logger(shift);;
121 1 50       5 return unless $log->is_debug;
122              
123 1         13 $msg = Dumper ($msg);
124 1         86 $log->debug($msg);
125             }
126              
127             sub logi {
128 1     1 1 3022 my $msg = shift;
129              
130 1         3 my $log = _get_logger(shift);;
131 1 50       5 return unless $log->is_info;
132              
133 1         10 $log->info($msg);
134             }
135              
136             sub logw {
137 1     1 1 2654 my $msg = shift;
138              
139 1         4 my $log = _get_logger(shift);;
140 1 50       6 return unless $log->is_warn;
141              
142 1         12 $log->logwarn($msg);
143             }
144              
145             sub loge {
146 1     1 1 5486 my $msg = shift;
147              
148 1         3 my $log = '';
149 1         4 my $options = {};
150 1         2 my $next_arg = shift;
151 1 50       5 if (ref $next_arg) {
152 0         0 my $options = shift;
153             } else {
154 1         4 $log = _get_logger($next_arg);;
155             }
156              
157 1 50       21 return unless $log->is_error;
158              
159 1         14 $msg = sprintf("%-80s %s\n", $msg, [caller(0)]->[0] . ": line " . [caller(0)]->[2]);
160 1 50       57 if ($options->{show_callers}) {
161 0         0 $msg .= ' ' . _get_callers();
162 0         0 chomp $msg;
163 0         0 chomp $msg;
164             }
165 1         5 $log->error_warn($msg);
166             }
167              
168             sub logf {
169 1     1 1 2676 my $msg = shift;
170              
171 1         2 my $log = '';
172 1         3 my $options = {};
173 1         3 my $next_arg = shift;
174 1 50       3 if (ref $next_arg) {
175 0         0 my $options = shift;
176             } else {
177 1         4 $log = _get_logger($next_arg);;
178             }
179              
180 1 50       6 return unless $log->is_fatal;
181              
182 1         13 $msg = sprintf("%-80s %s\n", $msg, [caller(0)]->[0] . ": line " . [caller(0)]->[2]);
183 1 50       85 if ($options->{show_callers}) {
184 0         0 $msg .= ' ' . _get_callers();
185 0         0 chomp $msg;
186 0         0 chomp $msg;
187             }
188              
189 1         8 $log->logdie($msg);
190             }
191             ### Private methods ###
192             sub _get_logger {
193 9   50 9   45 my $category = shift || '';
194 9 50       32 my $logger = Log::Log4perl->get_logger((caller(1))[0] . ($category ? '.' . $category : '') );
195 9         1407 $logger->level($log_level);
196 9         7090 return $logger;
197             }
198              
199              
200              
201             sub _get_callers {
202 1     1   3 my @callers = ();
203 1         3 my $has_sub = 1;
204 1         6 foreach (my $depth = 2; $has_sub; $depth++) {
205 7         16 my $caller = [caller($depth)]->[3] . ': ';
206 7         116 $has_sub = [caller($depth + 1)]->[3];
207 7 100       113 $caller .= $has_sub ? [caller($depth)]->[2] : 'main ' . [caller($depth)]->[2];
208 7         117 push @callers, $caller;
209             }
210 1         5 my $msg = join "\n ", @callers;
211 1         5 return $msg;
212              
213             }
214              
215             1; # Magic true value
216             # ABSTRACT: shortcut functions to make log4perl even easier
217              
218             __END__