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; private $bWriteOnErrorOnly;
/**
* @var bool
*/
private $bWriteOnPhpErrorOnly;
/** /**
* @var bool * @var bool
*/ */
@ -71,6 +76,7 @@ abstract class Driver
$this->iWriteOnTimeoutOnly = 0; $this->iWriteOnTimeoutOnly = 0;
$this->bWriteOnErrorOnly = false; $this->bWriteOnErrorOnly = false;
$this->bWriteOnPhpErrorOnly = false;
$this->bFlushCache = false; $this->bFlushCache = false;
$this->aCache = array(); $this->aCache = array();
@ -84,6 +90,10 @@ abstract class Driver
\MailSo\Log\Enumerations\Type::NOTICE => '[NOTICE]', \MailSo\Log\Enumerations\Type::NOTICE => '[NOTICE]',
\MailSo\Log\Enumerations\Type::WARNING => '[WARNING]', \MailSo\Log\Enumerations\Type::WARNING => '[WARNING]',
\MailSo\Log\Enumerations\Type::ERROR => '[ERROR]', \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]',
); );
} }
@ -116,6 +126,17 @@ abstract class Driver
return $this; return $this;
} }
/**
* @param bool $bValue
*
* @return \MailSo\Log\Driver
*/
public function WriteOnPhpErrorOnly($bValue)
{
$this->bWriteOnPhpErrorOnly = !!$bValue;
return $this;
}
/** /**
* @param int $iTimeout * @param int $iTimeout
* *
@ -214,15 +235,31 @@ abstract class Driver
final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '') final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
{ {
$bResult = true; $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,
\MailSo\Log\Enumerations\Type::NOTICE_PHP,
\MailSo\Log\Enumerations\Type::WARNING, \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])) if (isset($this->aCache[0]) && empty($this->aCache[0]))
{ {
$this->aCache[0] = $sFlush; $this->aCache[0] = $sFlush;
@ -289,7 +326,7 @@ abstract class Driver
*/ */
final public function WriteEmptyLine() final public function WriteEmptyLine()
{ {
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || 0 < $this->iWriteOnTimeoutOnly)) if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
{ {
$this->aCache[] = ''; $this->aCache[] = '';
} }

View file

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

View file

@ -18,4 +18,8 @@ class Type
const TIME = 6; const TIME = 6;
const MEMORY = 7; const MEMORY = 7;
const TIME_DELTA = 8; 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; private $bShowSecter;
/**
* @var bool
*/
private $bHideErrorNotices;
/** /**
* @access protected * @access protected
*/ */
@ -39,7 +44,9 @@ class Logger extends \MailSo\Base\Collection
$this->aForbiddenTypes = array(); $this->aForbiddenTypes = array();
$this->aSecretWords = array(); $this->aSecretWords = array();
$this->bShowSecter = false; $this->bShowSecter = false;
$this->bHideErrorNotices = false;
\set_error_handler(array(&$this, '__phpErrorHandler'));
\register_shutdown_function(array(&$this, '__loggerShutDown')); \register_shutdown_function(array(&$this, '__loggerShutDown'));
} }
@ -83,6 +90,14 @@ class Logger extends \MailSo\Base\Collection
return $sCache; return $sCache;
} }
/**
* @return bool
*/
public function Ping()
{
return true;
}
/** /**
* @return bool * @return bool
*/ */
@ -113,7 +128,17 @@ class Logger extends \MailSo\Base\Collection
public function SetShowSecter($bShow) public function SetShowSecter($bShow)
{ {
$this->bShowSecter = !!$bShow; $this->bShowSecter = !!$bShow;
return $this;
}
/**
* @param bool $bValue
*
* @return \MailSo\Log\Logger
*/
public function HideErrorNotices($bValue)
{
$this->bHideErrorNotices = !!$bValue;
return $this; return $this;
} }
@ -145,10 +170,36 @@ class Logger extends \MailSo\Base\Collection
public function RemoveForbiddenType($iType) public function RemoveForbiddenType($iType)
{ {
$this->aForbiddenTypes[$iType] = false; $this->aForbiddenTypes[$iType] = false;
return $this; 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 * @return void
*/ */

View file

@ -128,6 +128,8 @@ class Actions
$oConfig = $this->Config(); $oConfig = $this->Config();
$this->Plugins()->RunHook('filter.application-config', array(&$oConfig)); $this->Plugins()->RunHook('filter.application-config', array(&$oConfig));
$this->Logger()->Ping();
} }
/** /**
@ -726,8 +728,9 @@ class Actions
$this->oLogger->Add( $this->oLogger->Add(
\MailSo\Log\Drivers\File::NewInstance($sLogFileFullPath) \MailSo\Log\Drivers\File::NewInstance($sLogFileFullPath)
->WriteOnErrorOnly($this->Config()->Get('logs', 'write_on_error_only', true)) ->WriteOnErrorOnly($this->Config()->Get('logs', 'write_on_error_only', false))
->WriteOnTimeoutOnly($this->Config()->Get('logs', 'write_on_timeout_only', 30)) ->WriteOnPhpErrorOnly($this->Config()->Get('logs', 'write_on_php_error_only', false))
->WriteOnTimeoutOnly($this->Config()->Get('logs', 'write_on_timeout_only', 0))
); );
if (!$this->Config()->Get('debug', 'enable', false)) if (!$this->Config()->Get('debug', 'enable', false))
@ -1555,7 +1558,7 @@ class Actions
include_once 'Crypt/RSA.php'; include_once 'Crypt/RSA.php';
} }
\RainLoop\Service::$__HIDE_ERROR_NOTICES = true; $oLogger->HideErrorNotices(true);
$oRsa = new \Crypt_RSA(); $oRsa = new \Crypt_RSA();
$oRsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); $oRsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
@ -1575,7 +1578,7 @@ class Actions
$oLogger->Write('Invalid decrypted data', \MailSo\Log\Enumerations\Type::WARNING, 'RSA'); $oLogger->Write('Invalid decrypted data', \MailSo\Log\Enumerations\Type::WARNING, 'RSA');
} }
\RainLoop\Service::$__HIDE_ERROR_NOTICES = false; $oLogger->HideErrorNotices(false);
} }
else else
{ {
@ -1591,9 +1594,11 @@ class Actions
*/ */
public function DoGetPublicKey() public function DoGetPublicKey()
{ {
$oLogger = $this->Logger();
if ($this->Config()->Get('security', 'use_rsa_encryption', false)) if ($this->Config()->Get('security', 'use_rsa_encryption', false))
{ {
\RainLoop\Service::$__HIDE_ERROR_NOTICES = true; $oLogger->HideErrorNotices(true);
if (!\class_exists('Crypt_RSA')) if (!\class_exists('Crypt_RSA'))
{ {
@ -1613,14 +1618,14 @@ class Actions
$sHash = \md5($e->toHex().$n->toHex()); $sHash = \md5($e->toHex().$n->toHex());
\RainLoop\Service::$__HIDE_ERROR_NOTICES = false; $oLogger->HideErrorNotices(false);
return $this->DefaultResponse(__FUNCTION__, return $this->DefaultResponse(__FUNCTION__,
$this->Cacher()->Set(\RainLoop\KeyPathHelper::RsaCacherKey($sHash), $aKeys['privatekey']) ? $this->Cacher()->Set(\RainLoop\KeyPathHelper::RsaCacherKey($sHash), $aKeys['privatekey']) ?
array($sHash, $e->toHex(), $n->toHex()) : false); array($sHash, $e->toHex(), $n->toHex()) : false);
} }
} }
\RainLoop\Service::$__HIDE_ERROR_NOTICES = false; $oLogger->HideErrorNotices(false);
return $this->FalseResponse(__FUNCTION__); return $this->FalseResponse(__FUNCTION__);
} }

