blib/lib/Ereshkigal/LogDrek.pm

filter: Statementstmt 44.7% 17 / 38 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 40.3% 23 / 57 CC 7 SCAR 35.0
CC
7
Cov
34%
log_drek
56.0
BEGIN
1.0
BEGIN
1.0
CRAP 33.1
« blib/lib/Ereshkigal/Client.pm ↑ Summary blib/lib/Ereshkigal/App/Command.pm »
1 package Ereshkigal::LogDrek;
2  
3 18 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Sys::Syslog qw( closelog openlog syslog );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =head1 NAME
12  
13 Ereshkigal::LogDrek - Exportable syslog helper shared by the Ereshkigal bins and modules.
14  
15 =head1 VERSION
16  
17 Version 0.0.1
18  
19 =cut
20  
21 18 our $VERSION = '0.0.1';
22  
23 18 our @EXPORT_OK = qw( log_drek );
24  
25 =head1 SYNOPSIS
26  
27 use Ereshkigal::LogDrek qw( log_drek );
28  
29 log_drek( 'info', 'started' );
30 log_drek( 'err', 'something broke', $tracking_int );
31 log_drek( 'info', 'banned 1.2.3.4', undef, 'kur-sshd' );
32  
33 =head1 DESCRIPTION
34  
35 This holds the C<log_drek> sub used by both C<ereshkigal> and C<kur> as well
36 as the various Ereshkigal modules for logging everything they do. It is a
37 plain function usable without new or the like being called, exported on
38 request, so everything can share one implementation instead of each carrying
39 their own copy.
40  
41 =head1 EXPORTS
42  
43 Nothing is exported by default. L</log_drek> is available via C<@EXPORT_OK>.
44  
45 =head1 FUNCTIONS
46  
47 =head2 log_drek
48  
49 Writes a message to syslog.
50  
51 log_drek( $level, $message, $tracking_int, $ident );
52  
53 C<$level> defaults to 'info' when undef or not a valid syslog level. When
54 C<$tracking_int> is defined it is prepended to the message as
55 C<< $tracking_int . ' : ' . $message >>. C<$ident> is the syslog ident to log
56 under and defaults to 'ereshkigal' when undef. Kur instances should pass
57 C<'kur-' . $name> so log lines are attributable per instance.
58  
59 Logging failures are swallowed rather than propagated, with a best effort
60 print to STDERR instead, so logging can never take down the caller.
61  
62 =cut
63  
64 0 ▶sub log_drek {
Subroutine log_drek: not called CC=7 SCAR=40.3
Pod documented
65 0 my ( $level, $message, $tracking_int, $ident ) = @_;
66  
67 0 ▶ if ( !defined($level) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $level) 0 0 0
68 0 $level = 'info';
69 }
70  
71 # syslog dies on priorities it does not recognize, so anything not a
72 # valid level falls back to info
73 0 my %valid_syslog_levels = (
74 'emerg' => 1,
75 'alert' => 1,
76 'crit' => 1,
77 'err' => 1,
78 'warning' => 1,
79 'notice' => 1,
80 'info' => 1,
81 'debug' => 1,
82 );
83 0 ▶ if ( !defined( $valid_syslog_levels{$level} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $valid_syslog_levels{$level}) 0 0 0
84 0 $level = 'info';
85 }
86  
87 0 ▶ if ( !defined($message) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $message) 0 0 0
88 0 $message = '';
89 }
90 0 chomp($message);
91  
92 0 ▶ if ( defined($tracking_int) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tracking_int) 0 0 0
93 0 $message = $tracking_int . ' : ' . $message;
94 }
95  
96 0 ▶ if ( !defined($ident) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ident) 0 0 0
97 0 $ident = 'ereshkigal';
98 }
99  
100 # logging must never take down the caller, so failures talking to
101 # syslog are swallowed, with a best effort print to STDERR instead
102 0 eval {
103 0 openlog( $ident, 'cons,pid', 'daemon' );
104 0 syslog( $level, '%s', $message );
105 0 closelog();
106 };
107 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
108 0 eval { print STDERR $ident . ' ' . $level . ': ' . $message . "\n"; };
109 }
110  
111 0 return;
112 } ## end sub log_drek
113  
114 18 1;
« blib/lib/Ereshkigal/Client.pm ↑ Summary blib/lib/Ereshkigal/App/Command.pm »