| 1 |
package Lemonldap::NG::Common::Logger::Syslog; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Sys::Syslog qw(:standard :extended); |
| 5 |
|
| 6 |
our $VERSION = '2.22.0'; |
| 7 |
|
| 8 |
sub new { |
| 9 |
my ( $class, $conf, %args ) = @_; |
| 10 |
my $level = $conf->{logLevel} || 'info'; |
| 11 |
my $self = bless {}, $class; |
| 12 |
our $done; |
| 13 |
if ( $args{user} ) { |
| 14 |
$self->{facility} = $conf->{userSyslogFacility} || 'auth'; |
| 15 |
$self->{options} = $conf->{userSyslogOptions} || 'cons,pid,ndelay'; |
| 16 |
} |
| 17 |
else { |
| 18 |
$self->{facility} = $conf->{syslogFacility} || 'daemon'; |
| 19 |
$self->{options} = $conf->{syslogOptions} || 'cons,pid,ndelay'; |
| 20 |
} |
| 21 |
|
| 22 |
# Avoid to launch openlog multiple times, to prevent mech degradation |
| 23 |
# from native to unix. See also |
| 24 |
# https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/-/issues/2771 |
| 25 |
unless ($done) { |
| 26 |
if ( $conf->{syslogSockOptions} ) { |
| 27 |
$conf->{syslogSockOptions}->{type} ||= 'udp'; |
| 28 |
setlogsock( $conf->{syslogSockOptions} ); |
| 29 |
} |
| 30 |
eval { |
| 31 |
openlog( 'LLNG', $self->{options}, $self->{facility} ); |
| 32 |
$done = 1; |
| 33 |
}; |
| 34 |
} |
| 35 |
no warnings 'redefine'; |
| 36 |
my $show = 1; |
| 37 |
|
| 38 |
foreach (qw(error warn notice info debug)) { |
| 39 |
if ($show) { |
| 40 |
my $name = $_; |
| 41 |
$name = 'warning' if ( $_ eq 'warn' ); |
| 42 |
$name = 'err' if ( $_ eq 'error' ); |
| 43 |
eval |
| 44 |
qq'sub $_ {syslog("$name|".\$_[0]->{facility},"[$_] ". \$_[1])}'; |
| 45 |
die $@ if ($@); |
| 46 |
} |
| 47 |
else { |
| 48 |
eval qq'sub $_ {1}'; |
| 49 |
} |
| 50 |
$show = 0 if ( $level eq $_ ); |
| 51 |
} |
| 52 |
die "Unknown logLevel $level" if $show; |
| 53 |
|
| 54 |
return $self; |
| 55 |
} |
| 56 |
|
| 57 |
1; |