| 1 | package Daemonise::Plugin::Syslog; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use Mouse::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | # ABSTRACT: Daemonise plugin adding syslog functionality | ||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use Sys::Syslog qw(setlogsock :standard :macros); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | |||||||||||||||
| 10 | has 'syslog_host' => ( | ||||||||||||||
| 11 | is => 'rw', | ||||||||||||||
| 12 | isa => 'Str', | ||||||||||||||
| 13 | lazy => 1, | ||||||||||||||
| 14 | 0 | ▶ | default => sub { '127.0.0.1' }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 15 | 2 | ); | |||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | has 'syslog_port' => ( | ||||||||||||||
| 19 | is => 'rw', | ||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||
| 21 | lazy => 1, | ||||||||||||||
| 22 | 0 | ▶ | default => sub { '514' }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 23 | 2 | ); | |||||||||||||
| 24 | |||||||||||||||
| 25 | |||||||||||||||
| 26 | has 'syslog_type' => ( | ||||||||||||||
| 27 | is => 'rw', | ||||||||||||||
| 28 | isa => 'Str', | ||||||||||||||
| 29 | lazy => 1, | ||||||||||||||
| 30 | 0 | ▶ | default => sub { 'tcp' }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 31 | 2 | ); | |||||||||||||
| 32 | |||||||||||||||
| 33 | |||||||||||||||
| 34 | has 'syslog_log' => ( | ||||||||||||||
| 35 | is => 'rw', | ||||||||||||||
| 36 | isa => 'Bool', | ||||||||||||||
| 37 | lazy => 1, | ||||||||||||||
| 38 | 0 | ▶ | default => sub { 1 }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 39 | 2 | ); | |||||||||||||
| 40 | |||||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | ▶ | after 'configure' => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||
| 43 | 0 | my ($self, $reconfig) = @_; | |||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | ▶ | $self->log("configuring Syslog plugin") if $self->debug; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | my $syslog_config; | |||||||||||||
| 48 | 0 | ▶ | if (ref($self->config->{syslog}) eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 0 | $syslog_config = 1; | |||||||||||||
| 50 | 0 | foreach my $conf_key ('host', 'port', 'type') { | |||||||||||||
| 51 | 0 | my $attr = 'syslog_' . $conf_key; | |||||||||||||
| 52 | $self->$attr($self->config->{syslog}->{$conf_key}) | ||||||||||||||
| 53 | 0 | ▶ | if defined $self->config->{syslog}->{$conf_key}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | } | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | closelog(); | |||||||||||||
| 58 | 0 | ▶ | setlogsock({ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | type => $self->syslog_type, | ||||||||||||||
| 60 | host => $self->syslog_host, | ||||||||||||||
| 61 | port => $self->syslog_port | ||||||||||||||
| 62 | }) if $syslog_config; | ||||||||||||||
| 63 | 0 | openlog($self->name, 'pid,nonul', LOG_USER); | |||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | return; | |||||||||||||
| 66 | 2 | }; | |||||||||||||
| 67 | |||||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | ▶ | around 'log' => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 70 | 0 | my ($orig, $self, $msg) = @_; | |||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | $self->$orig($msg); | |||||||||||||
| 73 | |||||||||||||||
| 74 | 0 | ▶ | return unless $self->syslog_log; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | # collapse space and newlines to create single lines for syslog | ||||||||||||||
| 77 | 0 | $msg =~ s/\h*\v+\h*/ /gs; | |||||||||||||
| 78 | 0 | chomp($msg); | |||||||||||||
| 79 | |||||||||||||||
| 80 | # encode wide characters as UTF-8 | ||||||||||||||
| 81 | 0 | utf8::encode($msg); | |||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | syslog(LOG_NOTICE, $msg); | |||||||||||||
| 84 | |||||||||||||||
| 85 | 0 | return; | |||||||||||||
| 86 | 2 | }; | |||||||||||||
| 87 | |||||||||||||||
| 88 | |||||||||||||||
| 89 | 0 | ▶ | around 'stdout_redirect' => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||
| 90 | 0 | my ($orig, $self) = @_; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | unless ($self->syslog_log) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | $self->$orig(); | |||||||||||||
| 94 | 0 | return; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | my $tie_syslog; | |||||||||||||
| 98 | 0 | ▶ | eval { require Tie::Syslog; } and do { $tie_syslog = 1 unless $@ }; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | # revert to original plan if there is no Tie::Syslog installed | ||||||||||||||
| 101 | 0 | ▶ | unless ($tie_syslog) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | 0 | $self->$orig(); | |||||||||||||
| 103 | 0 | return; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | # FIXME Tie::Syslog does not support the "setlogsock" option | ||||||||||||||
| 107 | # so we can't set another syslog server. This is a problem | ||||||||||||||
| 108 | # on FreeBSD atm | ||||||||||||||
| 109 | 0 | $Tie::Syslog::ident = $self->name; | |||||||||||||
| 110 | 0 | tie *STDOUT, 'Tie::Syslog', { | |||||||||||||
| 111 | facility => 'LOG_USER', | ||||||||||||||
| 112 | priority => 'LOG_NOTICE', | ||||||||||||||
| 113 | }; | ||||||||||||||
| 114 | 0 | tie *STDERR, 'Tie::Syslog', { | |||||||||||||
| 115 | facility => 'LOG_USER', | ||||||||||||||
| 116 | priority => 'LOG_ERR', | ||||||||||||||
| 117 | }; | ||||||||||||||
| 118 | |||||||||||||||
| 119 | # inject our own PRINT function into Tie::Syslog so we can remove | ||||||||||||||
| 120 | # newlines when not in debug mode so syslog feeds splunk with nice | ||||||||||||||
| 121 | # single lines not losing the context | ||||||||||||||
| 122 | 0 | ▶ | unless ($self->debug) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 0 | undef &Tie::Syslog::PRINT; # silence redefine warnings | |||||||||||||
| 124 | 0 | ▶ | *Tie::Syslog::PRINT = sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||
| 125 | 0 | my ($s, @msg) = @_; | |||||||||||||
| 126 | |||||||||||||||
| 127 | warn "Cannot PRINT to a closed filehandle!" | ||||||||||||||
| 128 | 0 | ▶ | unless $s->{'is_open'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | |||||||||||||||
| 130 | 0 | my $msg = join('', @msg); | |||||||||||||
| 131 | |||||||||||||||
| 132 | # remove vertical (and potentially surrounding horizontal) spaces | ||||||||||||||
| 133 | 0 | $msg =~ s/\h*\v+\h*/ /gs; | |||||||||||||
| 134 | 0 | chomp($msg); | |||||||||||||
| 135 | |||||||||||||||
| 136 | # Sys::Syslog does not like wide characters and dies | ||||||||||||||
| 137 | 0 | utf8::encode($msg); | |||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | eval { syslog($s->facility . '|' . $s->priority, $msg); }; | |||||||||||||
| 140 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 0 | syslog($s->facility . '|' . $s->priority, | |||||||||||||
| 142 | "Tie::Syslog::PRINT failed with errors: $@"); | ||||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 0 | return; | |||||||||||||
| 146 | 0 | }; | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 0 | return; | |||||||||||||
| 150 | 2 | }; | |||||||||||||
| 151 | |||||||||||||||
| 152 | 2 | 1; | |||||||||||||
| 153 | |||||||||||||||
| 154 | __END__ | ||||||||||||||