lemonldap-ng/lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Dispatch.pm

43 lines
1.0 KiB
Perl
Raw Normal View History

2018-05-11 14:43:41 +02:00
package Lemonldap::NG::Common::Logger::Dispatch;
use strict;
2019-02-12 18:21:38 +01:00
our $VERSION = '2.1.0';
2018-05-11 14:43:41 +02:00
sub new {
no warnings 'redefine';
my $self = bless {}, shift;
2018-06-21 21:35:16 +02:00
my ( $conf, %args ) = @_;
2018-05-11 14:43:41 +02:00
my %bck;
my $last;
my $show = 1;
my $root = $args{user} ? 'userLogDispatch' : 'logDispatch';
2018-06-21 21:35:16 +02:00
unless ( $conf->{ $root . 'Error' } ) {
die "At least, ${root}Error must be defined in conf";
2018-05-11 14:43:41 +02:00
}
foreach my $l (qw(error warn notice info debug)) {
if ($show) {
$last = $conf->{ $root . ucfirst($l) } || $last;
2018-05-11 14:43:41 +02:00
unless ( $bck{$last} ) {
eval "require $last";
die $@ if ($@);
$bck{$last} = $last->new(@_);
}
my $obj = $bck{$last};
eval "sub $l {
shift;
return \$obj->$l(\@_);
}";
}
else {
eval qq'sub $l {1}';
}
$show = 0 if ( $conf->{logLevel} eq $l );
}
die "unknown level $conf->{logLevel}" if ($show);
return $self;
}
1;