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

View File

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

View File

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

View File

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

View File

@ -484,19 +484,21 @@ function dol_strtoupper($utf8_string)
* *
* @param string $message Line to log. Ne doit pas etre traduit si level = LOG_ERR * @param string $message Line to log. Ne doit pas etre traduit si level = LOG_ERR
* @param int $level Log level * @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 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 * 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 * @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 syslog module enabled
if (empty($conf->syslog->enabled)) return false; if (empty($conf->syslog->enabled)) return false;
if (!defined('SYSLOG_HANDLERS') || !constant('SYSLOG_HANDLERS')) return false; if (! empty($level))
{
// Test log level // Test log level
$logLevels = array( LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG); $logLevels = array( LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG);
if (!in_array($level, $logLevels)) if (!in_array($level, $logLevels))
@ -540,6 +542,16 @@ function dol_syslog($message, $level = LOG_INFO)
{ {
$loghandlerinstance->export($data); $loghandlerinstance->export($data);
} }
unset($data);
}
if (! empty($ident))
{
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 class LogHandler
{ {
protected $ident=0;
/** /**
* Content of the info tooltip. * Content of the info tooltip.
* *
@ -58,4 +61,15 @@ class LogHandler
{ {
return array(); 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 class mod_syslog_file extends LogHandler implements LogHandlerInterface
{ {
/** /**
* Return name of logger * Return name of logger
* *
@ -106,12 +107,15 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface
* Export the message * Export the message
* *
* @param array $content Array containing the info about 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 * @return void
*/ */
public function export($content) public function export($content)
{ {
$logfile = $this->getFilename(); $logfile = $this->getFilename();
if ($ident) $this->ident+=$ident;
if (defined("SYSLOG_FILE_NO_ERROR")) $filefd = @fopen($logfile, 'a+'); if (defined("SYSLOG_FILE_NO_ERROR")) $filefd = @fopen($logfile, 'a+');
else $filefd = fopen($logfile, 'a+'); else $filefd = fopen($logfile, 'a+');
@ -133,7 +137,7 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface
LOG_DEBUG => 'DEBUG' 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"); fwrite($filefd, $message."\n");
fclose($filefd); fclose($filefd);