lib/Beekeeper/Logger.pm

filter: Statementstmt 50.5% 48 / 95 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 2 totaltotal 43.0% 62 / 144 CC 15 SCAR 38.2
CC
15
Cov
39%
new
110.0
log
42.0
BEGIN
1.0
CRAP 45.5
« lib/Beekeeper/Service/Router/Worker.pm ↑ Summary lib/Beekeeper/MQTT.pm »
1 package Beekeeper::Logger;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.10';
7  
8 1 ▶use constant LOG_FATAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant LOG_ALERT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant LOG_CRIT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant LOG_ERROR => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant LOG_WARN => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant LOG_NOTICE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant LOG_INFO => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant LOG_DEBUG => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant LOG_TRACE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 my $JSON;
23  
24 1 our @EXPORT_OK = qw(
25 LOG_FATAL
26 LOG_ALERT
27 LOG_CRIT
28 LOG_ERROR
29 LOG_WARN
30 LOG_NOTICE
31 LOG_INFO
32 LOG_DEBUG
33 LOG_TRACE
34 %LOG_LABEL
35 );
36  
37 1 our %EXPORT_TAGS = ('log_levels' => \@EXPORT_OK );
38  
39 1 our %LOG_LABEL = (
40 &LOG_FATAL => 'fatal',
41 &LOG_ALERT => 'alert',
42 &LOG_CRIT => 'critical',
43 &LOG_ERROR => 'error',
44 &LOG_WARN => 'warning',
45 &LOG_NOTICE => 'notice',
46 &LOG_INFO => 'info',
47 &LOG_DEBUG => 'debug',
48 &LOG_TRACE => 'trace',
49 );
50  
51  
52 0 ▶sub new {
Subroutine new: not called CC=10 SCAR=47.0
Pod undocumented
53 0 my $class = shift;
54  
55 0 my $self = {
56 worker_class => undef,
57 foreground => undef,
58 log_file => undef,
59 service => undef,
60 host => undef,
61 pool => undef,
62 _BUS => undef,
63 @_
64 };
65  
66 0 ▶ unless ($self->{service}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"service"}) 0 0 0
67 # Make an educated guess based on worker class
68 0 my $service = lc $self->{worker_class};
69 0 $service =~ s/::/-/g;
70 0 $service =~ s/-worker$//;
71  
72 0 $self->{service} = $service;
73 }
74  
75 0 ▶ unless ($self->{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"log_file"}) 0 0 0
76 # Use a single log file per service
77 0 my $dir = '/var/log';
78 0 my $user = getpwuid($>);
79 0 my $file = $self->{service} . '.log';
80 0 ($user) = ($user =~ m/(\w+)/); # untaint
81  
82 0 ▶ $self->{log_file} = (-d "$dir/$user") ? "$dir/$user/$file" : "$dir/$file";
Branch: 1 branch
BranchTrueFalseTotal
-d "$dir/$user" ? : 0 0 0
83 }
84  
85 0 ▶ unless ($self->{foreground}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"foreground"}) 0 0 0
86  
87 0 my $log_file = $self->{log_file};
88  
89 # If running as root temporarily restore uid and gid to allow opening
90 0 ▶ local $> = $< if ($< == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $< == 0 0 0 0
91 0 ▶ local $) = $( if ($( == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $( == 0 0 0 0
92  
93 0 ▶ if (open(my $fh, '>>', $log_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>>', $log_file) { } 0 0 0
94 # Send STDERR and STDOUT to log file
95 0 ▶ open(STDERR, '>&', $fh) or die "Can't redirect STDERR to $log_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $fh or die "Can't redirect STDERR to ${log_file}: $!" 0 0 0
96 0 ▶ open(STDOUT, '>&', $fh) or die "Can't redirect STDOUT to $log_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $fh or die "Can't redirect STDOUT to ${log_file}: $!" 0 0 0
97 }
98 else {
99 # Probably no permissions to open the log file
100 0 warn "Can't open log file $log_file: $!";
101 }
102 }
103  
104 0 bless $self, $class;
105 0 return $self;
106 }
107  
108 0 ▶sub log {
Subroutine log: not called CC=6 SCAR=37.4
Pod undocumented
109 0 my ($self, $level, @msg) = @_;
110  
111 0 ▶ my $msg = join(' ', map { defined $_ ? "$_" : 'undef' } @msg );
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
112 0 chomp($msg);
113  
114 0 my $now = Time::HiRes::time;
115 0 my $ms = int(($now * 1000) % 1000);
116 0 my @t = reverse((localtime)[0..5]); $t[0] += 1900; $t[1]++;
117 0 my $tstamp = sprintf("%4d-%02d-%02d %02d:%02d:%02d.%03d", @t, $ms);
118  
119 ## 1. Log to local file
120  
121 0 print STDERR "[$tstamp][$$][$LOG_LABEL{$level}] $msg\n";
122  
123 ## 2. Log to topic
124  
125 0 my $bus = $self->{_BUS};
126 0 ▶ return unless $bus && $bus->{is_connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $bus and $bus->{"is_connected"} 0 0 0
Condition0%0/3
$bus and $bus->{"is_connected"}
!ll&&!rl&&r
000
MC/DC0%0/2
$bus and $bus->{"is_connected"}
$bus $bus->{"is_connected"}
Truth table0%0/3
A and B
A= $busB= $bus->{"is_connected"}
ABresult
0X0
100
111
127  
128 # JSON-RPC notification
129  
130 0 ▶ $JSON = JSON::XS->new->utf8->allow_blessed->convert_blessed unless defined $JSON;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $JSON 0 0 0
131  
132 my $json = $JSON->encode({
133 jsonrpc => '2.0',
134 method => $LOG_LABEL{$level},
135 params => {
136 level => $level,
137 service => $self->{service},
138 host => $self->{host},
139 pool => $self->{pool},
140 0 pid => $$,
141 message => $msg,
142 tstamp => $now,
143 }
144 });
145  
146 0 local $@;
147  
148 0 eval {
149  
150 0 my $service = $self->{service};
151 0 $service =~ tr|.|/|;
152  
153 0 $bus->publish(
154 topic => "log/$level/$service",
155 payload => \$json,
156 );
157 };
158  
159 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
160 0 my $msg = $@; chomp($msg);
161 0 print STDERR "[$tstamp][$$][$LOG_LABEL{$level}] $msg\n";
162 }
163 }
164  
165 1 1;
166  
167 __END__
« lib/Beekeeper/Service/Router/Worker.pm ↑ Summary lib/Beekeeper/MQTT.pm »