mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-24 11:08:45 +02:00
N°4261 - Portal exception logging: Add exception's file and line to the context.
plus: - the exception object is no more automatically added to the error.log context (it's way too verbose) - code cleanup (use constant instead of repeated strings) - ExceptionLog main method is now named `LogException` instead of `FromException`
This commit is contained in:
@@ -656,9 +656,9 @@ abstract class LogAPI
|
||||
* @throws \ConfigException if log wrongly configured
|
||||
* @uses GetMinLogLevel
|
||||
*/
|
||||
final public static function IsLogLevelEnabled(string $sLevel, string $sChannel, string $sCode = 'log_level_min'): bool
|
||||
final public static function IsLogLevelEnabled(string $sLevel, string $sChannel, string $code = 'log_level_min'): bool
|
||||
{
|
||||
$sMinLogLevel = self::GetMinLogLevel($sChannel, $sCode);
|
||||
$sMinLogLevel = self::GetMinLogLevel($sChannel, $code);
|
||||
|
||||
if ($sMinLogLevel === false || $sMinLogLevel === 'false') {
|
||||
return false;
|
||||
@@ -678,7 +678,6 @@ abstract class LogAPI
|
||||
|
||||
/**
|
||||
* @param string $sChannel
|
||||
* @param string $sCode
|
||||
*
|
||||
* @return string one of the LEVEL_* const value : the one configured it if exists, otherwise default log level for this channel
|
||||
* Config can be set :
|
||||
@@ -698,14 +697,14 @@ abstract class LogAPI
|
||||
*
|
||||
* @link https://www.itophub.io/wiki/page?id=3_0_0%3Aadmin%3Alog iTop log reference
|
||||
*/
|
||||
protected static function GetMinLogLevel($sChannel, $sCode = 'log_level_min')
|
||||
protected static function GetMinLogLevel($sChannel, $code = 'log_level_min')
|
||||
{
|
||||
$oConfig = static::GetConfig();
|
||||
if (!$oConfig instanceof Config) {
|
||||
return static::GetLevelDefault();
|
||||
}
|
||||
|
||||
$sLogLevelMin = $oConfig->Get($sCode);
|
||||
$sLogLevelMin = $oConfig->Get($code);
|
||||
|
||||
if (empty($sLogLevelMin)) {
|
||||
return static::GetLevelDefault();
|
||||
@@ -1107,9 +1106,9 @@ class LogFileRotationProcess implements iScheduledProcess
|
||||
/**
|
||||
* Log exceptions using dedicated API and logic.
|
||||
*
|
||||
* Please use {@see ExceptionLog::FromException()} to log exceptions
|
||||
* Please use {@see ExceptionLog::ExceptionLog()} to log exceptions
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @Since 3.0.0
|
||||
*/
|
||||
class ExceptionLog extends LogAPI
|
||||
{
|
||||
@@ -1135,11 +1134,10 @@ class ExceptionLog extends LogAPI
|
||||
$aContext['exception class'] = get_class($oException);
|
||||
}
|
||||
|
||||
self::Log($sLevel, $oException->getMessage(), get_class($oException), $aContext);
|
||||
return self::Log($sLevel, $oException->getMessage(), get_class($oException), $aContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws \ConfigException if log wrongly configured
|
||||
*/
|
||||
public static function Log($sLevel, $sMessage, $sClass = null, $aContext = array())
|
||||
@@ -1167,14 +1165,15 @@ class ExceptionLog extends LogAPI
|
||||
|
||||
}
|
||||
|
||||
protected static function FindClassChannel($sClass, $sCode = 'log_level_min')
|
||||
|
||||
protected static function FindClassChannel($sClass, $code = 'log_level_min')
|
||||
{
|
||||
$oConfig = static::GetConfig();
|
||||
if (!$oConfig instanceof Config) {
|
||||
return static::GetLevelDefault();
|
||||
}
|
||||
|
||||
$sLogLevelMin = $oConfig->Get($sCode);
|
||||
$sLogLevelMin = $oConfig->Get($code);
|
||||
|
||||
if (empty($sLogLevelMin)) {
|
||||
return $sClass;
|
||||
@@ -1201,9 +1200,6 @@ class ExceptionLog extends LogAPI
|
||||
return $sClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function Enable($sTargetFile = null)
|
||||
{
|
||||
if (empty($sTargetFile))
|
||||
@@ -1242,9 +1238,9 @@ class ExceptionLog extends LogAPI
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Used by the tests
|
||||
* used by the tests
|
||||
*/
|
||||
private static function GetLastEventIssue()
|
||||
private static function getLastEventIssue()
|
||||
{
|
||||
return self::$oLastEventIssue;
|
||||
}
|
||||
|
||||
@@ -86,7 +86,11 @@ class ExceptionListener implements ContainerAwareInterface
|
||||
}
|
||||
|
||||
// Log exception in iTop log
|
||||
\ExceptionLog::FromException($oException, ['uri' => $oEvent->getRequest()->getUri()]);
|
||||
\ExceptionLog::LogException($oException, [
|
||||
'uri' => $oEvent->getRequest()->getUri(),
|
||||
'file' => $oException->getFile(),
|
||||
'line' => $oException->getLine(),
|
||||
]);
|
||||
|
||||
// Prepare data for template
|
||||
$aData = array(
|
||||
|
||||
@@ -48,8 +48,7 @@ class ExceptionLogTest extends ItopDataTestCase
|
||||
|
||||
if (is_null($logLevelMin))
|
||||
{
|
||||
$oConf = \MetaModel::GetConfig();
|
||||
$logLevelMin = $oConf->Get('log_level_min');
|
||||
$logLevelMin = '';//this should be the default value, if it did change, please fix it here
|
||||
}
|
||||
|
||||
return $logLevelMin;
|
||||
@@ -57,8 +56,7 @@ class ExceptionLogTest extends ItopDataTestCase
|
||||
|
||||
if (is_null($logLevelMinWriteInDb))
|
||||
{
|
||||
$oConf = \MetaModel::GetConfig();
|
||||
$logLevelMinWriteInDb = $oConf->Get('log_level_min.write_in_db');
|
||||
$logLevelMinWriteInDb = [ 'Exception' => 'Error', ];//this should be the default value, if it did change, please fix it here
|
||||
}
|
||||
|
||||
return $logLevelMinWriteInDb;
|
||||
@@ -72,15 +70,15 @@ class ExceptionLogTest extends ItopDataTestCase
|
||||
$oException = new $aExceptions[$i]("Iteration number $i");
|
||||
$iExpectedWriteNumber = $aExpectedWriteNumber[$i];
|
||||
$iExpectedDbWriteNumber = $aExpectedDbWriteNumber[$i];
|
||||
$aExpectedContext = array_merge($aContext, ['exception' => $oException, 'exception class' => get_class($oException)]); //The context is preserved, and, if the key 'exception' is not yet in the array, it is added
|
||||
$aExpectedFileContext = array_merge($aContext, ['__exception class' => get_class($oException)]); //The context is preserved, and, if the key '__exception' is not yet in the array, it is added
|
||||
$mockFileLog->expects($this->exactly($iExpectedWriteNumber))
|
||||
->method($sLevel)
|
||||
->with($oException->GetMessage(), $sChannel, $aExpectedContext)
|
||||
->with($oException->GetMessage(), $sChannel, $aExpectedFileContext)
|
||||
;
|
||||
|
||||
ExceptionLog::MockStaticObjects($mockFileLog, $oMockConfig);
|
||||
|
||||
ExceptionLog::FromException($oException, $aContext, $sLevel);
|
||||
ExceptionLog::LogException($oException, $aContext, $sLevel);
|
||||
|
||||
$oExpectedLastEventIssue = $this->InvokeNonPublicStaticMethod('ExceptionLog', 'GetLastEventIssue', []);
|
||||
|
||||
@@ -88,7 +86,7 @@ class ExceptionLogTest extends ItopDataTestCase
|
||||
$this->assertNull($oExpectedLastEventIssue);
|
||||
} else {
|
||||
$this->assertInstanceOf(\EventIssue::class, $oExpectedLastEventIssue);
|
||||
$this->assertEquals($aExpectedContext, $oExpectedLastEventIssue->Get('data'));
|
||||
$this->assertEquals($aExpectedFileContext, $oExpectedLastEventIssue->Get('data'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user