Simple/Log.pm

filter: Statementstmt 92.5% 37 / 40 Branchbran 50.0% 5 / 10 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 3 totaltotal 65.3% 51 / 78 CC 10 SCAR 17.5
CC
10
Cov
74%
log_init
9.2
logging_args
4.2
get_logging_options
2.0
CRAP 5.7
« Simple/Config.pm ↑ Summary Simple/plugins/check_mem2.pl »
1 #-----------------------------------------------------------------
2 # Monitor::Simple::Log
3 # Author: Martin Senger <martin.senger@gmail.com>
4 # For copyright and disclaimer see below.
5 #
6 # ABSTRACT: See documentation in Monitor::Simple
7 # PODNAME: Monitor::Simple::Log
8 #-----------------------------------------------------------------
9  
10 package Monitor::Simple::Log;
11  
12 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 41 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 41 our $VERSION = '0.2.8'; # VERSION
17  
18 41 my $default_loglevel = 'INFO';
19 41 my $default_logfile = 'smonitor.log';
20 #my $default_logformat = '%d (%r) %p> %F{1}:%L - %m%n';
21 41 my $default_logformat = '%d (%r) %p> %m%n';
22  
23 # will be filled by calling log_init() - they are just a copy of $args
24 # given in log_init()
25 41 my $logging_options = {};
26  
27 # -----------------------------------------------------------------
28 # Return current logging options.
29 # -----------------------------------------------------------------
30 0 ▶sub get_logging_options {
Subroutine get_logging_options: not called CC=1 SCAR=6.9
Pod undocumented
31 0 return $logging_options;
32 }
33  
34 # -----------------------------------------------------------------
35 # Initiate logging. Arguments is a hashref (some values may be
36 # undefined). For example:
37 #
38 # Monitor::Simple::Log->log_init ({ level => $opt_loglevel,
39 # file => $opt_logfile,
40 # layout => $opt_logformat });
41 # -----------------------------------------------------------------
42 8 ▶sub log_init {
Subroutine log_init: called CC=7 SCAR=22.1
Pod undocumented
43 8 my ($self, $args) = @_;
44 8 $logging_options = $args;
45  
46 8 my $logger_conf = {};
47  
48 # log level
49 8 my $opt_loglevel = $args->{level};
50 8 ▶ if ($opt_loglevel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_loglevel) { } 0 8 8
51 0 my $level = Log::Log4perl::Level::to_priority (uc ($opt_loglevel));
52 0 $logger_conf->{level} = $level;
53 } else {
54 8 $logger_conf->{level} = Log::Log4perl::Level::to_priority ($default_loglevel);
55 }
56  
57 # log file
58 8 my $opt_logfile = $args->{file};
59 8 ▶ $opt_logfile ||= $default_logfile;
Condition33%1/3
$opt_logfile ||= $default_logfile
l!l&&r!l&&!r
080
MC/DC0%0/2
$opt_logfile ||= $default_logfile
$opt_logfile $default_logfile
Truth table33%1/3
A ||= B
A= $opt_logfileB= $default_logfile
ABresult
1X1
011
000
60 8 $opt_logfile =~ s{^[><]+}{}; # I had problems when '>' was there; it created binary log file (TBD?)
61 8 ▶ if ($opt_logfile ne 'STDOUT' and $opt_logfile ne 'STDERR' and $opt_logfile !~ m{^>}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_logfile ne "STDOUT" and $opt_logfile ne "STDERR" and not $opt_logfile =~ /^>/) 8 0 8
Condition33%2/6
$opt_logfile ne "STDOUT" and $opt_logfile ne "STDERR" and not $opt_logfile =~ /^>/
!ll&&!rl&&r
008
$opt_logfile ne "STDOUT" and $opt_logfile ne "STDERR"
!ll&&!rl&&r
008
MC/DC0%0/3
$opt_logfile ne "STDOUT" and $opt_logfile ne "STDERR" and not $opt_logfile =~ /^>/
$opt_logfile ne "STDOUT" $opt_logfile ne "STDERR" not $opt_logfile =~ /^>/
Truth table25%1/4
A and B and not(C)
A= $opt_logfile ne "STDOUT"B= $opt_logfile ne "STDERR"C= not $opt_logfile =~ /^>/
ABCresult
0XX0
10X0
1100
1111
62 8 $opt_logfile = ">>$opt_logfile";
63 }
64 8 $logger_conf->{file} = $opt_logfile;
65  
66 # log layout pattern
67 8 my $opt_logformat = $args->{layout};
68 8 ▶ $opt_logformat ||= $default_logformat;
Condition33%1/3
$opt_logformat ||= $default_logformat
l!l&&r!l&&!r
080
MC/DC0%0/2
$opt_logformat ||= $default_logformat
$opt_logformat $default_logformat
Truth table33%1/3
A ||= B
A= $opt_logformatB= $default_logformat
ABresult
1X1
011
000
69 8 $logger_conf->{layout} = $opt_logformat;
70  
71 8 Log::Log4perl->easy_init ($logger_conf);
72  
73 }
74  
75 # -----------------------------------------------------------------
76 # Convert (and return) my $logging_options to the command-line
77 # arguments used for plugins and notifiers.
78 # -----------------------------------------------------------------
79 5 ▶sub logging_args {
Subroutine logging_args: called CC=4 SCAR=14.5
Pod undocumented
80 5 my ($self) = @_;
81 5 my @logging_args = ();
82 5 ▶ push (@logging_args, '-logfile', $logging_options->{file}) if $logging_options->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $logging_options->{"file"} 0 5 5
83 5 ▶ push (@logging_args, '-loglevel', $logging_options->{level}) if $logging_options->{level};
Branch: 1 branch
BranchTrueFalseTotal
if $logging_options->{"level"} 0 5 5
84 5 ▶ push (@logging_args, '-logformat', $logging_options->{layout}) if $logging_options->{layout};
Branch: 1 branch
BranchTrueFalseTotal
if $logging_options->{"layout"} 0 5 5
85 5 return (@logging_args);
86 }
87  
88 41 1;
89  
90 __END__
« Simple/Config.pm ↑ Summary Simple/plugins/check_mem2.pl »