From 988054bfc5d799f5538f98c1456273d6765b2397 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 10 Sep 2020 10:17:19 +0200 Subject: [PATCH] LSlog handlers: fix log TRACE enabled error (even if disabled) --- src/includes/class/class.LSlog_console.php | 2 ++ src/includes/class/class.LSlog_email.php | 3 +- src/includes/class/class.LSlog_file.php | 3 +- src/includes/class/class.LSlog_handler.php | 36 ++++++++++++++++------ src/includes/class/class.LSlog_syslog.php | 3 +- src/includes/class/class.LSlog_system.php | 13 ++++++++ 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/includes/class/class.LSlog_console.php b/src/includes/class/class.LSlog_console.php index de0e482c..c603843c 100644 --- a/src/includes/class/class.LSlog_console.php +++ b/src/includes/class/class.LSlog_console.php @@ -42,6 +42,8 @@ class LSlog_console extends LSlog_handler { parent :: __construct($config); $this -> stdout = fopen('php://stdout', 'w'); $this -> stderr = fopen('php://stderr', 'w'); + if ($this -> enabled) + LSlog :: log_trace("$this Enabled", get_class($this)); } /** diff --git a/src/includes/class/class.LSlog_email.php b/src/includes/class/class.LSlog_email.php index ff3884fb..89caa813 100644 --- a/src/includes/class/class.LSlog_email.php +++ b/src/includes/class/class.LSlog_email.php @@ -43,7 +43,8 @@ class LSlog_email extends LSlog_handler { public function __construct($config) { parent :: __construct($config); $this -> recipient = self :: getConfig('recipient'); - $this -> logging('TRACE', "$this Enabled", get_class($this)); + if ($this -> enabled) + LSlog :: log_trace("$this Enabled", get_class($this)); } /** diff --git a/src/includes/class/class.LSlog_file.php b/src/includes/class/class.LSlog_file.php index 13bf602b..5def018b 100644 --- a/src/includes/class/class.LSlog_file.php +++ b/src/includes/class/class.LSlog_file.php @@ -43,7 +43,8 @@ class LSlog_file extends LSlog_handler { $this -> path = self :: getConfig('path', LSlog :: getConfig('filename', 'tmp/LS.log')); if (substr($this -> path, 0, 1) != '/') $this -> path = LS_ROOT_DIR."/".$this -> path; - $this -> logging('TRACE', "$this Enabled", get_class($this)); + if ($this -> enabled) + LSlog :: log_trace("$this Enabled", get_class($this)); } /** diff --git a/src/includes/class/class.LSlog_handler.php b/src/includes/class/class.LSlog_handler.php index bd152fbd..00b42299 100644 --- a/src/includes/class/class.LSlog_handler.php +++ b/src/includes/class/class.LSlog_handler.php @@ -87,6 +87,32 @@ class LSlog_handler { ); } + /** + * Get handler info + * + * @param[in] $key string The info name + * + * @retval mixed The info value + **/ + public function __get($key) { + switch ($key) { + case 'enabled': + return $this -> getConfig('enabled', true, 'bool'); + case 'format': + if (php_sapi_name() == "cli") + $format = $this -> getConfig('cli_format', $this -> default_cli_format, 'string'); + else + $format = $this -> getConfig('format', $this -> default_format, 'string'); + // Add datetime prefix (if enabled) + if ($this -> getConfig('datetime_prefix', $this -> default_datetime_prefix, 'boolean')) { + $format = date($this -> getConfig('datetime_format', $this -> default_datetime_format, 'string'))." - $format"; + } + return $format; + } + // Unknown key, log warning + LSlog :: log_warning("$this -> __get($key): invalid property requested\n".LSlog :: get_debug_backtrace_context()); + } + /** * Check system compatibility with this handler * @@ -184,16 +210,8 @@ class LSlog_handler { **/ protected function format($level, $message, $logger=null) { global $argv; - if (php_sapi_name() == "cli") - $format = $this -> getConfig('cli_format', $this -> default_cli_format, 'string'); - else - $format = $this -> getConfig('format', $this -> default_format, 'string'); - // Add datetime prefix (if enabled) - if ($this -> getConfig('datetime_prefix', $this -> default_datetime_prefix, 'boolean')) { - $format = date($this -> getConfig('datetime_format', $this -> default_datetime_format, 'string'))." - $format"; - } return getFData( - $format, + $this -> format, array( 'level' => $level, 'message' => $message, diff --git a/src/includes/class/class.LSlog_syslog.php b/src/includes/class/class.LSlog_syslog.php index 02ad0710..7790ac30 100644 --- a/src/includes/class/class.LSlog_syslog.php +++ b/src/includes/class/class.LSlog_syslog.php @@ -59,7 +59,8 @@ class LSlog_syslog extends LSlog_handler { public function __construct($config) { parent :: __construct($config); $this -> priority = static :: getConfig('priority'); - $this -> logging('TRACE', "$this Enabled", get_class($this)); + if ($this -> enabled) + LSlog :: log_trace("$this Enabled", get_class($this)); } /** diff --git a/src/includes/class/class.LSlog_system.php b/src/includes/class/class.LSlog_system.php index 9b89aa1a..1eb0324a 100644 --- a/src/includes/class/class.LSlog_system.php +++ b/src/includes/class/class.LSlog_system.php @@ -30,6 +30,19 @@ class LSlog_system extends LSlog_handler { // Default datetime prefix (enabled/disabled) protected $default_datetime_prefix = false; + /** + * Constructor + * + * @param[in] $config array The handler configuration + * + * @retval void + **/ + public function __construct($config) { + parent :: __construct($config); + if ($this -> enabled) + LSlog :: log_trace("$this Enabled", get_class($this)); + } + /** * Log a message *