Added "write_on_php_error_only" setting

This commit is contained in:
RainLoop Team 2014-09-03 23:53:24 +04:00
parent 9bf5847983
commit 9bde3fe5dd
7 changed files with 125 additions and 61 deletions

View file

@ -48,6 +48,11 @@ abstract class Driver
*/
private $bWriteOnErrorOnly;
/**
* @var bool
*/
private $bWriteOnPhpErrorOnly;
/**
* @var bool
*/
@ -71,9 +76,10 @@ abstract class Driver
$this->iWriteOnTimeoutOnly = 0;
$this->bWriteOnErrorOnly = false;
$this->bWriteOnPhpErrorOnly = false;
$this->bFlushCache = false;
$this->aCache = array();
$this->aPrefixes = array(
\MailSo\Log\Enumerations\Type::INFO => '[DATA]',
\MailSo\Log\Enumerations\Type::SECURE => '[SECURE]',
@ -84,6 +90,10 @@ abstract class Driver
\MailSo\Log\Enumerations\Type::NOTICE => '[NOTICE]',
\MailSo\Log\Enumerations\Type::WARNING => '[WARNING]',
\MailSo\Log\Enumerations\Type::ERROR => '[ERROR]',
\MailSo\Log\Enumerations\Type::NOTICE_PHP => '[NOTICE]',
\MailSo\Log\Enumerations\Type::WARNING_PHP => '[WARNING]',
\MailSo\Log\Enumerations\Type::ERROR_PHP => '[ERROR]',
);
}
@ -107,7 +117,7 @@ abstract class Driver
/**
* @param bool $bValue
*
*
* @return \MailSo\Log\Driver
*/
public function WriteOnErrorOnly($bValue)
@ -116,6 +126,17 @@ abstract class Driver
return $this;
}
/**
* @param bool $bValue
*
* @return \MailSo\Log\Driver
*/
public function WriteOnPhpErrorOnly($bValue)
{
$this->bWriteOnPhpErrorOnly = !!$bValue;
return $this;
}
/**
* @param int $iTimeout
*
@ -128,7 +149,7 @@ abstract class Driver
{
$this->iWriteOnTimeoutOnly = 0;
}
return $this;
}
@ -214,15 +235,31 @@ abstract class Driver
final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
{
$bResult = true;
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
{
if ($this->bWriteOnErrorOnly && \in_array($iType, array(
$bErrorPhp = false;
$bError = $this->bWriteOnErrorOnly && \in_array($iType, array(
\MailSo\Log\Enumerations\Type::NOTICE,
\MailSo\Log\Enumerations\Type::NOTICE_PHP,
\MailSo\Log\Enumerations\Type::WARNING,
\MailSo\Log\Enumerations\Type::ERROR
)))
\MailSo\Log\Enumerations\Type::WARNING_PHP,
\MailSo\Log\Enumerations\Type::ERROR,
\MailSo\Log\Enumerations\Type::ERROR_PHP
));
if (!$bError)
{
$sFlush = '--- FlushLogCache: WriteOnErrorOnly';
$bErrorPhp = $this->bWriteOnPhpErrorOnly && \in_array($iType, array(
\MailSo\Log\Enumerations\Type::NOTICE_PHP,
\MailSo\Log\Enumerations\Type::WARNING_PHP,
\MailSo\Log\Enumerations\Type::ERROR_PHP
));
}
if ($bError || $bErrorPhp)
{
$sFlush = '--- FlushLogCache: '.($bError ? 'WriteOnErrorOnly' : 'WriteOnPhpErrorOnly');
if (isset($this->aCache[0]) && empty($this->aCache[0]))
{
$this->aCache[0] = $sFlush;
@ -255,7 +292,7 @@ abstract class Driver
$this->aCache[] = '--- FlushLogCache: Trigger';
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
$this->bFlushCache = true;
$bResult = $this->writeImplementation($this->aCache);
$this->aCache = array();
@ -289,7 +326,7 @@ abstract class Driver
*/
final public function WriteEmptyLine()
{
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
{
$this->aCache[] = '';
}

View file

@ -69,13 +69,13 @@ class Inline extends \MailSo\Log\Driver
return true;
}
/**
* @return bool
*/
protected function clearImplementation()
{
if (\defined('PHP_SAPI') && 'cli' === PHP_SAPI)
if (\defined('PHP_SAPI') && 'cli' === PHP_SAPI && \MailSo\Base\Utils::FunctionExistsAndEnabled('system'))
{
\system('clear');
}

View file

@ -18,4 +18,8 @@ class Type
const TIME = 6;
const MEMORY = 7;
const TIME_DELTA = 8;
const NOTICE_PHP = 11;
const WARNING_PHP = 12;
const ERROR_PHP = 13;
}

