From d578d0be2befb2bfb19519c652ebea93016c597a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 2 Dec 2012 14:33:24 +0100 Subject: [PATCH] Perf: Log optimize. Saved 10KB of memory. --- htdocs/core/class/conf.class.php | 22 +++++++++++++++++ htdocs/core/lib/functions.lib.php | 39 ++++--------------------------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 1c1461436d5..5c8954d6664 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -60,6 +60,7 @@ class Conf public $societe_modules = array(); var $logbuffer = array(); + var $loghandlers = array(); //! To store properties of multi-company public $multicompany; @@ -456,6 +457,27 @@ class Conf { if (is_object($mc)) $mc->setValues($this); } + + // We init log handlers + if (defined('SYSLOG_HANDLERS')) $handlers = json_decode(constant('SYSLOG_HANDLERS')); + else $handlers = array(); + foreach ($handlers as $handler) + { + $file = DOL_DOCUMENT_ROOT.'/core/modules/syslog/'.$handler.'.php'; + if (!file_exists($file)) + { + throw new Exception('Missing log handler file '.$handler.'.php'); + } + + require_once $file; + $loghandlerinstance = new $handler(); + if (!$loghandlerinstance instanceof LogHandlerInterface) + { + throw new Exception('Log handler does not extend LogHandlerInterface'); + } + + $this->loghandlers[]=$loghandlerinstance; + } } } diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index a163d5cd726..b5acc261188 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -497,22 +497,12 @@ function dol_syslog($message, $level = LOG_INFO) if (!defined('SYSLOG_HANDLERS') || !constant('SYSLOG_HANDLERS')) return false; - $logLevels = array( - LOG_EMERG, - LOG_ALERT, - LOG_CRIT, - LOG_ERR, - LOG_WARNING, - LOG_NOTICE, - LOG_INFO, - LOG_DEBUG - ); - + // Test log level + $logLevels = array( LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG); if (!in_array($level, $logLevels)) { throw new Exception('Incorrect log level'); } - if ($level > $conf->global->SYSLOG_LEVEL) return false; // If adding log inside HTML page is required @@ -545,29 +535,10 @@ function dol_syslog($message, $level = LOG_INFO) // This is when PHP session is ran outside a web server, like from Linux command line (Not always defined, but usefull if OS defined it). else if (! empty($_SERVER['LOGNAME'])) $data['ip'] = '???@'.$_SERVER['LOGNAME']; - //We load SYSLOG handlers - if (defined('SYSLOG_HANDLERS')) $handlers = json_decode(SYSLOG_HANDLERS); - else $handlers = array(); - - foreach ($handlers as $handler) + // Loop on each log handler and send output + foreach ($conf->loghandlers as $loghandlerinstance) { - $file = DOL_DOCUMENT_ROOT.'/core/modules/syslog/'.$handler.'.php'; - - if (!file_exists($file)) - { - throw new Exception('Missing log handler'); - } - - require_once $file; - - $class = new $handler(); - - if (!$class instanceof LogHandlerInterface) - { - throw new Exception('Log handler does not extend LogHandlerInterface'); - } - - $class->export($data); + $loghandlerinstance->export($data); } }