Improved logging.

Added "[logs]write_on_timeout_only" settings
This commit is contained in:
RainLoop Team 2014-05-08 16:36:46 +04:00
parent 1250efa445
commit 23c299ac9e
7 changed files with 96 additions and 47 deletions

View file

@ -33,6 +33,11 @@ abstract class Driver
*/
protected $bTypedPrefix;
/**
* @var int
*/
private $iWriteOnTimeoutOnly;
/**
* @var bool
*/
@ -58,6 +63,7 @@ abstract class Driver
$this->bTimePrefix = true;
$this->bTypedPrefix = true;
$this->iWriteOnTimeoutOnly = 0;
$this->bWriteOnErrorOnly = false;
$this->bFlushCache = false;
$this->aCache = array();
@ -94,6 +100,22 @@ abstract class Driver
return $this;
}
/**
* @param int $iTimeout
*
* @return \MailSo\Log\Driver
*/
public function WriteOnTimeoutOnly($iTimeout)
{
$this->iWriteOnTimeoutOnly = (int) $iTimeout;
if (0 > $this->iWriteOnTimeoutOnly)
{
$this->iWriteOnTimeoutOnly = 0;
}
return $this;
}
/**
* @return \MailSo\Log\Driver
*/
@ -104,7 +126,7 @@ abstract class Driver
}
/**
* @param string|array $sDesc
* @param string|array $mDesc
* @return bool
*/
abstract protected function writeImplementation($mDesc);
@ -120,16 +142,16 @@ abstract class Driver
/**
* @param string $sTimePrefix
* @param string $sDesc
* @param int $iDescType = \MailSo\Log\Enumerations\Type::INFO
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param array $sName = ''
*
* @return string
*/
protected function loggerLineImplementation($sTimePrefix, $sDesc,
$iDescType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
$iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
{
return ($this->bTimePrefix ? '['.$sTimePrefix.'] ' : '').
($this->bTypedPrefix ? $this->getTypedPrefix($iDescType, $sName) : '').
($this->bTypedPrefix ? $this->getTypedPrefix($iType, $sName) : '').
$sDesc;
}
@ -152,46 +174,66 @@ abstract class Driver
}
/**
* @param int $iDescType
* @param int $iType
* @param string $sName = ''
*
* @return string
*/
protected function getTypedPrefix($iDescType, $sName = '')
protected function getTypedPrefix($iType, $sName = '')
{
$sName = 0 < \strlen($sName) ? $sName : $this->sName;
return isset($this->aPrefixes[$iDescType]) ? $sName.$this->aPrefixes[$iDescType].': ' : '';
return isset($this->aPrefixes[$iType]) ? $sName.$this->aPrefixes[$iType].': ' : '';
}
/**
* @final
* @param string $sDesc
* @param int $iDescType = \MailSo\Log\Enumerations\Type::INFO
* @param array $sName = ''
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param string $sName = ''
*
* @return bool
*/
final public function Write($sDesc, $iDescType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
{
if ($this->bWriteOnErrorOnly && !$this->bFlushCache)
$bResult = true;
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
{
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iDescType, $sName);
if (\in_array($iDescType, array(
if ($this->bWriteOnErrorOnly && \in_array($iType, array(
\MailSo\Log\Enumerations\Type::NOTICE,
\MailSo\Log\Enumerations\Type::WARNING,
\MailSo\Log\Enumerations\Type::ERROR
)))
{
$this->bFlushCache = true;
return $this->writeImplementation($this->aCache);
}
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), '--- FlushCache: WriteOnErrorOnly', \MailSo\Log\Enumerations\Type::INFO, 'LOGS');
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
return true;
$this->bFlushCache = true;
$bResult = $this->writeImplementation($this->aCache);
$this->aCache = array();
}
else if (0 < $this->iWriteOnTimeoutOnly && \time() - APP_START_TIME > $this->iWriteOnTimeoutOnly)
{
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), '--- FlushCache: WriteOnTimeoutOnly['.
(\time() - APP_START_TIME).'/'.$this->iWriteOnTimeoutOnly.']', \MailSo\Log\Enumerations\Type::NOTE, 'LOGS');
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
$this->bFlushCache = true;
$bResult = $this->writeImplementation($this->aCache);
$this->aCache = array();
}
else
{
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
}
}
else
{
$bResult = $this->writeImplementation(
$this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName));
}
return $this->writeImplementation(
$this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iDescType, $sName));
return $bResult;
}
/**
@ -209,7 +251,7 @@ abstract class Driver
*/
final public function WriteEmptyLine()
{
if ($this->bWriteOnErrorOnly && !$this->bFlushCache)
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
{
$this->aCache[] = '';
}

View file

@ -45,15 +45,15 @@ class Callback extends \MailSo\Log\Driver
}
/**
* @param string|array $sDesc
* @param string|array $mDesc
*
* @return bool
*/
protected function writeImplementation($sDesc)
protected function writeImplementation($mDesc)
{
if ($this->fWriteCallback)
{
\call_user_func_array($this->fWriteCallback, array($sDesc));
\call_user_func_array($this->fWriteCallback, array($mDesc));
}
return true;

View file

@ -51,12 +51,12 @@ class Inline extends \MailSo\Log\Driver
*/
protected function writeImplementation($mDesc)
{
if (is_array($mDesc))
if (\is_array($mDesc))
{
if ($this->bHtmlEncodeSpecialChars)
{
$mDesc = array_map(function ($sItem) {
$sItem = \htmlspecialchars($mDesc);
$mDesc = \array_map(function ($sItem) {
return \htmlspecialchars($sItem);
}, $mDesc);
}

View file

@ -83,7 +83,7 @@ class Logger extends \MailSo\Base\Collection
}
/**
* @param int $iDescType
* @param int $iType
*
* @return \MailSo\Log\Logger
*/
@ -95,7 +95,7 @@ class Logger extends \MailSo\Base\Collection
}
/**
* @param int $iDescType
* @param int $iType
*
* @return \MailSo\Log\Logger
*/
@ -141,15 +141,15 @@ class Logger extends \MailSo\Base\Collection
/**
* @param string $sDesc
* @param int $iDescType = \MailSo\Log\Enumerations\Type::INFO
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param string $sName = ''
* @param bool $bSearchWords = false
*
* @return bool
*/
public function Write($sDesc, $iDescType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', $bSearchWords = false)
public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', $bSearchWords = false)
{
if (isset($this->aForbiddenTypes[$iDescType]) && true === $this->aForbiddenTypes[$iDescType])
if (isset($this->aForbiddenTypes[$iType]) && true === $this->aForbiddenTypes[$iType])
{
return true;
}
@ -168,7 +168,7 @@ class Logger extends \MailSo\Base\Collection
$aLoggers =& $this->GetAsArray();
foreach ($aLoggers as /* @var $oLogger \MailSo\Log\Driver */ $oLogger)
{
$iResult &= $oLogger->Write($sDesc, $iDescType, $sName);
$iResult &= $oLogger->Write($sDesc, $iType, $sName);
}
return (bool) $iResult;
@ -176,26 +176,26 @@ class Logger extends \MailSo\Base\Collection
/**
* @param mixed $oValue
* @param int $iDescType = \MailSo\Log\Enumerations\Type::INFO
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param string $sName = ''
* @param bool $bSearchSecretWords = false
*
* @return bool
*/
public function WriteDump($oValue, $iDescType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', $bSearchSecretWords = false)
public function WriteDump($oValue, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', $bSearchSecretWords = false)
{
return $this->Write(\print_r($oValue, true), $iDescType, $sName, $bSearchSecretWords);
return $this->Write(\print_r($oValue, true), $iType, $sName, $bSearchSecretWords);
}
/**
* @param \Exception $oException
* @param int $iDescType = \MailSo\Log\Enumerations\Type::NOTICE
* @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE
* @param string $sName = ''
* @param bool $bSearchSecretWords = true
*
* @return bool
*/
public function WriteException($oException, $iDescType = \MailSo\Log\Enumerations\Type::NOTICE, $sName = '', $bSearchSecretWords = true)
public function WriteException($oException, $iType = \MailSo\Log\Enumerations\Type::NOTICE, $sName = '', $bSearchSecretWords = true)
{
if ($oException instanceof \Exception)
{
@ -206,28 +206,28 @@ class Logger extends \MailSo\Base\Collection
$oException->__LOGINNED__ = true;
return $this->Write((string) $oException, $iDescType, $sName, $bSearchSecretWords);
return $this->Write((string) $oException, $iType, $sName, $bSearchSecretWords);
}
return false;
}
/**
* @param \Exception $oException
* @param int $iDescType = \MailSo\Log\Enumerations\Type::NOTICE
* @param mixed $mData
* @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE
* @param string $sName = ''
* @param bool $bSearchSecretWords = true
*
* @return bool
*/
public function WriteMixed($mData, $iDescType = null, $sName = '', $bSearchSecretWords = true)
public function WriteMixed($mData, $iType = null, $sName = '', $bSearchSecretWords = true)
{
$iType = null === $iDescType ? \MailSo\Log\Enumerations\Type::INFO : $iDescType;
$iType = null === $iType ? \MailSo\Log\Enumerations\Type::INFO : $iType;
if (\is_array($mData) || \is_object($mData))
{
if ($mData instanceof \Exception)
{
$iType = null === $iDescType ? \MailSo\Log\Enumerations\Type::NOTICE : $iType;
$iType = null === $iType ? \MailSo\Log\Enumerations\Type::NOTICE : $iType;
return $this->WriteException($mData, $iType, $sName, $bSearchSecretWords);
}
else

View file

@ -758,8 +758,11 @@ class Actions
@mkdir($sLogFileDir, 0755, true);
}
$this->oLogger->Add(\MailSo\Log\Drivers\File::NewInstance($sLogFileFullPath)
->WriteOnErrorOnly($this->Config()->Get('logs', 'write_on_error_only', true)));
$this->oLogger->Add(
\MailSo\Log\Drivers\File::NewInstance($sLogFileFullPath)
->WriteOnErrorOnly($this->Config()->Get('logs', 'write_on_error_only', true))
->WriteOnTimeoutOnly($this->Config()->Get('logs', 'write_on_timeout_only', 30))
);
if (!$this->Config()->Get('debug', 'enable', false))
{
@ -3866,6 +3869,7 @@ class Actions
*/
public function DoMessageList()
{
// sleep(5);
// throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CantGetMessageList);
$sFolder = '';

View file

@ -142,6 +142,7 @@ Values:
'enable' => array(false, 'Enable logging'),
'write_on_error_only' => array(false, 'Logs entire request only if error occured'),
'write_on_timeout_only' => array(0, 'Logs entire request only if request timeout (in seconds) occured.'),
'filename' => array('log-{date:Y-m-d}.txt',
'Log filename.

View file

@ -3,8 +3,10 @@
if (defined('APP_VERSION'))
{
define('APP_START', microtime(true));
define('APP_INDEX_FILE', 'index.php');
define('APP_START_TIME', time());
define('APP_REQUEST_RND', md5(APP_START.rand(10000, 99999).APP_START));
define('APP_VERSION_ROOT_PATH', APP_INDEX_ROOT_PATH.'rainloop/v/'.APP_VERSION.'/');
define('APP_INDEX_FILE', 'index.php');
if (function_exists('date_default_timezone_set'))
{