View file

@ -28,6 +28,11 @@ class Logger extends \MailSo\Base\Collection
*/
private $bShowSecter;
/**
* @var bool
*/
private $bHideErrorNotices;
/**
* @access protected
*/
@ -39,7 +44,9 @@ class Logger extends \MailSo\Base\Collection
$this->aForbiddenTypes = array();
$this->aSecretWords = array();
$this->bShowSecter = false;
$this->bHideErrorNotices = false;
\set_error_handler(array(&$this, '__phpErrorHandler'));
\register_shutdown_function(array(&$this, '__loggerShutDown'));
}
@ -83,6 +90,14 @@ class Logger extends \MailSo\Base\Collection
return $sCache;
}
/**
* @return bool
*/
public function Ping()
{
return true;
}
/**
* @return bool
*/
@ -107,13 +122,23 @@ class Logger extends \MailSo\Base\Collection
/**
* @param bool $bShow
*
*
* @return \MailSo\Log\Logger
*/
public function SetShowSecter($bShow)
{
$this->bShowSecter = !!$bShow;
return $this;
}
/**
* @param bool $bValue
*
* @return \MailSo\Log\Logger
*/
public function HideErrorNotices($bValue)
{
$this->bHideErrorNotices = !!$bValue;
return $this;
}
@ -145,10 +170,36 @@ class Logger extends \MailSo\Base\Collection
public function RemoveForbiddenType($iType)
{
$this->aForbiddenTypes[$iType] = false;
return $this;
}
/**
* @param int $iErrNo
* @param string $sErrStr
* @param string $sErrFile
* @param int $iErrLine
*
* @return bool
*/
public function __phpErrorHandler($iErrNo, $sErrStr, $sErrFile, $iErrLine)
{
$iType = \MailSo\Log\Enumerations\Type::NOTICE_PHP;
switch ($iErrNo)
{
case E_USER_ERROR:
$iType = \MailSo\Log\Enumerations\Type::ERROR_PHP;
break;
case E_USER_WARNING:
$iType = \MailSo\Log\Enumerations\Type::WARNING_PHP;
break;
}
$this->Write($sErrFile.' [line:'.$iErrLine.', code:'.$iErrNo.']', $iType, 'PHP');
$this->Write('Error: '.$sErrStr, $iType, 'PHP');
return !!(\MailSo\Log\Enumerations\Type::NOTICE === $iType && $this->bHideErrorNotices);
}
/**
* @return void
*/
@ -164,7 +215,7 @@ class Logger extends \MailSo\Base\Collection
$this->Write('Memory peak usage: '.$aStatistic['php']['memory_get_peak_usage'],
\MailSo\Log\Enumerations\Type::MEMORY);
}
if (isset($aStatistic['time']))
{
$this->Write('Time delta: '.$aStatistic['time'], \MailSo\Log\Enumerations\Type::TIME_DELTA);
@ -179,7 +230,7 @@ class Logger extends \MailSo\Base\Collection
public function WriteEmptyLine()
{
$iResult = 1;
$aLoggers =& $this->GetAsArray();
foreach ($aLoggers as /* @var $oLogger \MailSo\Log\Driver */ &$oLogger)
{