blib/lib/Beekeeper/Logger.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 147 SCAR 50.5
CC
12
Cov
0%
new
72.0
log
30.0
BEGIN
2.0
CRAP 156.0
1 package Beekeeper::Logger;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.09';
7  
8 use constant LOG_FATAL => 1;
9 use constant LOG_ALERT => 2;
10 use constant LOG_CRIT => 3;
11 use constant LOG_ERROR => 4;
12 use constant LOG_WARN => 5;
13 use constant LOG_NOTICE => 6;
14 use constant LOG_INFO => 7;
15 use constant LOG_DEBUG => 8;
16 use constant LOG_TRACE => 9;
17  
18 use JSON::XS;
19 use Exporter 'import';
20 use Time::HiRes;
21  
22 my $JSON;
23  
24 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 our %EXPORT_TAGS = ('log_levels' => \@EXPORT_OK );
38  
39 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 sub new {
53 my $class = shift;
54  
55 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 unless ($self->{service}) {
67 # Make an educated guess based on worker class
68 my $service = lc $self->{worker_class};
69 $service =~ s/::/-/g;
70 $service =~ s/-worker$//;
71  
72 $self->{service} = $service;
73 }
74  
75 unless ($self->{log_file}) {
76 # Use a single log file per service
77 my $dir = '/var/log';
78 my $user = getpwuid($>);
79 my $file = $self->{service} . '.log';
80 ($user) = ($user =~ m/(\w+)/); # untaint
81  
82 $self->{log_file} = (-d "$dir/$user") ? "$dir/$user/$file" : "$dir/$file";
83 }
84  
85 unless ($self->{foreground}) {
86  
87 my $log_file = $self->{log_file};
88  
89 # If running as root temporarily restore uid and gid to allow opening
90 local $> = $< if ($< == 0);
91 local $) = $( if ($( == 0);
92  
93 if (open(my $fh, '>>', $log_file)) {
94 # Send STDERR and STDOUT to log file
95 open(STDERR, '>&', $fh) or die "Can't redirect STDERR to $log_file: $!";
96 open(STDOUT, '>&', $fh) or die "Can't redirect STDOUT to $log_file: $!";
97 }
98 else {
99 # Probably no permissions to open the log file
100 warn "Can't open log file $log_file: $!";
101 }
102 }
103  
104 bless $self, $class;
105 return $self;
106 }
107  
108 sub log {
109 my ($self, $level, @msg) = @_;
110  
111 my $msg = join(' ', map { defined $_ ? "$_" : 'undef' } @msg );
112 chomp($msg);
113  
114 my $now = Time::HiRes::time;
115 my $ms = int(($now * 1000) % 1000);
116 my @t = reverse((localtime)[0..5]); $t[0] += 1900; $t[1]++;
117 my $tstamp = sprintf("%4d-%02d-%02d %02d:%02d:%02d.%03d", @t, $ms);
118  
119 ## 1. Log to local file
120  
121 print STDERR "[$tstamp][$$][$LOG_LABEL{$level}] $msg\n";
122  
123 ## 2. Log to topic
124  
125 my $bus = $self->{_BUS};
126 return unless $bus && $bus->{is_connected};
127  
128 # JSON-RPC notification
129  
130 $JSON = JSON::XS->new->utf8->allow_blessed->convert_blessed unless defined $JSON;
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 pid => $$,
141 message => $msg,
142 tstamp => $now,
143 }
144 });
145  
146 local $@;
147  
148 eval {
149  
150 my $service = $self->{service};
151 $service =~ tr|.|/|;
152  
153 $bus->publish(
154 topic => "log/$level/$service",
155 payload => \$json,
156 );
157 };
158  
159 if ($@) {
160 my $msg = $@; chomp($msg);
161 print STDERR "[$tstamp][$$][$LOG_LABEL{$level}] $msg\n";
162 }
163 }
164  
165 1;
166  
167 __END__