Syslog.pm 1019 Bytes
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4 5
package Lemonldap::NG::Common::Logger::Syslog;

use strict;
use Sys::Syslog qw(:standard);

Yadd's avatar
Yadd committed
6
our $VERSION = '2.1.0';
Yadd's avatar
Yadd committed
7

Yadd's avatar
Yadd committed
8 9 10 11 12 13 14 15 16 17
sub new {
    my ( $class, $conf, %args ) = @_;
    my $level = $conf->{logLevel} || 'info';
    my $self = bless {}, $class;
    if ( $args{user} ) {
        $self->{facility} = $conf->{userSyslogFacility} || 'auth';
    }
    else {
        $self->{facility} = $conf->{syslogFacility} || 'daemon';
    }
18
    eval { openlog( 'LLNG', 'cons,pid,ndelay', $self->{facility} ) };
Yadd's avatar
Yadd committed
19 20 21 22 23 24
    no warnings 'redefine';
    my $show = 1;
    foreach (qw(error warn notice info debug)) {
        if ($show) {
            my $name = $_;
            $name = 'warning' if ( $_ eq 'warn' );
25
            $name = 'err'     if ( $_ eq 'error' );
Yadd's avatar
Yadd committed
26
            eval qq@sub $_ {syslog("$name|".\$_[0]->{facility},\$_[1])}@;
Yadd's avatar
Yadd committed
27
            die $@ if ($@);
Yadd's avatar
Yadd committed
28 29
        }
        else {
Yadd's avatar
Yadd committed
30
            eval "sub $_ {1}";
Yadd's avatar
Yadd committed
31 32 33 34 35 36 37 38
        }
        $show = 0 if ( $level eq $_ );
    }
    die "unknown level $level" if ($show);
    return $self;
}

1;