| 1 | package Log::Abstraction::Backend::Syslog; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # The syslog backend: send messages to syslog through Sys::Syslog | ||||||||||||||||||||
| 4 | 42 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 42 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 42 | ▶ | use parent 'Log::Abstraction::Backend'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 42 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 42 | ▶ | use Readonly (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 42 | ▶ | use Sys::Syslog 0.28 (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 42 | our $VERSION = '0.40'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # Default syslog connection parameters | ||||||||||||||||||||
| 16 | 42 | Readonly::Scalar my $DEFAULT_FACILITY => 'local0'; | |||||||||||||||||||
| 17 | 42 | Readonly::Scalar my $DEFAULT_OPTIONS => 'cons,pid'; | |||||||||||||||||||
| 18 | 42 | Readonly::Scalar my $DEFAULT_IDENTITY => 'user'; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Map internal level names to POSIX syslog priority strings. syslog has no | ||||||||||||||||||||
| 21 | # priority below debug, so trace shares debug's threshold | ||||||||||||||||||||
| 22 | 42 | Readonly::Hash my %PRIORITY => ( | |||||||||||||||||||
| 23 | trace => 'debug', | ||||||||||||||||||||
| 24 | debug => 'debug', | ||||||||||||||||||||
| 25 | info => 'info', | ||||||||||||||||||||
| 26 | notice => 'notice', | ||||||||||||||||||||
| 27 | warn => 'warning', | ||||||||||||||||||||
| 28 | warning => 'warning', | ||||||||||||||||||||
| 29 | error => 'err', | ||||||||||||||||||||
| 30 | critical => 'crit', | ||||||||||||||||||||
| 31 | alert => 'alert', | ||||||||||||||||||||
| 32 | emergency => 'emerg', | ||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # Number of live Log::Abstraction instances that have opened the | ||||||||||||||||||||
| 36 | # process-global syslog connection; closelog() is only called when the last | ||||||||||||||||||||
| 37 | # one is destroyed | ||||||||||||||||||||
| 38 | 42 | my $open_count = 0; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head1 NAME | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | Log::Abstraction::Backend::Syslog - send messages to syslog | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head1 VERSION | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0.40 | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | Built for the C<syslog> key of a C<logger> hash. The connection is opened | ||||||||||||||||||||
| 51 | (C<setlogsock()> with the hash's other keys, then C<openlog()>) for the | ||||||||||||||||||||
| 52 | first message, and the message sent with C<syslog()> through a C<%s> | ||||||||||||||||||||
| 53 | format, so C<%> sequences in it are logged literally. The caller's hash is | ||||||||||||||||||||
| 54 | not changed: C<facility>, C<level> and C<format> are taken out of a copy, | ||||||||||||||||||||
| 55 | and C<server> renamed C<host>. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | C<openlog()> and C<closelog()> are process-wide, so the instances that have | ||||||||||||||||||||
| 58 | opened the connection are counted, and C<closelog()> is called when the | ||||||||||||||||||||
| 59 | last one is destroyed. | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | For use by L<Log::Abstraction> only; see L<Log::Abstraction::Backend>. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =head1 METHODS | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head2 new | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | my $backend = Log::Abstraction::Backend::Syslog->new( | ||||||||||||||||||||
| 68 | syslog => \%syslog, level => $level, format => $format, | ||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 26 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 74 | 26 | my ($class, %args) = @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # Work on a copy so that the caller's hash is not changed | ||||||||||||||||||||
| 77 | 26 | my %sock = %{$args{'syslog'}}; | |||||||||||||||||||
| 78 | 26 | ▶ | my $facility = delete($sock{'facility'}) || $DEFAULT_FACILITY; | ||||||||||||||||||
|
Condition66%2/3
delete $sock{'facility'} || $DEFAULT_FACILITY
MC/DC0%0/2
delete $sock{'facility'} || $DEFAULT_FACILITY
delete $sock{'facility'}
$DEFAULT_FACILITY
Truth table66%2/3
A || B
A= delete $sock{'facility'}B= $DEFAULT_FACILITY
| |||||||||||||||||||||
| 79 | 26 | delete @sock{qw(level format)}; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # Accept 'server' as an alias for 'host' (CHI convention) | ||||||||||||||||||||
| 82 | 26 | ▶ | if($sock{'server'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 3 | $sock{'host'} = delete $sock{'server'}; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 26 | return $class->SUPER::new(%args, facility => $facility, sock => \%sock); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head2 emit | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | $backend->emit($logger, $event); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 30 | ▶ | sub emit { | ||||||||||||||||||
|
Subroutine
emit: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 96 | 30 | my ($self, $logger, $event) = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # Open the persistent syslog connection on first use | ||||||||||||||||||||
| 99 | 30 | ▶ | if(!$logger->{'_syslog_opened'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 23 | my $sock = $self->{'sock'}; | |||||||||||||||||||
| 101 | 23 | ▶ | Sys::Syslog::setlogsock({ %{$sock} }) if(scalar keys %{$sock}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 23 | Sys::Syslog::openlog($logger->{'script_name'}, $DEFAULT_OPTIONS, $DEFAULT_IDENTITY); | |||||||||||||||||||
| 103 | 23 | $logger->{'_syslog_opened'} = 1; | |||||||||||||||||||
| 104 | 23 | $open_count++; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # The message is passed through '%s' so that a '%m' (or any other '%' | ||||||||||||||||||||
| 108 | # sequence) in it is logged literally | ||||||||||||||||||||
| 109 | 30 | eval { | |||||||||||||||||||
| 110 | 30 | my $priority = $PRIORITY{$event->{'level'}}; | |||||||||||||||||||
| 111 | 30 | Sys::Syslog::syslog("$priority|$self->{facility}", '%s', $self->text($event, $event->{'text'})); | |||||||||||||||||||
| 112 | }; | ||||||||||||||||||||
| 113 | 30 | ▶ | if($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 2 | Carp::carp(ref($logger), ": syslog failed: $@"); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 retain_connection | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | Log::Abstraction::Backend::Syslog->retain_connection(); | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Counts another user of the open connection: a clone of a logger that has | ||||||||||||||||||||
| 123 | opened it. | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 1 | ▶ | sub retain_connection { | ||||||||||||||||||
|
Subroutine
retain_connection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 128 | 1 | $open_count++; | |||||||||||||||||||
| 129 | 1 | return; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head2 release_connection | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Log::Abstraction::Backend::Syslog->release_connection(); | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | A logger that opened the connection is going away; calls C<closelog()> if it | ||||||||||||||||||||
| 137 | was the last. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 26 | ▶ | sub release_connection { | ||||||||||||||||||
|
Subroutine
release_connection: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 142 | 26 | ▶ | $open_count-- if($open_count > 0); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 26 | ▶ | Sys::Syslog::closelog() if($open_count == 0); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | 26 | return; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =head1 AUTHOR | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Nigel Horne C<njh@nigelhorne.com> | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =head1 SEE ALSO | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | L<Log::Abstraction>, L<Log::Abstraction::Backend> | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Copyright (C) 2025-2026 Nigel Horne | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||
| 160 | If you use it, | ||||||||||||||||||||
| 161 | please let me know. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 42 | 1; | |||||||||||||||||||