From c5626c77b5e92ea758ad3c8fa1d75bab19b0b1ee Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Sun, 12 Feb 2017 20:09:46 +0000 Subject: [PATCH] Create logger files (#1162) --- lemonldap-ng-common/MANIFEST | 3 +- .../lib/Lemonldap/NG/Common/Logger/Apache2.pm | 9 ++++ .../lib/Lemonldap/NG/Common/Logger/Std.pm | 23 ++++++++++ .../lib/Lemonldap/NG/Common/PSGI.pm | 44 +++++++++---------- lemonldap-ng-handler/MANIFEST | 24 +++++----- .../Lemonldap/NG/Handler/ApacheMP2/Main.pm | 12 +---- .../lib/Lemonldap/NG/Handler/Main/Init.pm | 10 ++--- .../lib/Lemonldap/NG/Handler/Main/Run.pm | 14 +----- .../NG/Handler/Main/SharedVariables.pm | 14 +----- .../lib/Lemonldap/NG/Handler/PSGI/Main.pm | 8 +--- .../t/01-Lemonldap-NG-Handler-Main.t | 4 +- .../t/05-Lemonldap-NG-Handler-Reload.t | 9 +++- lemonldap-ng-handler/t/test.pm | 2 + .../lib/Lemonldap/NG/Portal/Main/Init.pm | 2 +- 14 files changed, 92 insertions(+), 86 deletions(-) create mode 100644 lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Apache2.pm create mode 100644 lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Std.pm diff --git a/lemonldap-ng-common/MANIFEST b/lemonldap-ng-common/MANIFEST index e50b34133..45a276bc2 100644 --- a/lemonldap-ng-common/MANIFEST +++ b/lemonldap-ng-common/MANIFEST @@ -33,6 +33,8 @@ lib/Lemonldap/NG/Common/Conf/Serializer.pm lib/Lemonldap/NG/Common/Conf/Wrapper.pm lib/Lemonldap/NG/Common/Crypto.pm lib/Lemonldap/NG/Common/FormEncode.pm +lib/Lemonldap/NG/Common/Logger/Apache2.pm +lib/Lemonldap/NG/Common/Logger/Std.pm lib/Lemonldap/NG/Common/Module.pm lib/Lemonldap/NG/Common/Notifications.pm lib/Lemonldap/NG/Common/Notifications/DBI.pm @@ -48,7 +50,6 @@ lib/Lemonldap/NG/Common/PSGI/Router.pm lib/Lemonldap/NG/Common/PSGI/SOAPServer.pm lib/Lemonldap/NG/Common/PSGI/SOAPService.pm lib/Lemonldap/NG/Common/Regexp.pm -lib/Lemonldap/NG/Common/Safe.pm lib/Lemonldap/NG/Common/Safelib.pm lib/Lemonldap/NG/Common/Session.pm lib/Lemonldap/NG/Common/Session/REST.pm diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Apache2.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Apache2.pm new file mode 100644 index 000000000..3e01f22c4 --- /dev/null +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Apache2.pm @@ -0,0 +1,9 @@ +package Lemonldap::NG::Common::Logger::Apache2; + +sub AUTOLOAD { + shift; + $AUTOLOAD =~ s/.*:://; + return Apache2::ServerRec->log->$AUTOLOAD(@_); +}; + +1; diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Std.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Std.pm new file mode 100644 index 000000000..79f77a4eb --- /dev/null +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Logger/Std.pm @@ -0,0 +1,23 @@ +package Lemonldap::NG::Common::Logger::Std; + +my @LEVEL = (qw(error warn notice info debug)); + +our $done = 0; + +sub import { + no warnings 'redefine'; + my $level = $_[1] || 'info'; + my $show = 1; + foreach ( @LEVEL ) { + if($show) { + eval qq'sub $_ {print STDERR "[$_] \$_[1]\n"}'; + } + else { + eval qq'sub $_ {1}'; + } + $show = 0 if($level eq $_); + } + die "unknown level $level" if($show); +} + +1; diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm index d2b72ad94..28bb4d4c3 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm @@ -10,6 +10,8 @@ our $VERSION = '2.0.0'; our $_json = JSON->new->allow_nonref; +# PROPERTIES + has error => ( is => 'rw', default => '' ); has languages => ( is => 'rw', isa => 'Str', default => 'en' ); has logLevel => ( is => 'rw', isa => 'Str', default => 'info' ); @@ -18,6 +20,7 @@ has staticPrefix => ( is => 'rw', isa => 'Str' ); has templateDir => ( is => 'rw', isa => 'Str' ); has links => ( is => 'rw', isa => 'ArrayRef' ); has menuLinks => ( is => 'rw', isa => 'ArrayRef' ); +has logger => ( is => 'rw', default => 'Lemonldap::NG::Common::Logger::Std', ); has syslog => ( is => 'rw', isa => 'Str', @@ -36,22 +39,31 @@ has syslog => ( }, ); +# INITIALIZATION + +sub init { + my ( $self, $args ) = @_; + unless ( ref $args ) { + $self->error('init argument must be a hashref'); + return 0; + } + foreach my $k ( keys %$args ) { + $self->{$k} = $args->{$k}; + } + eval "use $self->{logger} '$self->{logLevel}'"; + die $@ if($@); + return 1; +} + +# RUNNING METHODS + ## @method void lmLog(string mess, string level) # Log subroutine. Print on STDERR messages if it exceeds `logLevel` value # @param $mess Text to log # @param $level Level (debug|info|notice|warn|error) sub lmLog { my ( $self, $msg, $level ) = @_; - my $levels = { - error => 4, - warn => 3, - notice => 2, - info => 1, - debug => 0 - }; - my $l = $levels->{$level} || 1; - return if ( ref($self) and $l < $levels->{ $self->{logLevel} } ); - print STDERR "[$level] " . ( $l ? '' : (caller)[0] . ': ' ) . " $msg\n"; + return $self->logger->$level($msg); } ##@method void userLog(string mess, string level) @@ -191,18 +203,6 @@ sub _mustBeDefined { die "$name() method must be implemented (probably in $ref)"; } -sub init { - my ( $self, $args ) = @_; - unless ( ref $args ) { - $self->error('init argument must be a hashref'); - return 0; - } - foreach my $k ( keys %$args ) { - $self->{$k} = $args->{$k}; - } - return 1; -} - sub handler { _mustBeDefined(@_) } sub sendHtml { diff --git a/lemonldap-ng-handler/MANIFEST b/lemonldap-ng-handler/MANIFEST index 171cb75b0..27eb338de 100644 --- a/lemonldap-ng-handler/MANIFEST +++ b/lemonldap-ng-handler/MANIFEST @@ -7,26 +7,30 @@ lib/Lemonldap/NG/Handler.pm lib/Lemonldap/NG/Handler/ApacheMP2.pm lib/Lemonldap/NG/Handler/ApacheMP2/AuthBasic.pm lib/Lemonldap/NG/Handler/ApacheMP2/Main.pm -lib/Lemonldap/NG/Handler/API/ExperimentalNginx.pm +lib/Lemonldap/NG/Handler/ApacheMP2/Menu.pm +lib/Lemonldap/NG/Handler/ApacheMP2/SecureToken.pm +lib/Lemonldap/NG/Handler/ApacheMP2/ZimbraPreAuth.pm lib/Lemonldap/NG/Handler/Lib/AuthBasic.pm +lib/Lemonldap/NG/Handler/Lib/PSGI.pm +lib/Lemonldap/NG/Handler/Lib/SecureToken.pm +lib/Lemonldap/NG/Handler/Lib/Status.pm +lib/Lemonldap/NG/Handler/Lib/ZimbraPreAuth.pm lib/Lemonldap/NG/Handler/Main.pm lib/Lemonldap/NG/Handler/Main/Init.pm lib/Lemonldap/NG/Handler/Main/Jail.pm lib/Lemonldap/NG/Handler/Main/Reload.pm lib/Lemonldap/NG/Handler/Main/Run.pm lib/Lemonldap/NG/Handler/Main/SharedVariables.pm -lib/Lemonldap/NG/Handler/Menu.pm -lib/Lemonldap/NG/Handler/Nginx.pm lib/Lemonldap/NG/Handler/PSGI.pm -lib/Lemonldap/NG/Handler/PSGI/API.pm -lib/Lemonldap/NG/Handler/PSGI/API/Server.pm -lib/Lemonldap/NG/Handler/PSGI/Base.pm +lib/Lemonldap/NG/Handler/PSGI/Main.pm lib/Lemonldap/NG/Handler/PSGI/Router.pm -lib/Lemonldap/NG/Handler/PSGI/Server.pm lib/Lemonldap/NG/Handler/PSGI/Try.pm -lib/Lemonldap/NG/Handler/SecureToken.pm -lib/Lemonldap/NG/Handler/Status.pm -lib/Lemonldap/NG/Handler/ZimbraPreAuth.pm +lib/Lemonldap/NG/Handler/Server.pm +lib/Lemonldap/NG/Handler/Server/AuthBasic.pm +lib/Lemonldap/NG/Handler/Server/Main.pm +lib/Lemonldap/NG/Handler/Server/Nginx.pm +lib/Lemonldap/NG/Handler/Server/SecureToken.pm +lib/Lemonldap/NG/Handler/Server/ZimbraPreAuth.pm Makefile.PL MANIFEST This list of files META.yml diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/ApacheMP2/Main.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/ApacheMP2/Main.pm index 539250694..d820a3c80 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/ApacheMP2/Main.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/ApacheMP2/Main.pm @@ -25,6 +25,7 @@ use constant SERVER_ERROR => Apache2::Const::SERVER_ERROR; use constant AUTH_REQUIRED => Apache2::Const::AUTH_REQUIRED; use constant MAINTENANCE => Apache2::Const::HTTP_SERVICE_UNAVAILABLE; use constant BUFF_LEN => 8192; +use constant defaultLogger => 'Lemonldap::NG::Common::Logger::Apache2'; eval { require threads::shared; }; @@ -93,17 +94,6 @@ sub newRequest { $request = $r; } -## @method void _lmLog(string $msg, string $level) -# logs message $msg to Apache logs with loglevel $level -# @param $msg string message to log -# @param $level string loglevel -sub _lmLog { - my ( $class, $msg, $level ) = @_; - - # TODO: remove the useless tag 'ApacheMP2.pm(70):' in debug logs - Apache2::ServerRec->log->$level($msg); -} - ## @method void set_user(string user) # sets remote_user # @param user string username diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Init.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Init.pm index c63b07ca8..70bbda7b8 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Init.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Init.pm @@ -48,12 +48,10 @@ sub init($$) { # Set log level for Lemonldap::NG logs sub logLevelInit { my ( $class, $level ) = @_; - if ( $level and not defined $class->logLevels->{$level} ) { - $class->lmLog( "Undefined log level: $level", 'error' ); - } - else { - $class->_logLevel( $class->logLevels->{ $level || 'notice' } ); - } + my $logger = $class->localConfig->{logger} ||= $class->defaultLogger; + $level ||= 'info'; + eval "use $logger '$level'"; + die $@ if($@); } # @method void serverSignatureInit diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm index 663242464..37d1c3282 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/Run.pm @@ -259,19 +259,7 @@ sub updateStatus { # @param $level string (emerg|alert|crit|error|warn|notice|info|debug) sub lmLog { my ( $class, $msg, $level ) = @_; - return if ( $class->logLevels->{$level} < $class->_logLevel ); - - my ( $module, $file, $line ) = caller(); - - if ( $level eq 'debug' ) { - $file =~ s#.+/##; - $class->_lmLog( "$file($line): $msg", 'debug' ); - } - else { - $class->_lmLog( "$file($line):", 'debug' ) - if ( $class->_logLevel == 0 ); - $class->_lmLog( "Lemonldap::NG::Handler: $msg", $level ); - } + return $class->localConfig->{logger}->$level($msg); } ## @rmethod protected boolean checkMaintenanceMode diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/SharedVariables.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/SharedVariables.pm index adf6ffb82..c9e2f0d13 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/SharedVariables.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main/SharedVariables.pm @@ -18,18 +18,6 @@ our $_tshv = { confAcc => {}, lmConf => {}, localConfig => {}, - logLevel => 'notice', - _logLevel => 2, - logLevels => { - emerg => 7, - alert => 6, - crit => 5, - error => 4, - warn => 3, - notice => 2, - info => 1, - debug => 0, - } }; # Current sessions properties @@ -38,7 +26,7 @@ our $_v = { session => {}, datas => {} }; BEGIN { # Thread shared accessors foreach ( - qw(tsv cfgNum lastCheck checkTime confAcc localConfig logLevel _logLevel logLevels lmConf) + qw(tsv cfgNum lastCheck checkTime confAcc localConfig lmConf) ) { eval " sub $_ { diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Main.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Main.pm index a9bec97a0..e55bf208a 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Main.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/PSGI/Main.pm @@ -14,6 +14,7 @@ use constant DONE => 0; use constant SERVER_ERROR => 500; use constant AUTH_REQUIRED => 401; use constant MAINTENANCE => 503; +use constant defaultLogger => 'Lemonldap::NG::Common::Logger::Std'; our $request; @@ -40,13 +41,6 @@ sub newRequest { $request = $r; } -## @method void _lmLog(string $msg, string $level) -# logs message $msg to STDERR with level $level -# set Env Var lmLogLevel to set loglevel; set to "info" by default -# @param $msg string message to log -# @param $level string loglevel -*_lmLog = *Lemonldap::NG::Common::PSGI::lmLog; - ## @method void set_user(string user) # sets remote_user in response headers # @param user string username diff --git a/lemonldap-ng-handler/t/01-Lemonldap-NG-Handler-Main.t b/lemonldap-ng-handler/t/01-Lemonldap-NG-Handler-Main.t index b70810c4e..70d6fa224 100644 --- a/lemonldap-ng-handler/t/01-Lemonldap-NG-Handler-Main.t +++ b/lemonldap-ng-handler/t/01-Lemonldap-NG-Handler-Main.t @@ -3,7 +3,7 @@ use strict; use warnings; require 't/test.pm'; -use Test::More tests => 9; +use Test::More tests => 10; BEGIN { use_ok('Lemonldap::NG::Handler::Main') } # get a standard basic configuration in $args hashref @@ -23,6 +23,7 @@ $ENV{SERVER_NAME} = "test1.example.com"; my $conf = { 'cfgNum' => 1, + 'logLevel' => 'error', 'portal' => 'http://auth.example.com/', 'globalStorage' => 'Apache::Session::File', 'post' => {}, @@ -45,6 +46,7 @@ my $conf = { }, }; +ok( $h->init($conf), 'init' ); ok( $h->configReload($conf), 'Load conf' ); ok( $h->tsv->{portal}->() eq 'http://auth.example.com/', 'portal' ); diff --git a/lemonldap-ng-handler/t/05-Lemonldap-NG-Handler-Reload.t b/lemonldap-ng-handler/t/05-Lemonldap-NG-Handler-Reload.t index 6c211258b..950b067d5 100644 --- a/lemonldap-ng-handler/t/05-Lemonldap-NG-Handler-Reload.t +++ b/lemonldap-ng-handler/t/05-Lemonldap-NG-Handler-Reload.t @@ -7,7 +7,7 @@ package My::Package; -use Test::More tests => 5; +use Test::More tests => 6; BEGIN { use_ok('Lemonldap::NG::Handler::Main'); @@ -22,6 +22,13 @@ my $globalinit; my $tsv = {}; +sub Lemonldap::NG::Handler::Main::defaultLogger { + 'Lemonldap::NG::Common::Logger::Std'; +} + +eval { Lemonldap::NG::Handler::Main->logLevelInit('error') }; +ok( !$@, 'logLevelInit' ); + ok( Lemonldap::NG::Handler::Main->jailInit( { diff --git a/lemonldap-ng-handler/t/test.pm b/lemonldap-ng-handler/t/test.pm index 95ce5dbfd..4b9620b10 100644 --- a/lemonldap-ng-handler/t/test.pm +++ b/lemonldap-ng-handler/t/test.pm @@ -8,6 +8,8 @@ use Lemonldap::NG::Handler::Main; our @ISA = qw(Lemonldap::NG::Handler::Main); our $header; +use constant defaultLogger => 'Lemonldap::NG::Common::Logger::Std'; + use constant REDIRECT => 302; #sub hostname { 'test1.example.com' } *hostname = \&main::hostname; diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Init.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Init.pm index 931636db8..43a36d5fd 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Init.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Init.pm @@ -93,7 +93,7 @@ sub init { # Purge loaded module list $self->loadedModules( {} ); Lemonldap::NG::Handler::Main->onReload( $self, 'reloadConf' ); - return 0 unless ( $self->SUPER::init($args) ); + return 0 unless ( $self->SUPER::init($self->localConfig) ); return 0 if ( $self->error ); # Handle requests (other path may be declared in enabled plugins)