LScli: fix console logging to always log at least errors (excepted in quiet mode)

This commit is contained in:
Benjamin Renard 2020-05-14 10:49:50 +02:00
parent a1a23d1600
commit 8b4b227327
3 changed files with 78 additions and 11 deletions

View file

@ -85,9 +85,10 @@ class LScli extends LSlog_staticLoggerClass {
echo "Usage : ".basename($argv[0])." [-h] [-qdC] command\n"; echo "Usage : ".basename($argv[0])." [-h] [-qdC] command\n";
echo " -h Show this message\n"; echo " -h Show this message\n";
echo " -q|--quiet Quiet mode\n"; echo " -q|--quiet Quiet mode: nothing log on console (but keep other logging handler)\n";
echo " -d|--debug Debug mode\n"; echo " -d|--debug Debug mode (set log level to DEBUG, default: WARNING)\n";
echo " -C|--console Log on console\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 " -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 " server by its declaration order in configuration (default:\n";
echo " first one).\n"; echo " first one).\n";
@ -126,7 +127,9 @@ class LScli extends LSlog_staticLoggerClass {
return; return;
} }
global $argv; global $argv;
$log_level = 'INFO'; $log_level = 'WARNING';
$console_log = false;
$quiet = false;
$ldap_server_id = false; $ldap_server_id = false;
$command = false; $command = false;
$command_args = array(); $command_args = array();
@ -148,13 +151,17 @@ class LScli extends LSlog_staticLoggerClass {
case '--debug': case '--debug':
$log_level = 'DEBUG'; $log_level = 'DEBUG';
break; break;
case '-v':
case '--verbose':
$log_level = 'INFO';
break;
case '-q': case '-q':
case '--quiet': case '--quiet':
$log_level = 'WARNING'; $quiet = true;
break; break;
case '-C': case '-C':
case '--console': case '--console':
LSlog :: logOnConsole(); $console_log = true;
break; break;
case '-S': case '-S':
case '--ldap-server': case '--ldap-server':
@ -191,6 +198,16 @@ class LScli extends LSlog_staticLoggerClass {
// Set log level // Set log level
LSlog :: setLevel($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) { if (!$command) {
self :: log_debug("LScli :: handle_args() : no detected command => show usage"); self :: log_debug("LScli :: handle_args() : no detected command => show usage");
self :: usage(); self :: usage();

View file

@ -123,15 +123,34 @@ class LSlog {
/** /**
* Enable console handler (if not already enabled) * 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 * @retval boolean True if log on console enabled, false otherwise
**/ **/
public static function logOnConsole() { public static function logOnConsole($level=null) {
for ($i=0; $i < count(self :: $handlers); $i++) for ($i=0; $i < count(self :: $handlers); $i++) {
if (is_a(self :: $handlers[$i], 'LSlog_console')) if (is_a(self :: $handlers[$i], 'LSlog_console')) {
if (!is_null($level))
self :: $handlers[$i] -> setLevel($level);
return true; 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 * Set log level
@ -245,6 +264,17 @@ class LSlog {
return (self :: $levels[$level] >= self :: $levels[$configured_level]); 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 * PHP error logging helpers
*/ */

View file

@ -30,6 +30,9 @@ class LSlog_handler {
// The handler configuration // The handler configuration
protected $config; protected $config;
// Log level
protected $level;
// Default log formats // Default log formats
protected $default_format = '%{requesturi} - %{remoteaddr} - %{ldapservername} - %{authuser} - %{logger} - %{level} - %{message}'; protected $default_format = '%{requesturi} - %{remoteaddr} - %{ldapservername} - %{authuser} - %{logger} - %{level} - %{message}';
protected $default_cli_format = '%{clibinpath} - %{logger} - %{level} - %{message}'; protected $default_cli_format = '%{clibinpath} - %{logger} - %{level} - %{message}';
@ -53,6 +56,7 @@ class LSlog_handler {
**/ **/
public function __construct($config) { public function __construct($config) {
$this -> config = $config; $this -> config = $config;
$this -> level = $this -> getConfig('level', null, 'string');
$this -> loggers = $this -> getConfig('loggers', array()); $this -> loggers = $this -> getConfig('loggers', array());
if (!is_array($this -> loggers)) if (!is_array($this -> loggers))
$this -> loggers = array($this -> loggers); $this -> loggers = array($this -> loggers);
@ -90,6 +94,22 @@ class LSlog_handler {
return LSconfig :: get($var, $default, $cast, $this -> config); 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 * 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 * @retval bool True if a message with this level have to be logged, False otherwise
**/ **/
public function checkLevel($level) { public function checkLevel($level) {
return LSlog :: checkLevel($level, $this -> getConfig('level')); return LSlog :: checkLevel($level, $this -> level);
} }
/** /**