Create logger files (#1162)

This commit is contained in:
Xavier Guimard 2017-02-12 20:09:46 +00:00
parent d1091a2c99
commit c5626c77b5
14 changed files with 92 additions and 86 deletions

View File

@ -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

View File

@ -0,0 +1,9 @@
package Lemonldap::NG::Common::Logger::Apache2;
sub AUTOLOAD {
shift;
$AUTOLOAD =~ s/.*:://;
return Apache2::ServerRec->log->$AUTOLOAD(@_);
};
1;

View File

@ -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;

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 $_ {

View File

@ -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

View File

@ -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' );

View File

@ -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(
{

View File

@ -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;

View File

@ -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)