New: Log handler can now manage indent on database transactions

This commit is contained in:
Laurent Destailleur 2012-12-02 15:20:45 +01:00
parent d578d0be2b
commit fd22705de4
7 changed files with 96 additions and 52 deletions

View File

@ -26,8 +26,7 @@
/**
* \class DoliDBMysql
* \brief Class to manage Dolibarr database access for a Mysql database
* Class to manage Dolibarr database access for a Mysql database
*/
class DoliDBMysql
{
@ -291,12 +290,14 @@ class DoliDBMysql
{
$this->transaction_opened++;
dol_syslog("BEGIN Transaction",LOG_DEBUG);
dol_syslog('',0,1);
}
return $ret;
}
else
{
$this->transaction_opened++;
dol_syslog('',0,1);
return 1;
}
}
@ -309,6 +310,7 @@ class DoliDBMysql
*/
function commit($log='')
{
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("COMMIT");
@ -334,6 +336,7 @@ class DoliDBMysql
*/
function rollback($log='')
{
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("ROLLBACK");

View File

@ -26,8 +26,7 @@
/**
* \class DoliDBMysqli
* \brief Class to manage Dolibarr database access for a Mysql database
* Class to manage Dolibarr database access for a Mysql database
*/
class DoliDBMysqli
{
@ -286,12 +285,14 @@ class DoliDBMysqli
{
$this->transaction_opened++;
dol_syslog("BEGIN Transaction",LOG_DEBUG);
dol_syslog('',0,1);
}
return $ret;
}
else
{
$this->transaction_opened++;
dol_syslog('',0,1);
return 1;
}
}
@ -304,7 +305,8 @@ class DoliDBMysqli
*/
function commit($log='')
{
if ($this->transaction_opened<=1)
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("COMMIT");
if ($ret)
@ -329,7 +331,8 @@ class DoliDBMysqli
*/
function rollback($log='')
{
if ($this->transaction_opened<=1)
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("ROLLBACK");
$this->transaction_opened=0;

View File

@ -474,12 +474,14 @@ class DoliDBPgsql
{
$this->transaction_opened++;
dol_syslog("BEGIN Transaction",LOG_DEBUG);
dol_syslog('',0,1);
}
return $ret;
}
else
{
$this->transaction_opened++;
dol_syslog('',0,1);
return 1;
}
}
@ -492,6 +494,7 @@ class DoliDBPgsql
*/
function commit($log='')
{
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("COMMIT;");
@ -516,6 +519,7 @@ class DoliDBPgsql
*/
function rollback()
{
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("ROLLBACK;");

View File

@ -416,12 +416,14 @@ class DoliDBSqlite
{
$this->transaction_opened++;
dol_syslog("BEGIN Transaction",LOG_DEBUG);
dol_syslog('',0,1);
}
return $ret;
}
else
{
$this->transaction_opened++;
dol_syslog('',0,1);
return 1;
}
}
@ -434,7 +436,8 @@ class DoliDBSqlite
*/
function commit($log='')
{
if ($this->transaction_opened<=1)
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("COMMIT");
if ($ret)
@ -445,8 +448,8 @@ class DoliDBSqlite
return $ret;
}
else
{
$this->transaction_opened--;
{
$this->transaction_opened--;
return 1;
}
}
@ -459,7 +462,8 @@ class DoliDBSqlite
*/
function rollback($log='')
{
if ($this->transaction_opened<=1)
dol_syslog('',0,-1);
if ($this->transaction_opened<=1)
{
$ret=$this->query("ROLLBACK");
$this->transaction_opened=0;

View File

@ -484,61 +484,73 @@ function dol_strtoupper($utf8_string)
*
* @param string $message Line to log. Ne doit pas etre traduit si level = LOG_ERR
* @param int $level Log level
* 0=Show nothing
* On Windows LOG_ERR=4, LOG_WARNING=5, LOG_NOTICE=LOG_INFO=6, LOG_DEBUG=6 si define_syslog_variables ou PHP 5.3+, 7 si dolibarr
* On Linux LOG_ERR=3, LOG_WARNING=4, LOG_INFO=6, LOG_DEBUG=7
* @param int $ident 1=Increase ident of 1, -1=Decrease ident of 1
* @return void
*/
function dol_syslog($message, $level = LOG_INFO)
function dol_syslog($message, $level = LOG_INFO, $ident = 0)
{
global $conf, $user, $langs;
global $conf, $user;
// If syslog module enabled
if (empty($conf->syslog->enabled)) return false;
if (!defined('SYSLOG_HANDLERS') || !constant('SYSLOG_HANDLERS')) return false;
// 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))
if (! empty($level))
{
throw new Exception('Incorrect log level');
}
if ($level > $conf->global->SYSLOG_LEVEL) return false;
// 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
if (! empty($_REQUEST['logtohtml']) && ! empty($conf->global->MAIN_LOGTOHTML))
{
$conf->logbuffer[] = dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".$message;
// If adding log inside HTML page is required
if (! empty($_REQUEST['logtohtml']) && ! empty($conf->global->MAIN_LOGTOHTML))
{
$conf->logbuffer[] = dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".$message;
}
// If enable html log tag enabled and url parameter log defined, we show output log on HTML comments
if (! empty($conf->global->MAIN_ENABLE_LOG_HTML) && ! empty($_GET["log"]))
{
print "\n\n<!-- Log start\n";
print $message."\n";
print "Log end -->\n";
}
$data = array(
'message' => $message,
'script' => (isset($_SERVER['PHP_SELF'])? basename($_SERVER['PHP_SELF'],'.php') : false),
'level' => $level,
'user' => ((is_object($user) && $user->id) ? $user->login : false),
'ip' => false
);
if (! empty($_SERVER["REMOTE_ADDR"])) $data['ip'] = $_SERVER['REMOTE_ADDR'];
// This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
else if (! empty($_SERVER['SERVER_ADDR'])) $data['ip'] = $_SERVER['SERVER_ADDR'];
// This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but useful if OS defined it).
else if (! empty($_SERVER['COMPUTERNAME'])) $data['ip'] = $_SERVER['COMPUTERNAME'].(empty($_SERVER['USERNAME'])?'':'@'.$_SERVER['USERNAME']);
// 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'];
// Loop on each log handler and send output
foreach ($conf->loghandlers as $loghandlerinstance)
{
$loghandlerinstance->export($data);
}
unset($data);
}
// If enable html log tag enabled and url parameter log defined, we show output log on HTML comments
if (! empty($conf->global->MAIN_ENABLE_LOG_HTML) && ! empty($_GET["log"]))
if (! empty($ident))
{
print "\n\n<!-- Log start\n";
print $message."\n";
print "Log end -->\n";
}
$data = array(
'message' => $message,
'script' => (isset($_SERVER['PHP_SELF'])? basename($_SERVER['PHP_SELF'],'.php') : false),
'level' => $level,
'user' => ((is_object($user) && $user->id) ? $user->login : false),
'ip' => false
);
if (! empty($_SERVER["REMOTE_ADDR"])) $data['ip'] = $_SERVER['REMOTE_ADDR'];
// This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
else if (! empty($_SERVER['SERVER_ADDR'])) $data['ip'] = $_SERVER['SERVER_ADDR'];
// This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but useful if OS defined it).
else if (! empty($_SERVER['COMPUTERNAME'])) $data['ip'] = $_SERVER['COMPUTERNAME'].(empty($_SERVER['USERNAME'])?'':'@'.$_SERVER['USERNAME']);
// 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'];
// Loop on each log handler and send output
foreach ($conf->loghandlers as $loghandlerinstance)
{
$loghandlerinstance->export($data);
foreach ($conf->loghandlers as $loghandlerinstance)
{
$loghandlerinstance->setIdent($ident);
}
}
}

View File

@ -7,6 +7,9 @@ require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandlerInterface.php';
*/
class LogHandler
{
protected $ident=0;
/**
* Content of the info tooltip.
*
@ -58,4 +61,15 @@ class LogHandler
{
return array();
}
/**
* Set current ident.
*
* @param int $ident 1=Increase ident of 1, -1=Decrease ident of 1
* @return void
*/
public function setIdent($ident)
{
$this->ident+=$ident;
}
}

View File

@ -7,6 +7,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
*/
class mod_syslog_file extends LogHandler implements LogHandlerInterface
{
/**
* Return name of logger
*
@ -106,12 +107,15 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface
* Export the message
*
* @param array $content Array containing the info about the message
* @param int $ident 1=Increase ident of 1, -1=Decrease ident of 1
* @return void
*/
public function export($content)
{
$logfile = $this->getFilename();
if ($ident) $this->ident+=$ident;
if (defined("SYSLOG_FILE_NO_ERROR")) $filefd = @fopen($logfile, 'a+');
else $filefd = fopen($logfile, 'a+');
@ -133,7 +137,7 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface
LOG_DEBUG => 'DEBUG'
);
$message = dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".sprintf("%-5s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".$content['message'];
$message = dol_print_date(time(),"%Y-%m-%d %H:%M:%S")." ".sprintf("%-5s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".($this->ident>0?str_pad('',$this->ident,' '):'').$content['message'];
fwrite($filefd, $message."\n");
fclose($filefd);