Log.pm

filter: Statementstmt 98.0% 99 / 101 Branchbran 86.3% 19 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 96.5% 28 / 29 Podpod 85.7% 12 / 14 totaltotal 95.3% 163 / 171 CC 14 SCAR 6.9
CC
14
Cov
96%
lookback
5.0
format_log
3.1
log_always
2.0
CRAP 2.0
« Result.pm ↑ Summary Helper.pm »
1 package Net::SolarWinds::Log;
2
3 =pod
4
5 =head1 NAME
6
7 Net::SolarWinds::Log - Simple file logging module
8
9 =head1 SYNOPSIS
10
11 use Net::SolarWinds::Log;
12
13 my $log=new Net::SolarWinds::Log('/some/log/file.log');
14
15 $log->log_info("This will not be logged");
16 $log->log_error("This will be logged");
17
18 $log->set_loglevel(Net::SolarWinds::Log->LOG_INFO);
19
20 $log->log_info("This will now be logged");
21 $log->log_error("This will still be logged");
22
23
24
25 =head1 DESCRIPTION
26
27 This package provides a very simple somewhat standardized logging interface. The module itself extends Net::SolarWinds::FileRotationBase and inherits all of its features.
28
29 =cut
30
31 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use base qw(Net::SolarWinds::FileRotationBase);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 require Exporter;
39
40 4 our @EXPORT_OK = (qw(LOG_NONE LOG_ERROR LOG_WARN LOG_INFO LOG_DEBUG));
41
42 4 ▶use constant LOOK_BACK_DEPTH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
43
44 =head1 Exports
45
46 The following constants can be exported using the standard exporter syntax
47
48 use Net::SolarWinds::Log (qw(LOG_NONE LOG_ERROR LOG_WARN LOG_INFO LOG_DEBUG));
49
50 =cut
51
52 4 ▶use constant LOG_NONE => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 ▶use constant LOG_ALWAYS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 4 ▶use constant LOG_ERROR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use constant LOG_WARN => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use constant LOG_INFO => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use constant LOG_DEBUG => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
58
59 =head2 Default Log level
60
61 The default log level is LOG_ERROR or 1. In the default state only errors are logged.
62
63 =cut
64
65 4 ▶use constant DEFAULT_LOG_LEVEL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
66
67 =head1 OO Methods
68
69 =over 3
70
71 =item * Object constructor
72
73 The object constructor takes key=>'value' argument pairs example:
74
75 my $log=new Net::SolarWinds::Log(
76 filename=>'/full/path/to/file.log',
77 loglevel=>4,
78
79 # optional, if not set the system hostname will be used
80 hostname=>'somehost'
81
82 # ignored when filename is set
83 basefilename=>'myapp',
84 folder=>'/var/myappfolder',
85
86 );
87
88 When the constructor is called with a single argument it is assumed to be the fully quallified name of the log file to manage and rotate.
89
90 my $log=new Net::SolarWinds::Log('/some/log/file.log');
91
92 =cut
93
94 6 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
95 6 my ( $class, @args ) = @_;
96
97 6 ▶ unshift @args, 'filename' if ( $#args == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $#args == 0 1 5 6
98
99 6 my $self = $class->SUPER::new(
100 loglevel => $class->DEFAULT_LOG_LEVEL,
101 basefilename => 'changeme',
102 hostname => hostname,
103 @args
104 );
105 }
106
107 =item * my $hash=$self->lookback(stack_level);
108
109 This method returns a hash that provides information about who called this function relative to the stack_level argument. The class default value is 4.
110
111 Example result
112
113 {
114 # the fully qualified package that this method ran under
115 package=>'main',
116
117 # the package and subrouteen this was called under
118 sub=>'main::some_method',
119
120 # the source file ( may be eval or undef )
121 filename=>'/path/to/my/Script',
122
123 # the line in wich the function was called
124 # if the internals are unsure the value is undef
125 line=>11
126 }
127
128 =cut
129
130 60 ▶sub lookback {
Subroutine lookback: called CC=5 SCAR=16.1
Pod documented
131 60 my ( $self, $level ) = @_;
132
133 60 my $hash = {};
134 60 @{$hash}{qw(package filename line sub)} = caller($level);
135
136 # Look up the stack until we find something that explains who and what called us
137 60 ▶ LOOK_BACK_LOOP: while ( defined( $hash->{sub} ) and $hash->{sub} =~ /eval/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{"sub"} and $hash->{"sub"} =~ /eval/) 5 59 64
Condition100%3/3
defined $hash->{"sub"} and $hash->{"sub"} =~ /eval/
!ll&&!rl&&r
1585
MC/DC100%2/2
defined $hash->{"sub"} and $hash->{"sub"} =~ /eval/
defined $hash->{"sub"} $hash->{"sub"} =~ /eval/
Truth table100%3/3
A and B
A= defined $hash->{"sub"}B= $hash->{"sub"} =~ /eval/
ABresult
0X0
100
111
138
139 5 my $copy = {%$hash};
140 5 @{$hash}{qw(package filename line sub)} = caller( ++$level );
141
142 # give up when we have a dead package name
143 5 ▶ unless ( defined( $hash->{package} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"package"}) 1 4 5
144
145 1 $hash = $copy;
146 1 $hash->{eval} = 1;
147
148 1 last LOOK_BACK_LOOP;
149
150 }
151
152 }
153
154 # if we don't know where we were called from, we can assume main.
155 1 @{$hash}{qw(sub filename package line)} = ( 'main::', $0, 'main', 'undef' )
156 60 ▶ unless defined( $hash->{package} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash->{"package"} 1 59 60
157
158 60 return $hash;
159 }
160
161 =item * my $string=$log->format_log('LEVEL=ERROR|WARN|INFO|DEBUG',"some log");
162
163 Formats your log entry as:
164
165 HOSTNAME PID TIMESTAMP LEVEL STACK_TRACE DATA \n
166
167 Special notes: any undef value will be converted to a string value of 'undef'.
168
169 =cut
170
171 59 ▶sub format_log {
Subroutine format_log: called CC=3 SCAR=11.3
Pod documented
172 59 my ( $self, $level, @info ) = @_;
173
174 59 foreach my $string (@info) {
175
176 59 ▶ unless ( defined($string) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 59 59
177 0 $string = 'undef';
178 }
179
180 }
181
182 59 my $lb = $self->lookback( $self->LOOK_BACK_DEPTH );
183
184 59 my $string = join ' ', $self->{hostname}, $$, scalar(localtime), $level, $lb->{sub}, @info;
185 59 return $string . "\n";
186 }
187
188 =item * $log->log_info("message");
189
190 Logs to a file if the log level is LOG_INFO or greater.
191
192 =cut
193
194 5 ▶sub log_info {
Subroutine log_info: called CC=2 SCAR=6.9
Pod documented
195 5 my ( $self, @args ) = @_;
196
197 5 ▶ return unless $self->{loglevel} >= $self->LOG_INFO;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"loglevel"} >= $self->LOG_INFO 3 2 5
198
199 2 $self->write_to_log( 'INFO', @args );
200 }
201
202 =item * $log->log_error("message");
203
204 Logs to a file if the log level is LOG_error or greater.
205
206 =cut
207
208 10 ▶sub log_error {
Subroutine log_error: called CC=2 SCAR=6.9
Pod documented
209 10 my ( $self, @args ) = @_;
210
211 10 ▶ return unless $self->{loglevel} >= $self->LOG_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"loglevel"} >= $self->LOG_ERROR 2 8 10
212
213 8 $self->write_to_log( 'ERROR', @args );
214 }
215
216 =item * $log->log_die("Some message");
217
218 Logs the message then dies.
219
220 =cut
221
222 1 ▶sub log_die {
Subroutine log_die: called CC=2 SCAR=7.0
Pod documented
223 1 my ($self,@args)=@_;
224 1 my $string=$self->format_log('DIE',@args);
225
226 1 ▶ die $string unless $self->{loglevel} >= $self->LOG_ALWAYS;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"loglevel"} >= $self->LOG_ALWAYS 0 1 1
227
228 1 $self->write_to_log( 'DIE', @args );
229 1 die $string;
230 }
231
232 =item * $log->log_warn("message");
233
234 Logs to a file if the log level is LOG_WARN or greater.
235
236 =cut
237
238 5 ▶sub log_warn {
Subroutine log_warn: called CC=2 SCAR=6.9
Pod documented
239 5 my ( $self, @args ) = @_;
240
241 5 ▶ return unless $self->{loglevel} >= $self->LOG_WARN;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"loglevel"} >= $self->LOG_WARN 2 3 5
242
243 3 $self->write_to_log( 'WARN', @args );
244 }
245
246 =item * $log->log_always("message");
247
248 Logs to a file if the log level is LOG_ALWAYS or greater.
249
250 =cut
251
252 5 ▶sub log_always {
Subroutine log_always: called CC=2 SCAR=7.1
Pod documented
253 5 my ( $self, @args ) = @_;
254
255 5 ▶ return unless $self->{loglevel} >= $self->LOG_ALWAYS;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"loglevel"} >= $self->LOG_ALWAYS 0 5 5
256
257 5 $self->write_to_log( 'ALWAYS', @args );
258 }
259
260 =item * $log->log_debug("message");
261
262 Logs to a file if the log level is LOG_DEBUG or greater.
263
264 =cut
265
266 5 ▶sub log_debug {
Subroutine log_debug: called CC=2 SCAR=6.9
Pod documented
267 5 my ( $self, @args ) = @_;
268
269 5 ▶ return unless $self->{loglevel} >= $self->LOG_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"loglevel"} >= $self->LOG_DEBUG 4 1 5
270
271 1 $self->write_to_log( 'DEBUG', @args );
272 }
273
274 =item * $log->write_to_log('LEVEL=ERROR|WARN|INFO|DEBUG','message');
275
276 Writes 'message' to the log file with formatting representing 2 levels aboive itself in the stack.
277
278 =cut
279
280 57 ▶sub write_to_log {
Subroutine write_to_log: called CC=1 SCAR=0.0
Pod documented
281 57 my ( $self, @info ) = @_;
282
283 57 my $string = $self->format_log(@info);
284
285 57 $self->write_to_file($string);
286 }
287
288 =item * my $loglevel=$log->get_loglevel;
289
290 Returns the current runtime loglevel.
291
292 =cut
293
294 59 ▶sub get_loglevel {
Subroutine get_loglevel: called CC=1 SCAR=0.0
Pod documented
295 59 $_[0]->{loglevel};
296 }
297
298 =item * $log->set_loglevel(level);
299
300 Used to set the current loglevel to the level.
301
302 =cut
303
304 17 ▶sub set_loglevel {
Subroutine set_loglevel: called CC=1 SCAR=0.0
Pod documented
305 17 my ( $self, $level ) = @_;
306 17 $self->{loglevel} = $level;
307 }
308
309 # overload default functions, but make them work.. sort of
310 1 ▶sub get_log { $_[0] }
Subroutine get_log: called CC=1 SCAR=0.0
Pod documented
311 0 ▶sub set_log { croak "Cannot set log object within itself!" }
Subroutine set_log: not called CC=1 SCAR=6.9
Pod undocumented
312
313 =back
314
315 =head1 Author
316
317 Michael Shipper
318
319 =cut
320
321 4 1;
« Result.pm ↑ Summary Helper.pm »