View file

@ -140,7 +140,8 @@ Values:
'enable' => array(false, 'Enable logging'), 'enable' => array(false, 'Enable logging'),
'write_on_error_only' => array(false, 'Logs entire request only if error occured'), 'write_on_error_only' => array(false, 'Logs entire request only if error occured (php requred)'),
'write_on_php_error_only' => array(false, 'Logs entire request only if php error occured'),
'write_on_timeout_only' => array(0, 'Logs entire request only if request timeout (in seconds) occured.'), 'write_on_timeout_only' => array(0, 'Logs entire request only if request timeout (in seconds) occured.'),
'hide_passwords' => array(true, 'Required for development purposes only. 'hide_passwords' => array(true, 'Required for development purposes only.

View file

@ -4,11 +4,6 @@ namespace RainLoop;
class Service class Service
{ {
/**
* @var bool
*/
static $__HIDE_ERROR_NOTICES = false;
/** /**
* @var \MailSo\Base\Http * @var \MailSo\Base\Http
*/ */
@ -32,8 +27,6 @@ class Service
$this->oHttp = \MailSo\Base\Http::SingletonInstance(); $this->oHttp = \MailSo\Base\Http::SingletonInstance();
$this->oActions = \RainLoop\Api::Actions(); $this->oActions = \RainLoop\Api::Actions();
\set_error_handler(array(&$this, 'LogPhpErrorHandler'));
$this->oServiceActions = new \RainLoop\ServiceActions($this->oHttp, $this->oActions); $this->oServiceActions = new \RainLoop\ServiceActions($this->oHttp, $this->oActions);
if ($this->oActions->Config()->Get('debug', 'enable', false)) if ($this->oActions->Config()->Get('debug', 'enable', false))
@ -65,33 +58,6 @@ class Service
return new self(); return new self();
} }
/**
* @param int $iErrNo
* @param string $sErrStr
* @param string $sErrFile
* @param int $iErrLine
*
* @return bool
*/
public function LogPhpErrorHandler($iErrNo, $sErrStr, $sErrFile, $iErrLine)
{
$iType = \MailSo\Log\Enumerations\Type::NOTICE;
switch ($iErrNo)
{
case E_USER_ERROR:
$iType = \MailSo\Log\Enumerations\Type::ERROR;
break;
case E_USER_WARNING:
$iType = \MailSo\Log\Enumerations\Type::WARNING;
break;
}
$this->oActions->Logger()->Write($sErrFile.' [line:'.$iErrLine.', code:'.$iErrNo.']', $iType, 'PHP');
$this->oActions->Logger()->Write('Error: '.$sErrStr, $iType, 'PHP');
return !!(\MailSo\Log\Enumerations\Type::NOTICE === $iType && \RainLoop\Service::$__HIDE_ERROR_NOTICES);
}
/** /**
* @return \RainLoop\Service * @return \RainLoop\Service
*/ */