From 8b4b2273271f4e8adc01d0754ef68155c1f29a59 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 14 May 2020 10:49:50 +0200 Subject: [PATCH] LScli: fix console logging to always log at least errors (excepted in quiet mode) --- src/includes/class/class.LScli.php | 29 +++++++++++++---- src/includes/class/class.LSlog.php | 38 +++++++++++++++++++--- src/includes/class/class.LSlog_handler.php | 22 ++++++++++++- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/includes/class/class.LScli.php b/src/includes/class/class.LScli.php index c86e5ed0..bcd379f7 100644 --- a/src/includes/class/class.LScli.php +++ b/src/includes/class/class.LScli.php @@ -85,9 +85,10 @@ class LScli extends LSlog_staticLoggerClass { echo "Usage : ".basename($argv[0])." [-h] [-qdC] command\n"; echo " -h Show this message\n"; - echo " -q|--quiet Quiet mode\n"; - echo " -d|--debug Debug mode\n"; - echo " -C|--console Log on console\n"; + echo " -q|--quiet Quiet mode: nothing log on console (but keep other logging handler)\n"; + echo " -d|--debug Debug mode (set log level to DEBUG, default: WARNING)\n"; + echo " -v|--verbose Verbose mode (set log level to INFO, default: WARNING)\n"; + echo " -C|--console Log on console with same log level as other handlers (otherwise, log only errors)\n"; echo " -S|--ldap-server Connect to a specific LDAP server: you could specify a LDAP\n"; echo " server by its declaration order in configuration (default:\n"; echo " first one).\n"; @@ -126,7 +127,9 @@ class LScli extends LSlog_staticLoggerClass { return; } global $argv; - $log_level = 'INFO'; + $log_level = 'WARNING'; + $console_log = false; + $quiet = false; $ldap_server_id = false; $command = false; $command_args = array(); @@ -148,13 +151,17 @@ class LScli extends LSlog_staticLoggerClass { case '--debug': $log_level = 'DEBUG'; break; + case '-v': + case '--verbose': + $log_level = 'INFO'; + break; case '-q': case '--quiet': - $log_level = 'WARNING'; + $quiet = true; break; case '-C': case '--console': - LSlog :: logOnConsole(); + $console_log = true; break; case '-S': case '--ldap-server': @@ -191,6 +198,16 @@ class LScli extends LSlog_staticLoggerClass { // Set log level LSlog :: setLevel($log_level); + // Enable/disable log on console + if ($quiet) + // Quiet mode: disable log on console + LSlog :: disableLogOnConsole(); + else + // Enable console log: + // - if $console_log: use same log level as other handlers + // - otherwise: log only errors + LSlog :: logOnConsole(($console_log?$log_level:'ERROR')); + if (!$command) { self :: log_debug("LScli :: handle_args() : no detected command => show usage"); self :: usage(); diff --git a/src/includes/class/class.LSlog.php b/src/includes/class/class.LSlog.php index 1ac3d19f..7d77a6fd 100644 --- a/src/includes/class/class.LSlog.php +++ b/src/includes/class/class.LSlog.php @@ -123,15 +123,34 @@ class LSlog { /** * Enable console handler (if not already enabled) * + * @param[in] $level string|null The log level of the console handler + * * @retval boolean True if log on console enabled, false otherwise **/ - public static function logOnConsole() { - for ($i=0; $i < count(self :: $handlers); $i++) - if (is_a(self :: $handlers[$i], 'LSlog_console')) + public static function logOnConsole($level=null) { + for ($i=0; $i < count(self :: $handlers); $i++) { + if (is_a(self :: $handlers[$i], 'LSlog_console')) { + if (!is_null($level)) + self :: $handlers[$i] -> setLevel($level); return true; - return self :: add_handler('console'); + } + } + return self :: add_handler('console', array('level' => $level)); } + /** + * Disable console handler (if already enabled) + * + * @retval void + **/ + public static function disableLogOnConsole() { + for ($i=0; $i < count(self :: $handlers); $i++) { + if (is_a(self :: $handlers[$i], 'LSlog_console')) { + LSlog :: debug('Remove console handler'); + unset(self :: $handlers[$i]); + } + } + } /** * Set log level @@ -245,6 +264,17 @@ class LSlog { return (self :: $levels[$level] >= self :: $levels[$configured_level]); } + /** + * Check if a log level exists + * + * @param[in] $level string The level + * + * @retval bool True if the specified log level exists, False otherwise + **/ + public static function checkLevelExists($level) { + return array_key_exists($level, self :: $levels); + } + /* * PHP error logging helpers */ diff --git a/src/includes/class/class.LSlog_handler.php b/src/includes/class/class.LSlog_handler.php index eddb7dac..24dda664 100644 --- a/src/includes/class/class.LSlog_handler.php +++ b/src/includes/class/class.LSlog_handler.php @@ -30,6 +30,9 @@ class LSlog_handler { // The handler configuration protected $config; + // Log level + protected $level; + // Default log formats protected $default_format = '%{requesturi} - %{remoteaddr} - %{ldapservername} - %{authuser} - %{logger} - %{level} - %{message}'; protected $default_cli_format = '%{clibinpath} - %{logger} - %{level} - %{message}'; @@ -53,6 +56,7 @@ class LSlog_handler { **/ public function __construct($config) { $this -> config = $config; + $this -> level = $this -> getConfig('level', null, 'string'); $this -> loggers = $this -> getConfig('loggers', array()); if (!is_array($this -> loggers)) $this -> loggers = array($this -> loggers); @@ -90,6 +94,22 @@ class LSlog_handler { return LSconfig :: get($var, $default, $cast, $this -> config); } + /** + * Set log level + * + * @param[in] $level string The level + * + * @retval bool True if log level set, False otherwise + **/ + public function setLevel($level) { + if (!is_null($level) && !LSlog :: checkLevelExists($level)) { + LSlog :: error("Invalid log level '$level'"); + return false; + } + LSlog :: debug("Log handler ".get_called_class()." level set to ".(is_null($level)?'default':$level)); + $this -> level = $level; + } + /** * Check level against configured level * @@ -98,7 +118,7 @@ class LSlog_handler { * @retval bool True if a message with this level have to be logged, False otherwise **/ public function checkLevel($level) { - return LSlog :: checkLevel($level, $this -> getConfig('level')); + return LSlog :: checkLevel($level, $this -> level); } /**