Perf: Log optimize. Saved 10KB of memory.

This commit is contained in:
Laurent Destailleur 2012-12-02 14:33:24 +01:00
parent d428549e15
commit d578d0be2b
2 changed files with 27 additions and 34 deletions

View File

@ -60,6 +60,7 @@ class Conf
public $societe_modules = array(); public $societe_modules = array();
var $logbuffer = array(); var $logbuffer = array();
var $loghandlers = array();
//! To store properties of multi-company //! To store properties of multi-company
public $multicompany; public $multicompany;
@ -456,6 +457,27 @@ class Conf
{ {
if (is_object($mc)) $mc->setValues($this); 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;
}
} }
} }

View File

@ -497,22 +497,12 @@ function dol_syslog($message, $level = LOG_INFO)
if (!defined('SYSLOG_HANDLERS') || !constant('SYSLOG_HANDLERS')) return false; if (!defined('SYSLOG_HANDLERS') || !constant('SYSLOG_HANDLERS')) return false;
$logLevels = array( // Test log level
LOG_EMERG, $logLevels = array( LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG);
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG
);
if (!in_array($level, $logLevels)) if (!in_array($level, $logLevels))
{ {
throw new Exception('Incorrect log level'); throw new Exception('Incorrect log level');
} }
if ($level > $conf->global->SYSLOG_LEVEL) return false; if ($level > $conf->global->SYSLOG_LEVEL) return false;
// If adding log inside HTML page is required // 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). // 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']; else if (! empty($_SERVER['LOGNAME'])) $data['ip'] = '???@'.$_SERVER['LOGNAME'];
//We load SYSLOG handlers // Loop on each log handler and send output
if (defined('SYSLOG_HANDLERS')) $handlers = json_decode(SYSLOG_HANDLERS); foreach ($conf->loghandlers as $loghandlerinstance)
else $handlers = array();
foreach ($handlers as $handler)
{ {
$file = DOL_DOCUMENT_ROOT.'/core/modules/syslog/'.$handler.'.php'; $loghandlerinstance->export($data);
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);
} }
} }