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